springboot+mybatis+annotation注解集成

1,创建项目

2,pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>springboot</groupId>
    <artifactId>springboot-mybatis-annotation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-mybatis-annotation</name>
    <description>Springboot-mybatis :: 整合 Mybatis Annotation 案例</description>

    <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <mybatis-spring-boot>1.2.0</mybatis-spring-boot>
        <mysql-connector>5.1.39</mysql-connector>
    </properties>

    <dependencies>
        <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Spring Boot Mybatis 依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>

        <!-- MySQL 连接驱动依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector}</version>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>


</project>

3,entity,service,controller

package org.spring.springboot.domain;

/**
 * 城市实体类
 */
public class City {

    /**
     * 城市编号
     */
    private Long id;

    /**
     * 省份编号
     */
    private Long provinceId;

    /**
     * 城市名称
     */
    private String cityName;

    /**
     * 描述
     */
    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(Long provinceId) {
        this.provinceId = provinceId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
package org.spring.springboot.service;

import org.spring.springboot.domain.City;

/**
 * 城市业务逻辑接口类
 */
public interface CityService {

    /**
     * 根据城市名称,查询城市信息
     * @param cityName
     */
    City findCityByName(String cityName);

    int insertCity(City city);

    int updateCity(City city);

    int deleteCityById(Long id);
}
package org.spring.springboot.controller;

import org.spring.springboot.domain.City;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * springboot mybatis annotation spring+mybatis+注解集成
 */
@RestController
public class CityRestController {

    @Autowired
    private CityService cityService;
    /***
     * 查询
     * @param cityName
     * @return
     */
    @RequestMapping(value = "/api/city", method = RequestMethod.GET)
    public City findOneCity(@RequestParam(value = "cityName", required = true) String cityName) {
        return cityService.findCityByName(cityName);
    }
    /***
     * 添加
     * @param city
     * @return
     */
    @RequestMapping(value = "/api/insert", method = RequestMethod.POST)
    public int insertOneCity(@RequestBody City city) {
        return cityService.insertCity(city);
    }
    
    /***
     * 修改
     * @param city
     * @return
     */
    @RequestMapping(value = "/api/update", method = RequestMethod.POST)
    public int updateOneCity(@RequestBody City city) {
        return cityService.updateCity(city);
    }
    
    /***
     * 删除
     * @param city
     * @return
     */
    @RequestMapping(value = "/api/delete", method = RequestMethod.GET)
    public int deleteCityById(@RequestParam(value = "id", required = true) Long id) {
        return cityService.deleteCityById(id);
    }
    

}

4,注解相关的dao,mapper文件

package org.spring.springboot.dao;

import org.apache.ibatis.annotations.*;
import org.spring.springboot.domain.City;

/**
 * 城市 DAO 接口类
 *
 * Created by xchunzhao on 02/05/2017.
 */
@Mapper // 标志为 Mybatis 的 Mapper
public interface CityDao {

    /**
     * 根据城市名称,查询城市信息
     *
     * @param cityName 城市名
     */
    @Select("SELECT * FROM city")   
    // 返回 Map 结果集
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "provinceId", column = "province_id"),
            @Result(property = "cityName", column = "city_name"),
            @Result(property = "description", column = "description"),
    })
    City findByName(@Param("cityName") String cityName);
    @Insert({
        "insert into city (" +
                "id, " +
                "province_id, " +
                "city_name, " +      
                "description)",
        "values (" +
                "#{id,jdbcType=BIGINT}, " +
                "#{provinceId,jdbcType=BIGINT}, " +
                "#{cityName,jdbcType=VARCHAR}, " +              
                "#{description,jdbcType=VARCHAR})"
    })  
    int insertCity(City city);
    
    @Update({
        "update city set province_id=#{provinceId,jdbcType=BIGINT},city_name=#{cityName,jdbcType=VARCHAR},description=#{description,jdbcType=VARCHAR} where id=#{id,jdbcType=BIGINT}"
    })
    
    int updateCity(City city);
    
    @Delete("delete from city where id=#{id,jdbcType=BIGINT}")
    int deleteCityById(Long id);
}

5,application.properties

##db
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

6,Application.java

package org.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 7,前端发送请求

8,查看数据库,添加成功

 

posted on 2017-10-24 16:07  让代码飞  阅读(325)  评论(0)    收藏  举报

导航

一款免费在线思维导图工具推荐:https://www.processon.com/i/593e9a29e4b0898669edaf7f?full_name=python