背景
这几天搞了个saas项目,里面用到了多数据和execl模板导出功能,
其实我是经常用到的,但没在博客中写过,最近有点时间,正好稍微写一下。 方便大家使用
这次我先写多数据,execl模板导出下次有空在写。
使用
pom引入
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>
这里因为需要用的多个数据库 所以我又引入了mysql和postgresql,大家居然自己的情况来。
mysql版本我使用的是8.0.16
druid版本我使用的是1.1.20
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
配置文件
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=5
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.dynamic.primary=master
spring.datasource.dynamic.strict=false
spring.datasource.dynamic.datasource.master.url=jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
spring.datasource.dynamic.datasource.master.username=root
spring.datasource.dynamic.datasource.master.password=123456
spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.slave_1.url=jdbc:postgresql://127.0.0.1:45565/postgres
spring.datasource.dynamic.datasource.slave_1.username=root
spring.datasource.dynamic.datasource.slave_1.password=123456
spring.datasource.dynamic.datasource.slave_1.driver-class-name=org.postgresql.Driver
如果还有多个数据源的话,继续添加slave_xx就可以了
这里有个注意点spring.datasource.dynamic.primary=master 这里是指的默认的数据源,大家根据自身的情况调整。
代码实现
这里的话,我只是写的例子,所以比较简单。
model
@Setter
@Getter
public class TestMysql {
private Integer id;
private String name;
}
@Setter
@Getter
public class TestPg {
private Integer id;
private String name;
}
mapper
@Mapper
public interface TestMysqlMapper extends BaseMapper<TestMysql> {
TestMysql select();
}
@Mapper
public interface TestPgMapper extends BaseMapper<TestPg> {
TestPg select();
}
xml
<?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.secusoft.demo.mapper.TestPgMapper">
<select id="select" resultType="com.secusoft.demo.model.TestPg">
select * from test.test_pg
</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.secusoft.demo.mapper.TestMysqlMapper">
<select id="select" resultType="com.secusoft.demo.model.TestMysql">
select * from test_mysql
</select>
</mapper>
service
public interface TestPgService extends IService<TestPg> {
TestPg select();
}
public interface TestMysqlService extends IService<TestMysql> {
TestMysql select();
}
实现
@Service
@DS("slave_1")
public class TestPgServiceImpl extends ServiceImpl<TestPgMapper, TestPg> implements TestPgService {
@Override
public TestPg select() {
return this.baseMapper.select();
}
}
@Service
public class TestMysqlServiceImpl extends ServiceImpl<TestMysqlMapper, TestMysql> implements TestMysqlService {
@Override
public TestMysql select() {
return this.baseMapper.select();
}
}
这里有个注意点:@DS(“slave_1”)注解 对应的就是上面写的slave_1,master可默认不写
controller
@RestController
@RequestMapping("/test")
public class TestController {
@Resource
private TestMysqlService testMysqlService;
@Resource
private TestPgService testPgService;
@GetMapping("/sql")
public Object sql() {
TestMysql testMysql = testMysqlService.select();
TestPg testPg = testPgService.select();
List<Object> list=new ArrayList<>();
list.add(testMysql);
list.add(testPg);
return list;
}
}
调用测试
完成。