目录
- 1. 解析配置文件初始化数据源
- 2. 定义数据源枚举类型
- 3. TheadLocal保存数据源类型
- 4. 自定义sqlSessionProxy
- 5. 自定义路由
- 6. 定义切面,dao层定义切面
- 7. 最后在写库增加事务管理
- 8. 在配置文件中增加数据源配置
配置文件是通过springcloudconfig远程分布式配置。采用阿里Druid数据源。并支持一主多从的读写分离。分页组件通过拦截器拦截带有page后缀的方法名,动态的设置total总数。
1. 解析配置文件初始化数据源
@Configurationpublic class DataSourceConfiguration {/*** 数据源类型*/@Value("${spring.datasource.type}")private Class extends DataSource> dataSourceType;
/*** 主数据源配置** @return*/@Bean(name = "masterDataSource", destroyMethod = "close")@Primary@ConfigurationProperties(prefix = "spring.datasource")public DataSource masterDataSource() {DataSource source = DataSourceBuilder.create().type(dataSourceType).build();
return source;
}/*** 从数据源配置** @return*/@Bean(name = "slaveDataSource0")@ConfigurationProperties(prefix = "spring.slave0")public DataSource slaveDataSource0() {DataSource source = DataSourceBuilder.create().type(dataSourceType).build();
return source;
}/*** 从数据源集合** @return*/@Bean(name = "slaveDataSources")public List slaveDataSources() {List slaveDataSources = new ArrayList();
slaveDataSources.add(slaveDataSource0());
return slaveDataSources;
}}
2. 定义数据源枚举类型
public enum DataSourceType {master("master", "master"), slave("slave", "slave");
private String type;
private String name;
DataSourceType(String type, String name) {this.type = type;
this.name = name;
}public String getType() {return type;
}public void setType(String type) {this.type = type;
}public String getName() {return name;
}public void setName(String name) {this.name = name;
}}
3. TheadLocal保存数据源类型
public class DataSourceContextHolder {private static final ThreadLocal local = new ThreadLocal();
public static ThreadLocal getLocal() {return local;
}public static void slave() {local.set(DataSourceType.slave.getType());
}public static void master() {local.set(DataSourceType.master.getType());
}public static String getJdbcType() {return local.get();
}public static void clearDataSource(){local.remove();
}}
4. 自定义sqlSessionProxy
并将数据源填充到DataSourceRoute
@Configuration@ConditionalOnClass({EnableTransactionManagement.class})@Import({DataSourceConfiguration.class})public class DataSourceSqlSessionFactory {private Logger logger = Logger.getLogger(DataSourceSqlSessionFactory.class);
@Value("${spring.datasource.type}")private Class extends DataSource> dataSourceType;
@Value("${mybatis.mapper-locations}")private String mapperLocations;
@Value("${mybatis.type-aliases-package}")private String aliasesPackage;
@Value("${slave.datasource.number}")private int dataSourceNumber;
@Resource(name = "masterDataSource")private DataSource masterDataSource;
@Resource(name = "slaveDataSources")private List slaveDataSources;
@Bean@ConditionalOnMissingBeanpublic SqlSessionFactory sqlSessionFactory() throws Exception {logger.info("======================= init sqlSessionFactory");
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(roundRobinDataSourceProxy());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocations));
sqlSessionFactoryBean.setTypeAliasesPackage(aliasesPackage);
sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return sqlSessionFactoryBean.getObject();
}@Bean(name = "roundRobinDataSourceProxy")public AbstractRoutingDataSource roundRobinDataSourceProxy() {logger.info("======================= init robinDataSourceProxy");
DataSourceRoute proxy = new DataSourceRoute(dataSourceNumber);
Map