Spring Boot 应用系列 2 -- Spring Boot 2 整合MyBatis和Druid
本系列将分别演示单数据源和多数据源的配置和应用,本文先演示单数据源(MySQL)的配置。
1. pom.xml文件配置
需要在dependencies节点添加:
<!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.12</version> </dependency> <!-- MySQL end --> <!-- Connection Pool --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!-- Connection Pool end --> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- MyBatis end -->
2. 因为是单数据源,为了方便起见,我们把数据源和连接池全部放到application.yml里,当然数据源的配置也可以另起一个新文件。
1 server: 2 port: 9007 3 4 spring: 5 datasource: 6 name: devutility-test-database-mybatis-springboot 7 type: com.alibaba.druid.pool.DruidDataSource 8 druid: 9 driver-class-name: com.mysql.cj.jdbc.Driver 10 url: ${DB_URL:jdbc:mysql://127.0.0.1:3306/Test}?useUnicode=true&useSSL=false 11 username: ${DB_UID:tester} 12 password: ${DB_PWD:tester} 13 initial-size: 1 14 min-idle: 1 15 max-active: 20 16 max-wait: 60000 17 time-between-eviction-runs-millis: 60000 18 min-evictable-idle-time-millis: 300000 19 validation-query: SELECT 'x' 20 test-while-idle: true 21 test-on-borrow: false 22 test-on-return: false 23 pool-prepared-statements: false 24 max-pool-prepared-statement-per-connection-size: 20 25 filters: stat 26 mybatis: 27 mapper-locations: classpath:mappers/*.xml 28 type-aliases-package: devutility.test.database.mybatis.springboot.entities
第27行定义了mapper的xml文件的地址;
第28行定义了实体类的包,在mapper的xml文件中可以只写实体类名而不必写全实体类的命名空间。
3. MyBatis相关配置
MyBatis的配置主要涉及到mapper的xml文件,mapper的接口文件,以及数据库相关的实体类的定义。
(1) 首先定义一个客户实体类:
package devutility.test.database.mybatis.springboot.entities; public class Customer extends BaseEntity { private long id; private String name1; private String address1; private String city; private int state; private int zip; private String phone; private String email;
(2) 定义mapper的xml文件,也叫XML映射文件,存放于src/main/resources/mappers目录下。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="devutility.test.database.mybatis.springboot.mappers.CustomerMapper"> <select id="get" resultType="Customer"> select * from Customer where id = #{id}; </select> </mapper>
XML映射文件的定义规则可以参考MyBatis官网,此处我们仅实现一个根据主键查询Customer的功能。
(3) 定义Mapper接口:
package devutility.test.database.mybatis.springboot.mappers; import org.apache.ibatis.annotations.Mapper; import devutility.test.database.mybatis.springboot.entities.Customer; @Mapper public interface CustomerMapper { Customer get(long id); }
4. 应用
package devutility.test.database.mybatis.springboot.controllers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import devutility.test.database.mybatis.springboot.entities.Customer; import devutility.test.database.mybatis.springboot.mappers.CustomerMapper; @RestController @RequestMapping("/customer") public class CustomerController { @Autowired private CustomerMapper customerMapper; @RequestMapping("/get") public Customer get(long id) { return customerMapper.get(id); } }