项目框架:SpringBoot + MyBatis + Mysql
项目连接两个数据库源。
1、application.yml配置
- 使用DruidDataSource
- 主数据库为primary,副数据库为secondary,名称可以自己定义,只要和后续的配置文件config相对应即可
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
#配置初始化大小/最小/最大
initial-size: 5
min-idle: 5
max-active: 100
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
primary:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://10.0.254.101:3306/test001?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false # 测试
username: root
password: 123456
secondary:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://10.0.254.102:3306/test001?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false # 测试
username: root
password: 123456
2、在config包下新增配置文件:PrimaryDataSourceConfig和SecondaryDataSourceConfig,结构如下:
PrimaryDataSourceConfig:
```java
@Configuration
@MapperScan(basePackages = "com.test.dao.primary", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class PrimaryDataSourceConfig {
private static final String MAPPER_LOCATION = "classpath:mapper/primary/*.xml";
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//设置mapper配置文件
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
return bean.getObject();
}
@Bean(name = "primaryTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
SecondaryDataSourceConfig:
@Configuration
@MapperScan(basePackages = "com.test.dao.secondary", sqlSessionTemplateRef = "secondarySqlSessionTemplate")
public class SecondaryDataSourceConfig {
private static final String MAPPER_LOCATION = "classpath:mapper/secondary/*.xml";
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//设置mapper配置文件
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
return bean.getObject();
}
@Bean(name = "secondaryTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "secondarySqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
3、dao层文件结构如下所示,要分开来存放,因为上面配置了basePackages
StudentDao示例代码如下:
StudentDao:
@Repository
public interface StudentDao {
int selectStudentById(Long id);
}
4、Mapper文件
结构如下,一样需要分文件夹,因为在上述配置文件中指定了mapper文件夹MAPPER_LOCATION :
5、StudentMapper.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.test.dao.primary.StudentDao">
<select id="selectStudentById" parameterType="java.lang.Long" resultType="com.test.entity.Student">
select *
from student
where id = #{id}
</select>
</mapper>