springboot之整合mybatis-annotation(注解方式)

本例将采用maven管理,代码托管在github上,地址:https://github.com/wolf909867753/springboot。

1。创建maven-module,mybatis-annotation,并在pom.xml中添加springboot依赖

<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/maven-v4_0_0.xsd">
    <parent>
        <artifactId>spring-boot-starter-parentartifactId>
        <groupId>org.springframework.bootgroupId>
        <version>1.5.1.RELEASEversion>
    parent>

    <modelVersion>4.0.0modelVersion>
    <artifactId>mybatis-annotationartifactId>
    <packaging>warpackaging>
    <name>mybatis-annotation Demoname>
    <url>http://maven.apache.orgurl>

    <properties>
        <mybatis-spring-boot>1.2.0mybatis-spring-boot>
        <mysql-spring-boot>5.1.32mysql-spring-boot>
    properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>${mybatis-spring-boot}version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql-spring-boot}version>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
    dependencies>
    <build>
        <finalName>mybatis-annotationfinalName>
    build>
project>

2.工程结构

springboot之整合mybatis-annotation(注解方式)_第1张图片

3.创建工程mybaits,添加配置文件resources\application.properties工程

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


    目录如下:
    com\springboot\domain\City.java
    

public class City implements Serializable{


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

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

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

    /**
     * 描述
     */
    private String description;
        getter/setter.....

}   


    com\springboot\dao\CityDao.java
    

/**
 * 城市 DAO 接口类
 * Created by wanglu-jf on 17/6/27.
 * @Mapper 标志接口为 MyBatis Mapper 接口
 * @Select 是 Select 操作语句
 * @Results 标志结果集,以及与库表字段的映射关系
 */
@Mapper
public interface CityDao {
    /**
     * 查询城市信息
     */
    @Select(" SELECT * FROM city WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "provinceId", column = "province_id"),
            @Result(property = "cityName", column = "city_name"),
            @Result(property = "description", column = "description")
    })
    public City queryCityById(@Param("id") int id);
}

   
    com\springboot\service\ICityService.java
    
public interface ICityService {

    /**
     * 查询城市信息
     */
    public City queryById(int id);
}

    
    com\springboot\service\impl\CityServiceImpl.java
    
@Service
public class CityServiceImpl implements ICityService {

    @Autowired
    private CityDao cityDao;
    /**
     * 查询城市信息
     */
    @Override
    public City queryById(int id) {
        return cityDao.queryCityById(id);
    }
}

    
    com\springboot\controller\CityController.java
    
    
@RestController
@RequestMapping("city/")
public class CityController {

    @Autowired
    private ICityService cityService;

    @RequestMapping(value = "query/{id}",method = RequestMethod.GET)
    public City queryById(@PathVariable("id") int id){
        return this.cityService.queryById(id);
    }
}


    com\springboot\Application.java
    
@SpringBootApplication
public class Application {

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

    
4.运行Application.java的main方法或者发布到tomcat,
    访问http://127.0.0.1:8080/city/query/%E6%BD%8D%E5%9D%8A%E5%B8%82,
    输出
        {"id":1,"provinceId":1,"cityName":"潍坊市","description":"我的家在山东省潍坊市。"}
















你可能感兴趣的:(springboot,java)