mybatis-plus封装了单个表的基本操作,也比较符合spring-boot的基本风格,本次尝试集成了spring-boot+mybatis+mybatis-plus+druid的多数据源,话不多说,直接贴代码
数据源数据连接配置:
/**
* Project Name:fire-industry-DM-center
* File Name:DataSourcePrimaryConfig.java
* Package Name:com.firestone.config.mybatis
* Date:2018年3月13日下午3:18:40
*
*/
package com.firestone.config.mybatis;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
/**
* @Description: TODO(第一数据源数据连接配置)
* @author CAIYJ
* @date 2018年3月13日 下午3:18:40
*/
@Configuration
@MapperScan(basePackages = "com.firestone.dao.primary", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class DataSourcePrimaryConfig {
private static final Logger logger = LogManager
.getLogger(DataSourcePrimaryConfig.class);
@Bean(name = "primaryDataSource", initMethod = "init", destroyMethod = "close")
@Primary
public DruidDataSource dataSource(
DruidConfigPrimaryProperties druidConfigPrimaryProperties)
throws SQLException {
logger.info("初始化primaryDataSource");
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(
druidConfigPrimaryProperties.getDriverClassName());
druidDataSource.setUrl(druidConfigPrimaryProperties.getUrl());
druidDataSource.setUsername(druidConfigPrimaryProperties.getUsername());
druidDataSource.setPassword(druidConfigPrimaryProperties.getPassword());
druidDataSource
.setInitialSize(druidConfigPrimaryProperties.getMinIdle());
druidDataSource.setMinIdle(druidConfigPrimaryProperties.getMinIdle());
druidDataSource
.setMaxActive(druidConfigPrimaryProperties.getMaxActive());
druidDataSource.setMaxWait(druidConfigPrimaryProperties.getMaxWait());
druidDataSource
.setTimeBetweenEvictionRunsMillis(druidConfigPrimaryProperties
.getTimeBetweenEvictionRunsMillis());
druidDataSource.setMinEvictableIdleTimeMillis(
druidConfigPrimaryProperties.getMinEvictableIdleTimeMillis());
druidDataSource.setValidationQuery(
druidConfigPrimaryProperties.getValidationQuery());
druidDataSource.setTestWhileIdle(
druidConfigPrimaryProperties.getTestWhileIdle());
druidDataSource.setTestOnBorrow(
druidConfigPrimaryProperties.getTestOnBorrow());
druidDataSource.setTestOnReturn(
druidConfigPrimaryProperties.getTestOnReturn());
druidDataSource.setPoolPreparedStatements(
druidConfigPrimaryProperties.getPoolPreparedStatements());
druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(
druidConfigPrimaryProperties
.getMaxPoolPreparedStatementPerConnectionSize());
druidDataSource.setFilters(druidConfigPrimaryProperties.getFilters());
return druidDataSource;
}
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(
@Qualifier("primaryDataSource") DataSource dataSource,
GlobalConfiguration globalConfiguration) throws Exception {
String mapperLocations = "classpath:mapper/primary/*.xml";
String configLocation = "classpath:mybatis-sqlconfig.xml";
String typeAliasesPackage = "com.firestone.bean.entity.mysql.primary";
MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
// 数据源
factory.setDataSource(dataSource);
// 全局配置
factory.setGlobalConfig(globalConfiguration);
Interceptor[] interceptor = { new PaginationInterceptor() };
// 分页插件
factory.setPlugins(interceptor);
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
// 自动扫描Mapping.xml文件
factory.setMapperLocations(resolver.getResources(mapperLocations));
factory.setConfigLocation(resolver.getResource(configLocation));
factory.setTypeAliasesPackage(typeAliasesPackage);
return factory.getObject();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
// MyBatis 动态扫描
@Bean(name = "primaryMapperScannerConfigurer")
public MapperScannerConfigurer mapperScannerConfigurer() {
logger.info("初始化primaryMapperScannerConfigurer");
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
String basePackage = "com.firestone.dao.primary";
mapperScannerConfigurer.setBasePackage(basePackage);
return mapperScannerConfigurer;
}
@Bean(name = "primaryTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(
@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(
@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory)
throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
数据源连接属性
/**
* Project Name:fire-industry-DM-center
* File Name:DruidConfigPrimaryProperties.java
* Package Name:com.firestone.config.mybatis
* Date:2018年3月13日下午3:07:00
*
*/
package com.firestone.config.mybatis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Description: TODO(MYSQL数据连接第一数据源连接属性)
* @author CAIYJ
* @date 2018年3月13日 下午3:07:00
*/
@Component
@ConfigurationProperties(prefix = "spring.datasource.primary")
public class DruidConfigPrimaryProperties {
/**
* @Fields driverClassName : TODO(使用的JDBC驱动的完整有效的java 类名)
*/
private String driverClassName;
/**
* @Fields url : TODO( 传递给JDBC驱动的用于建立连接的UR)
*/
private String url;
/**
* @Fields username : TODO(传递给JDBC驱动的用于建立连接的用户名)
*/
private String username;
/**
* @Fields password : TODO(传递给JDBC驱动的用于建立连接的密码)
*/
private String password;
/**
* @Fields minIdle : TODO(最大空闲连接 ;如果设置为0则不创建)
*/
private Integer minIdle;
/**
* @Fields maxActive : TODO(最大活动连接 ;如果设置为非正数则表示不限制)
*/
private Integer maxActive;
/**
* @Fields maxWait : TODO(最大等待时间 -1表示无限等待)
*/
private Integer maxWait;
/**
* @Fields timeBetweenEvictionRunsMillis : TODO(在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位.
* 如果设置为非正数,则不运行空闲连接回收器线程)
*/
private Long timeBetweenEvictionRunsMillis;
/**
* @Fields minEvictableIdleTimeMillis :
* TODO(连接在池中保持空闲而不被空闲连接回收器线程(如果有)回收的最小时间值,单位毫秒)
*/
private Long minEvictableIdleTimeMillis;
/**
* @Fields validationQuery :
* TODO(SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前.如果指定,则查询必须是一个SQL
* SELECT并且必须返回至少一行记录)
*/
private String validationQuery;
/**
* @Fields testWhileIdle : TODO(指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.
* 注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串)
*/
private Boolean testWhileIdle;
/**
* @Fields testOnBorrow : TODO(指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个.
* 注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串)
*/
private Boolean testOnBorrow;
/**
* @Fields testOnReturn : TODO(指明是否在归还到池中前进行检验 注意:
* 设置为true后如果要生效,validationQuery参数必须设置为非空字符串)
*/
private Boolean testOnReturn;
/**
* @Fields poolPreparedStatements : TODO( 开启池的prepared statement 池功能)
*/
private Boolean poolPreparedStatements;
/**
* @Fields maxPoolPreparedStatementPerConnectionSize : TODO(每个连接上PSCache的大小
* )
*/
private Integer maxPoolPreparedStatementPerConnectionSize;
/**
* @Fields filters : TODO(属性类型是字符串,通过别名的方式配置扩展插件)
*/
private String filters;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getMinIdle() {
return minIdle;
}
public void setMinIdle(Integer minIdle) {
this.minIdle = minIdle;
}
public Integer getMaxActive() {
return maxActive;
}
public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
}
public Integer getMaxWait() {
return maxWait;
}
public void setMaxWait(Integer maxWait) {
this.maxWait = maxWait;
}
public Long getTimeBetweenEvictionRunsMillis() {
return timeBetweenEvictionRunsMillis;
}
public void setTimeBetweenEvictionRunsMillis(
Long timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}
public Long getMinEvictableIdleTimeMillis() {
return minEvictableIdleTimeMillis;
}
public void setMinEvictableIdleTimeMillis(Long minEvictableIdleTimeMillis) {
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}
public String getValidationQuery() {
return validationQuery;
}
public void setValidationQuery(String validationQuery) {
this.validationQuery = validationQuery;
}
public Boolean getTestWhileIdle() {
return testWhileIdle;
}
public void setTestWhileIdle(Boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
}
public Boolean getTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(Boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
public Boolean getTestOnReturn() {
return testOnReturn;
}
public void setTestOnReturn(Boolean testOnReturn) {
this.testOnReturn = testOnReturn;
}
public Boolean getPoolPreparedStatements() {
return poolPreparedStatements;
}
public void setPoolPreparedStatements(Boolean poolPreparedStatements) {
this.poolPreparedStatements = poolPreparedStatements;
}
public Integer getMaxPoolPreparedStatementPerConnectionSize() {
return maxPoolPreparedStatementPerConnectionSize;
}
public void setMaxPoolPreparedStatementPerConnectionSize(
Integer maxPoolPreparedStatementPerConnectionSize) {
this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
}
public String getFilters() {
return filters;
}
public void setFilters(String filters) {
this.filters = filters;
}
}