为便于在工作中进行单机多服务部署及简化开发配置,将现有系统迁移至Spring boot。
Spring boot相关依赖:web、jdbc、aop相关依赖包(由于项目中应用了redis,所以一并移植了)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.4.2.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.dangdanggroupId>
<artifactId>sharding-jdbc-coreartifactId>
<version>${sharding-jdbc.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>${mybatis.version}version>
dependency>
<dependency>
<groupId>javax.persistencegroupId>
<artifactId>persistence-apiartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>${mybatis-spring.version}version>
dependency>
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;
}
}
数据源配置及分表规则定义(只对tb_p表按照item_id进行分表13张,tb_item不分表)
@Component
public class XbDataSource {
@Autowired
DataSource dataSource;
DataSource shardingDataSource;
@PostConstruct
public void init() {
HashMap map = new HashMap<>();
map.put("dataSource", dataSource);
DataSourceRule dataSourceRule = new DataSourceRule(map);
List tableRuleList = new ArrayList<>();
List 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 availableTargetNames, final ShardingValue 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 doInSharding(final Collection availableTargetNames, final ShardingValue shardingValue) {
Collection result = new LinkedHashSet(availableTargetNames.size());
Collection 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 doBetweenSharding(final Collection availableTargetNames, final ShardingValue shardingValue) {
Collection result = new LinkedHashSet(availableTargetNames.size());
Range 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;
}