数据库:mariadb
一:mvn依赖
<!--数据源-->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<!--mariadb-->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-annotation</artifactId>
<version>3.5.4</version>
</dependency>
二:配置yml
--- # 数据源配置
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
first:
driverClassName: org.mariadb.jdbc.Driver
jdbcUrl: jdbc:mariadb://127.0.0.1:3308/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
username: root
password: 123456
type: ${spring.datasource.type}
# 最大连接池数量
maxPoolSize: 20
# 最小空闲线程数量
minIdle: 10
# 配置获取连接等待超时的时间
connectionTimeout: 30000
# 校验超时时间
validationTimeout: 5000
# 空闲连接存活最大时间,默认10分钟
idleTimeout: 600000
# 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认30分钟
maxLifetime: 1800000
# 连接测试query(配置检测连接是否有效)
connectionTestQuery: SELECT 1
# 多久检查一次连接的活性
keepaliveTime: 30000
second:
driverClassName: org.mariadb.jdbc.Driver
jdbcUrl: jdbc:mariadb://127.0.0.1:3308/INFORMATION_SCHEMA?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
username: root
password: 123456
type: ${spring.datasource.type}
# 最大连接池数量
maxPoolSize: 20
# 最小空闲线程数量
minIdle: 10
# 配置获取连接等待超时的时间
connectionTimeout: 30000
# 校验超时时间
validationTimeout: 5000
# 空闲连接存活最大时间,默认10分钟
idleTimeout: 600000
# 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认30分钟
maxLifetime: 1800000
# 连接测试query(配置检测连接是否有效)
connectionTestQuery: SELECT 1
# 多久检查一次连接的活性
keepaliveTime: 30000
--- # mybatis-plus配置
mybatis-plus:
global-config:
dbConfig:
idType: ASSIGN_ID #id使用雪花算法
三:config配置数据源
1.第一个数据源
/**
* @Description first数据源
*/
@Configuration
@MapperScan(basePackages = "com.das.demo.mapper", sqlSessionFactoryRef = "firstSqlSessionFactory")
public class FirstDataSourceConfiguration {
//插件
@Autowired
MybatisPlusInterceptor mybatisPlusInterceptor;
/**
* 指定mapper xml文件路径
*/
public static final String MAPPER_LOCATION = "classpath*:mapper/*Mapper.xml";
/**
* 全局自定义配置
*/
@Bean(name = "globalConfig")
@ConfigurationProperties(prefix = "mybatis-plus")
public GlobalConfig globalConfig() {
return new GlobalConfig();
}
@Primary
@Bean("firstHikariConfig")
@ConfigurationProperties(prefix = "spring.datasource.first")
public HikariConfig firstHikariConfig() {
return new HikariConfig();
}
@Primary
@Bean("firstDataSource")
public HikariDataSource firstDataSource(@Qualifier("firstHikariConfig") HikariConfig hikariConfig) {
return new HikariDataSource(hikariConfig);
}
/**
* 配置事务管理器
*
* @return
*/
@Bean(name = "firstTransactionManager")
@Primary
public PlatformTransactionManager firstTransactionManager(@Qualifier("firstDataSource") HikariDataSource hikariDataSource) {
return new DataSourceTransactionManager(hikariDataSource);
}
/**
* 自定义SQLSession工厂
*
* @return , org.apache.ibatis.session.Configuration configuration
* @throws Exception
*/
@Primary
@Bean(name = "firstSqlSessionFactory")
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") HikariDataSource hikariDataSource,
@Qualifier("globalConfig") GlobalConfig globalConfig) throws Exception {
MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
sessionFactoryBean.setDataSource(hikariDataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
sessionFactoryBean.setGlobalConfig(globalConfig);
// sessionFactoryBean.setPlugins(mybatisPlusInterceptor);
// MybatisConfiguration configuration = new MybatisConfiguration();
// configuration.setMapUnderscoreToCamelCase(true);
// // 配置打印sql语句
// configuration.setLogImpl(StdOutImpl.class);
// sessionFactoryBean.setConfiguration(configuration);
return sessionFactoryBean.getObject();
}
@Primary
@Bean(name = "firstSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory firstSqlSessionFactory) throws Exception {
return new SqlSessionTemplate(firstSqlSessionFactory);
}
@Bean("waterGlobalConfig")
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setMetaObjectHandler(new UpdateMetaObjectHandler());
return globalConfig;
}
2.第二个数据源
/**
* @Description second数据源
*/
@Configuration
@MapperScan(basePackages = "com.das.demo.mapper", sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfiguration {
/**
* 指定mapper xml文件路径
*/
public static final String MAPPER_LOCATION = "classpath*:mapper/*Mapper.xml";
@Bean("secondHikariConfig")
@ConfigurationProperties(prefix = "spring.datasource.second")
public HikariConfig secondHikariConfig() {
return new HikariConfig();
}
@Bean("secondDataSource")
public HikariDataSource secondDataSource(@Qualifier("secondHikariConfig") HikariConfig hikariConfig) {
return new HikariDataSource(hikariConfig);
}
/**
* 配置事务管理器
*
* @return
*/
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager secondTransactionManager(@Qualifier("secondDataSource") HikariDataSource hikariDataSource) {
return new DataSourceTransactionManager(hikariDataSource);
}
/**
* 自定义SQLSession工厂
*
* @return
* @throws Exception
*/
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") HikariDataSource hikariDataSource) throws Exception {
MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
sessionFactoryBean.setDataSource(hikariDataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
return sessionFactoryBean.getObject();
}
@Bean(name = "secondSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
分页配置:
/**
* MybatisPlus分页配置
*/
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL));
return interceptor;
}
}