您的位置:首页 > 编程语言 > Java开发

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

2017-06-29 17:09 746 查看
本例将采用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-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>1.5.1.RELEASE</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis-annotation</artifactId>
<packaging>war</packaging>
<name>mybatis-annotation Demo</name>
<url>http://maven.apache.org</url>

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

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-spring-boot}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mybatis-annotation</finalName>
</build>
</project>


2.工程结构



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":"我的家在山东省潍坊市。"}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: