Spring boot+MyBatis+Sharding jdbc配置
阿新 • • 發佈:2019-01-04
為便於在工作中進行單機多服務部署及簡化開發配置,將現有系統遷移至Spring boot。
Maven配置
Spring boot相關依賴:web、jdbc、aop相關依賴包(由於專案中應用了redis,所以一併移植了)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId >spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId >spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--sharding-jdbc -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>${sharding-jdbc.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
MyBatis配置相關
mapper配置檔案存放於resources目錄:
resources/
mapper/
xxx.xml
xxx.xml
...
整合Spring boot 配置檔案:MyBatisConfig,MyBatisMapperScannerConfig。
參考:abel MyBatis整合
@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {
@Autowired
XbDataSource xbDataSource;//整合sharding-jdbc分表資料來源
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean() {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(xbDataSource.getDataSource());
bean.setTypeAliasesPackage("com.xiaobai.crawler.model");//實體對應包路徑
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(xbDataSource.getDataSource());
}
}
@Configuration
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.xiaobai.crawler.mapper");//mapper類路徑
return mapperScannerConfigurer;
}
}
sharding-jdbc分表配置
資料來源配置及分表規則定義(只對tb_p表按照item_id進行分表13張,tb_item不分表)
@Component
public class XbDataSource {
@Autowired
DataSource dataSource;
DataSource shardingDataSource;
@PostConstruct
public void init() {
HashMap<String, DataSource> map = new HashMap<>();
map.put("dataSource", dataSource);
DataSourceRule dataSourceRule = new DataSourceRule(map);
List<TableRule> tableRuleList = new ArrayList<>();
List<String> pList = new ArrayList<>();
for (int i = 1; i < 14; i++) {
pList.add("tb_p_" + i);
}
//tb_p邏輯表名,pList實際所有的分表
tableRuleList.add(new TableRule.TableRuleBuilder("tb_p")
.actualTables(pList)
.dataSourceRule(dataSourceRule)
.tableShardingStrategy(new TableShardingStrategy("item_id", new ProgramShardingAlgorithm())).build());
tableRuleList.add(new TableRule.TableRuleBuilder("tb_item")
.actualTables(Lists.newArrayList("tb_item"))
.dataSourceRule(dataSourceRule).build());
ShardingRule shardingRule = ShardingRule.builder()
.dataSourceRule(dataSourceRule)
.tableRules(tableRuleList)
.build();
shardingDataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
}
public DataSource getDataSource() {
return shardingDataSource;
}
@Component
public final class ProgramShardingAlgorithm implements SingleKeyTableShardingAlgorithm<Integer> {
/**
* equals比較條件
* @param availableTargetNames
* @param shardingValue
* @return
*/
@Override
public String doEqualSharding(final Collection<String> availableTargetNames, final ShardingValue<Integer> shardingValue) {
for (String each : availableTargetNames) {
if (each.endsWith(shardingValue.getValue() %14+ "")) {
return each;
}
}
throw new UnsupportedOperationException();
}
/**
* in比較條件
* @param availableTargetNames
* @param shardingValue
* @return
*/
@Override
public Collection<String> doInSharding(final Collection<String> availableTargetNames, final ShardingValue<Integer> shardingValue) {
Collection<String> result = new LinkedHashSet<String>(availableTargetNames.size());
Collection<Integer> values = shardingValue.getValues();
for (Integer value : values) {
for (String tableNames : availableTargetNames) {
if (tableNames.endsWith(value % 14 + "")) {
result.add(tableNames);
}
}
}
return result;
}
/**
* between比較條件
* @param availableTargetNames
* @param shardingValue
* @return
*/
@Override
public Collection<String> doBetweenSharding(final Collection<String> availableTargetNames, final ShardingValue<Integer> shardingValue) {
Collection<String> result = new LinkedHashSet<String>(availableTargetNames.size());
Range<Integer> range = shardingValue.getValueRange();
for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
for (String each : availableTargetNames) {
if (each.endsWith(i % 14+ "")) {
result.add(each);
}
}
}
return result;
}