|
| 1 | +package cn.iwuliao.ds.core; |
| 2 | + |
| 3 | + |
| 4 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 5 | +import org.mybatis.spring.SqlSessionFactoryBean; |
| 6 | +import org.mybatis.spring.mapper.MapperScannerConfigurer; |
| 7 | +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| 8 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 9 | +import org.springframework.transaction.PlatformTransactionManager; |
| 10 | + |
| 11 | +import javax.sql.DataSource; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.Properties; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author tangyu |
| 19 | + * @since 2019-04-06 03:13 |
| 20 | + */ |
| 21 | +public class DatasourceUtil { |
| 22 | + |
| 23 | + /** |
| 24 | + * 获取 SqlsessionFactory 对象 |
| 25 | + * |
| 26 | + * @param dataSource |
| 27 | + * @param location |
| 28 | + * @param handlersPackage |
| 29 | + * @return |
| 30 | + * @throws Exception |
| 31 | + */ |
| 32 | + public static SqlSessionFactory getSqlSessionFactory(DataSource dataSource, String location, String handlersPackage) throws Exception { |
| 33 | + SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); |
| 34 | + factory.setDataSource(dataSource); |
| 35 | + factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(location)); |
| 36 | + if (Objects.nonNull(handlersPackage)) { |
| 37 | + factory.setTypeHandlersPackage(handlersPackage); |
| 38 | + } |
| 39 | + return factory.getObject(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * 将Mapper 文件 注入到IOC 容器 |
| 44 | + * |
| 45 | + * @param baseRestSlaveSqlSessionFactory |
| 46 | + * @param basePackage |
| 47 | + * @return |
| 48 | + */ |
| 49 | + public static MapperScannerConfigurer getMapperScannerConfigurer(String baseRestSlaveSqlSessionFactory, String basePackage) { |
| 50 | + MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); |
| 51 | + mapperScannerConfigurer.setBasePackage(basePackage); |
| 52 | + //使用实体类名/属性名作为表名/字段名 |
| 53 | + return mapperScannerConfigurer; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + /** |
| 58 | + * 动态获取数据源 |
| 59 | + * |
| 60 | + * @param master |
| 61 | + * @param slave |
| 62 | + * @return |
| 63 | + */ |
| 64 | + public static MasterSlaveRoutingDataSource getRoutingDataSource(DataSource master, DataSource slave) { |
| 65 | + |
| 66 | + MasterSlaveRoutingDataSource proxy = new MasterSlaveRoutingDataSource(); |
| 67 | + |
| 68 | + Map<Object, Object> targetDataResources = new HashMap<>(); |
| 69 | + targetDataResources.put(DbContextHolder.DbType.MASTER, master); |
| 70 | + targetDataResources.put(DbContextHolder.DbType.SLAVE, slave); |
| 71 | + |
| 72 | + proxy.setDefaultTargetDataSource(master); |
| 73 | + proxy.setTargetDataSources(targetDataResources); |
| 74 | + |
| 75 | + proxy.afterPropertiesSet(); |
| 76 | + return proxy; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * 事物管理 |
| 81 | + * |
| 82 | + * @param nowwarningDataSource |
| 83 | + * @return |
| 84 | + */ |
| 85 | + public static PlatformTransactionManager getransactionManager(DataSource nowwarningDataSource) { |
| 86 | + |
| 87 | + return new DataSourceTransactionManager(nowwarningDataSource); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments