本系列将分别演示单数据源和多数据源的配置和应用,本文先演示单数据源(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里,当然数据源的配置也可以另起一个新文件。
server:
port: spring:
datasource:
name: devutility-test-database-mybatis-springboot
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: ${DB_URL:jdbc:mysql://127.0.0.1:3306/Test}?useUnicode=true&useSSL=false
username: ${DB_UID:tester}
password: ${DB_PWD:tester}
initial-size:
min-idle:
max-active:
max-wait:
time-between-eviction-runs-millis:
min-evictable-idle-time-millis:
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size:
filters: stat
mybatis:
mapper-locations: classpath:mappers/*.xml
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);
}
}