JAVA技术交流QQ群:170933152
1.这里使用MySql和PostGreSql做为数据源
2.可以同时在xml中配置和在配置类中配置,
不过这里用配置类配置,xml那种没有用到,稍后,
放到后面给大家参考写法,并不使用xml配置
3.开始,首先这里不需要用xml配置,如果已经有xml配置了也没有问题
不用就是了
4.在springBoot应用中,首先:
E:\EclipseWorkSpace\smartxxx\src\main\resources\dbconfig.properties
dbconfig.properties
看内容,这里把多数据源的部分截出来了
#MySql
jdbc.mysql.url=jdbc:mysql://172.19.128.30:3306/smart_mine?useUnicode=true&characterEncoding=UTF-8
jdbc.mysql.username=root
jdbc.mysql.password=root
jdbc.mysql.driverClassName=com.mysql.jdbc.Driver
#PostgreSQL GIS MAP
jdbc.postgre.url=jdbc:postgresql://127.0.0.1:5432/postgres?useUnicode=true&characterEncoding=UTF-8
jdbc.postgre.username=postgres
jdbc.postgre.password=postgreserver
jdbc.postgre.driverClassName=org.postgresql.Driver
5.这样就可以了,然后开始配置:
6.使用的常量:
E:\EclipseWorkSpace\smartxxxx\src\main\java\cn\smartmine\core\ProjectConstant.java
package cn.smartmine.core;
/**
* 项目常量
*/
public final class ProjectConstant {
//以下多数据源配置的常量
public static final String MULTI_PROPERTY_FILE = "classpath:dbconfig.properties";// 多数据源配置文件
public static final String PRIMARY_DATASOURCE_MYSQL = "primary_datasource_mysql";//主数据源
public static final String SLAVE_DATASOURCE_POSTGRESQL="slave_datasource_postgresql";//从数据源配置
public static final String MYSQL_CONFIG_PROPERTY = "jdbc.mysql";//MySql 配置文件对应属性读取值
public static final String POSTGRESQL_CONFG_PROPERTY="jdbc.postgre";//PostGreSql 配置文件对应属性读取值
public static final String MYSQL_CONFIG_MAPPERS="classpath:cn/mapper/smartmine/tads/*Mapper.xml";//tads mappers
public static final String POSTGRESQL_CONFIG_MAPPERS="classpath:cn/mapper/smartmine/gis/*Mapper.xml";//gis mappers
public static final String MYSQL_CONFIG_BASEPACKAGE="cn.smartmine.tads.dao";//tads basepackage
public static final String POSTGRESQL_CONFIG_BASEPACKAGE="cn.smartmine.gis.dao";//gis basepackage
public static final String POSTGRESQL_SESSION_FACTORY="postgresql_sessionfactory";//postgresql session工厂
public static final String MYSQL_SESSION_FACTORY="mysql_sessionfactory";//mysql session工厂
public static final String POSTGRESQL_TRANSACTIONMANAGER="postgresql_transactionmanager";//postgresql 事物管理
public static final String MYSQL_TRANSACTIONMANAGER="mysql_transactionmanager";//mysql 事物管理
}
7.这里集成了mybatis所以:
E:\EclipseWorkSpace\smartmine\src\main\java\cn\smartmine\core\Mapper.java
通用的mapper
package cn.smartmine.core;
import tk.mybatis.mapper.common.BaseMapper;
import tk.mybatis.mapper.common.ConditionMapper;
import tk.mybatis.mapper.common.IdsMapper;
import tk.mybatis.mapper.common.special.InsertListMapper;
/**
* 定制版MyBatis Mapper插件接口,如需其他接口参考官方文档自行添加。
*/
public interface Mapper
extends
BaseMapper
ConditionMapper
IdsMapper
InsertListMapper
}
8.
然后开始配置类:
E:\EclipseWorkSpace\smartmine\src\main\java\cn\smartmine\configurer\AbstractDataSourceConfig.java
package cn.smartmine.configurer;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
/**
* 描述: 配置多数据源抽象类,各类数据源必须继承此抽象配置类
*
* @author lidewei
* @create 2018-08-10 08:52
**/
public abstract class AbstractDataSourceConfig {
private String driverClassName;
/**
* JDBC url of the database.
*/
private String url;
/**
* Login user of the database.
*/
private String username;
/**
* Login password of the database.
*/
private String password;
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;
}
protected DataSource getDatasource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(this.getUrl());
dataSource.setUsername(this.getUsername());
dataSource.setPassword(this.getPassword());
dataSource.setDriverClassName(this.getDriverClassName());
return dataSource;
}
}
---------------------------
9.E:\EclipseWorkSpace\smartmine\src\main\java\cn\smartmine\configurer\MySqlDataSourceConfig.java
package cn.smartmine.configurer;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import cn.smartmine.core.ProjectConstant;
/**
* 描述: Mybatis & Mapper & PageHelper 抽象配置父类
*
* @author lidewei
* @create 2018-08-10 08:52
**/
@Configuration
@PropertySource(ProjectConstant.MULTI_PROPERTY_FILE)
@ConfigurationProperties(ProjectConstant.MYSQL_CONFIG_PROPERTY)
public class MySqlDataSourceConfig extends AbstractDataSourceConfig {
@Bean(ProjectConstant.PRIMARY_DATASOURCE_MYSQL)
@Primary
public DataSource getMySqlDataSource() {
DataSource datasource = getDatasource();
return datasource;
}
}
-----------------------------
10.
E:\EclipseWorkSpace\smartmine\src\main\java\cn\smartmine\configurer\PostGreSqlDataSourceConfig.java
package cn.smartmine.configurer;
import javax.sql.DataSource;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//备用 事先检查配置文件中是否存在此属性
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import cn.smartmine.core.ProjectConstant;
/**
* 描述: PostGreSqlDataSourceConfig PostGreSql数据源配置实现类
*
* @author lidewei
* @create 2018-08-10 08:59
**/
@Configuration
@PropertySource(ProjectConstant.MULTI_PROPERTY_FILE)
@ConfigurationProperties(ProjectConstant.POSTGRESQL_CONFG_PROPERTY)
//备用 事先检查配置文件中是否存在此属性
//@ConditionalOnProperty(name = "jdbc.postgre.url", matchIfMissing = false)
public class PostGreSqlDataSourceConfig extends AbstractDataSourceConfig {
@Bean(ProjectConstant.SLAVE_DATASOURCE_POSTGRESQL)
public DataSource getPostGreSqlDataSource() {
DataSource datasource = getDatasource();
return datasource;
}
}
-------------------------------------
11.
E:\EclipseWorkSpace\smartmine\src\main\java\cn\smartmine\configurer\AbstractMyBatisConfigurer.java
package cn.smartmine.configurer;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import javax.sql.DataSource;
import java.util.Properties;
import static cn.smartmine.core.ProjectConstant.*;
/**
* 描述: Mybatis & Mapper & PageHelper 抽象配置父类
*
* @author lidewei
* @create 2018-08-10 08:52
**/
@Configuration
public abstract class AbstractMyBatisConfigurer {
/**
*
* @param dataSource 各类数据源
* @return 数据session工厂
* @throws Exception 异常
*/
public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
//配置分页插件,详情请查阅官方文档
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("pageSizeZero", "true");//分页尺寸为0时查询所有纪录不再执行分页
properties.setProperty("reasonable", "true");//页码<=0 查询第一页,页码>=总页数查询最后一页
properties.setProperty("supportMethodsArguments", "true");//支持通过 Mapper 接口参数来传递分页参数
pageHelper.setProperties(properties);
//添加插件
factory.setPlugins(new Interceptor[]{pageHelper});
return factory;
}
/**
* Mapper扫描用父方法
* @return Mybatis用MapperScannerConfigurer
*/
public MapperScannerConfigurer getMapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
Properties properties = new Properties();
properties.setProperty("mappers", MAPPER_INTERFACE_REFERENCE);
properties.setProperty("notEmpty", "false");//insert、update是否判断字符串类型!='' 即 test="str != null"表达式内是否追加 and str != ''
properties.setProperty("IDENTITY", "MYSQL");
mapperScannerConfigurer.setProperties(properties);
return mapperScannerConfigurer;
}
}
--------------------------------
12.
E:\EclipseWorkSpace\smartmine\src\main\java\cn\smartmine\configurer\MySqlMybatisConfigurer.java
package cn.smartmine.configurer;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
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 cn.smartmine.core.ProjectConstant;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
/**
* 描述: MySqlMybatisConfigurer MySql配置实现类
*
* @author lidewei
* @create 2018-08-10 08:59
**/
@Configuration
public class MySqlMybatisConfigurer extends AbstractMyBatisConfigurer {
/**
* 配置主数据源,这里使用MySql
* @param dataSource MySql主数据源
* @return 返回SqlSession工厂
* @throws Exception 异常
*/
@Bean(name=ProjectConstant.MYSQL_SESSION_FACTORY)
@Primary
public SqlSessionFactory sqlSessionFactoryBean(@Autowired @Qualifier(ProjectConstant.PRIMARY_DATASOURCE_MYSQL) DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = getSqlSessionFactoryBean(dataSource);
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(ProjectConstant.MYSQL_CONFIG_MAPPERS));
return sqlSessionFactoryBean.getObject();
}
/**
* MapperScannerConfigurer扫描类 MySql实现
* @return MapperScannerConfigurer扫描对象
*/
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = getMapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName(ProjectConstant.MYSQL_SESSION_FACTORY);
mapperScannerConfigurer.setBasePackage(ProjectConstant.MYSQL_CONFIG_BASEPACKAGE);
return mapperScannerConfigurer;
}
/**
*
* @param dataSource Mysql数据源
* @return mysql事物管理对象
*/
@Bean(name = ProjectConstant.MYSQL_TRANSACTIONMANAGER)
@Primary
public DataSourceTransactionManager mysqlTransactionManager(@Autowired @Qualifier(ProjectConstant.PRIMARY_DATASOURCE_MYSQL) DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
----------------------------
13.
E:\EclipseWorkSpace\smartmine\src\main\java\cn\smartmine\configurer\PostGreSqlMyBatisConfigurer.java
package cn.smartmine.configurer;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import cn.smartmine.core.ProjectConstant;
import javax.sql.DataSource;
/**
* 描述: PostGreSqlDataSourceConfig PostGreSql数据源配置实现类
*
* @author lidewei
* @create 2018-08-10 08:59
**/
@Configuration
@ConditionalOnBean(name = ProjectConstant.SLAVE_DATASOURCE_POSTGRESQL)
public class PostGreSqlMyBatisConfigurer extends AbstractMyBatisConfigurer {
/**
* MapperScannerConfigurer PostGreSql Mapper扫描类
*
* @return MapperScannerConfigurer返回Mapper扫描对象
*/
@Bean
public MapperScannerConfigurer PostGreSqlMapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = getMapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName(ProjectConstant.POSTGRESQL_SESSION_FACTORY);
mapperScannerConfigurer.setBasePackage(ProjectConstant.POSTGRESQL_CONFIG_BASEPACKAGE);
return mapperScannerConfigurer;
}
/**
* PostGreSql 事物管理类
* @param dataSource PostGreSql数据源
* @return
*/
@Bean(name = ProjectConstant.POSTGRESQL_TRANSACTIONMANAGER)
public DataSourceTransactionManager PostGreSqlTransactionManager(
@Autowired @Qualifier(ProjectConstant.SLAVE_DATASOURCE_POSTGRESQL) DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = ProjectConstant.POSTGRESQL_SESSION_FACTORY)
public SqlSessionFactory PostGreSqlSqlSessionFactoryBean(
@Autowired @Qualifier(ProjectConstant.SLAVE_DATASOURCE_POSTGRESQL) DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = getSqlSessionFactoryBean(dataSource);
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean
.setMapperLocations(resolver.getResources(ProjectConstant.POSTGRESQL_CONFIG_MAPPERS));
return sqlSessionFactoryBean.getObject();
}
}
------------------------------------------------------------------------------------