springboot整合mybatisplus配置多数据源
1、hikari方式
第一步、导入依赖包
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybaitsplus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<!--dynamic-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${mybatis.dynamic.version}</version>
</dependency>
第二步、编写配置文件
#公共参数
#Mysql
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#最小空闲连接
spring.datasource.hikari.minimum-idle=10
#最大连接数
spring.datasource.hikari.maximum-pool-size=30
#连接最小空闲时间(单位:毫秒)
spring.datasource.hikari.idle-timeout=600000
#连接最大存活时间(单位:毫秒)
spring.datasource.hikari.max-lifetime=1800000
#连接池获取连接的最长等待时间(单位:毫秒)
spring.datasource.hikari.connection-timeout=30000
#验证查询
spring.datasource.hikari.connection-test-query=SELECT 1
#不指定默认该库
spring.datasource.dynamic.primary=db1
spring.datasource.dynamic.datasource.db1.username=root
spring.datasource.dynamic.datasource.db1.password=123456
spring.datasource.dynamic.datasource.db1.url=jdbc:mysql://192.168.56.66:3306/db1?serverTimezone=UTC
spring.datasource.dynamic.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.db2.username=root
spring.datasource.dynamic.datasource.db2.password=123456
spring.datasource.dynamic.datasource.db2.url=jdbc:mysql://192.168.56.66:3307/db2?serverTimezone=UTC
spring.datasource.dynamic.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver
第三步、编写配置
@Slf4j
@Configuration
//使用@DS注解区分
@MapperScan({"com.kalen.utils.mapper.master", "com.kalen.utils.mapper.slave"})
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
第四步、编写实体类
@Data
@TableName(value = "tb1")
public class Tb1 {
@TableId(value = "id",type = IdType.AUTO)
private int id;
private String name;
private String password;
private int gender;
private String email;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") //用于Spring,接收请求的时间数据格式
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone ="GMT+8") //用于返回JSON数据
private Date birthday;
}
@Data
@TableName(value = "tb2")
public class Tb2 {
@TableId(value = "id",type = IdType.AUTO)
private int id;
@TableField(value = "user_id")
private int userId;
private String note;
private String permissionIds;
}
第五步、编写mapper文件
public interface Tb1Mapper extends BaseMapper<Tb1> {
//测试是否读取mapper.xml创建代理对象
List<Tb1> selectAll();
}
public interface Tb2Mapper extends BaseMapper<Tb2> {
List<Tb2> selectAll();
}
注意:idea有个坑,就是在resources目录下创建文件的时候要注意层级,目录名称必须和java一致,mapper对应原则
<?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="com.kalen.utils.mapper.master.Tb1Mapper">
<resultMap id="BaseResultMap" type="com.kalen.utils.entity.pojo.Tb1">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="password" property="password"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="birthday" property="birthday"/>
</resultMap>
<sql id="Base_Column_List">
id, 'name', password, gender, email, birthday
</sql>
<select id="selectAll" resultType="com.kalen.utils.entity.pojo.Tb1">
select * from db1.tb1
</select>
</mapper>
<?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="com.kalen.utils.mapper.slave.Tb2Mapper">
<resultMap id="BaseResultMap" type="com.kalen.utils.entity.pojo.Tb2">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="note" property="note"/>
<result column="permission_ids" property="permissionIds"/>
</resultMap>
<sql id="Base_Column_List">
id, user_id, note, permission_ids
</sql>
<select id="selectAll" resultType="com.kalen.utils.entity.pojo.Tb2">
select * from db2.tb2
</select>
</mapper>
第六步、编写service和实现类
public interface ITb1Service extends IService<Tb1> {
List<Tb1> selectAll();
}
@Service
@DS("db1")
public class Tb1ServiceImpl extends ServiceImpl<Tb1Mapper,Tb1> implements ITb1Service {
@Override
public List<Tb1> selectAll() {
return this.baseMapper.selectAll();
}
}
public interface ITb2Service extends IService<Tb2> {
List<Tb2> selectAll();
}
@Service
@DS("db2")
public class Tb2ServiceImpl extends ServiceImpl<Tb2Mapper, Tb2> implements ITb2Service {
@Override
public List<Tb2> selectAll() {
return this.baseMapper.selectAll();
}
}
第七步、编写测试类
@SpringBootTest
class KlUtilsApplicationTests {
@Autowired
private ITb1Service tb1Service;
@Autowired
private ITb2Service tb2Service;
@Test
void contextLoads() {
Tb1 tb1 = tb1Service.getById(1);
Tb2 tb2 = tb2Service.getById(1);
System.out.println(tb1);
System.out.println(tb1Service.selectAll());
System.out.println(tb2);
System.out.println(tb2Service.selectAll());
}
}
Tb1(id=1, name=kalen, password=abc123, gender=0, email=986739985, birthday=1998-07-08)
[Tb1(id=1, name=kalen, password=abc123, gender=0, email=986739985, birthday=1998-07-08)]
Tb2(id=1, userId=1, note=超级管理员, permissionIds=1,2,3,4,5,6,7,8)
[Tb2(id=1, userId=1, note=超级管理员, permissionIds=1,2,3,4,5,6,7,8)]