SpringBoot는 Quartz 작업 스케줄링을 사용하여 월별 데이터베이스 분할을 구현합니다.
사용 된 기술 : mybatis + springmvc + quartz + springboot
넌센스 말하지 말고 직접 붙여 넣기 만하면 돼 ~
SpringContextJobConfig.java
/**
* Copyright (C), 2019-2020,霓裳茶舞
* FileName: SpringContextJobConfig
* Author: SixJR.
* Date: 2020年11月13日 17:46
* Description: 用来解决JOB注入service为空的时候的处理
* History:
* <author> <time> <version> <desc>
*/
@Component
public class SpringContextJobConfig implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static Object getBean(String beanName){
return context.getBean(beanName);
}
public static Object getBeanByClazz(Class clazz){
return context.getBean(clazz);
}
public static String getMessage(String key) {
return context.getMessage(key, null, Locale.getDefault());
}
}
SubTableJob.java
/**
* Copyright (C), 2019-2020,霓裳茶舞
* FileName: MyJob
* Author: SixJR.
* Date: 2020年11月13日 16:00
* Description: 按月分库任务
* History:
* <author> <time> <version> <desc>
*/
public class SubTableJob implements Job {
@SneakyThrows
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("=============开始执行分库任务["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +"]=============");
//通过SpringContextJobConfig获取到service
SubTableService subOrderTableService= (SubTableService) SpringContextJobConfig.getBean("subOrderTableService");
//这里就是写你要分库的表名
subOrderTableService.SubTable("wechat_user_order");
subOrderTableService.SubTable("wechat_user_order_item");
subOrderTableService.SubTable("add_points_logs");
subOrderTableService.SubTable("keruyun_order_mapping");
subOrderTableService.SubTable("refund_logs");
System.out.println("=============分库任务执行完毕["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +"]=============");
}
}
SubTableService.java
package com.nscw.freshtask.service;
/**
* Copyright (C), 2019-2020,霓裳茶舞
* FileName: SubOrderTable
* Author: SixJR.
* Date: 2020年11月13日 16:45
* Description: 表分库
* History:
* <author> <time> <version> <desc>
*/
public interface SubTableService {
/**
* 分表
* @return
*/
int SubTable(String baseTable) throws Exception;
}
SubTableServiceImpl .java
/**
* Copyright (C), 2019-2020,霓裳茶舞
* FileName: SubOrderTableServiceImpl
* Author: SixJR.
* Date: 2020年11月13日 16:48
* Description: 表分库
* History:
* <author> <time> <version> <desc>
*/
@Service("subOrderTableService")
public class SubTableServiceImpl implements SubTableService {
@Autowired
private CopyTableMapper copyTableMapper;
/**
* 通用的分库方法
* @param baseTable 要分库的表
* @return
* @throws Exception
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int SubTable(String baseTable) throws Exception {
LocalDate date = LocalDate.now();
String tableName=baseTable+date.getYear()+""+(date.getMonthValue()-1);
try {
if(copyTableMapper.checkedIsExistTable(tableName)<1){
copyTableMapper.createNewTable(tableName,baseTable);//创建新表
copyTableMapper.copyTable(tableName,baseTable);//把数据copy到新表
copyTableMapper.truncateTable(baseTable);//清空旧表数据
System.out.println("表["+tableName+"]分库成功~");
}else{
System.out.println("表["+tableName+"]已经存在啦~");
}
} catch (Exception exception) {
Result result=new Result();
result.setCode(403);
result.setMessage("表"+tableName+"分库失败");
result.setData("失败原因:"+exception.getMessage());
throw new CustomException(result);
}
return 1;
}
}
CopyTableMapper.java
/**
* Copyright (C), 2019-2020,霓裳茶舞
* FileName: SubOrderTableMapper
* Author: SixJR.
* Date: 2020年11月13日 17:10
* Description: 分库映射层
* History:
* <author> <time> <version> <desc>
*/
public interface CopyTableMapper {
/**
* 检测表是否存在,没有则新建
* @param tableName
* @return
*/
@Select("SELECT COUNT(1) FROM information_schema.tables WHERE table_name = #{tableName}")
int checkedIsExistTable(@Param("tableName") String tableName);
/**
* 创建新表并拷贝旧表的表结构
* @param newTable
* @param oldTable
* @return
*/
@Update("CREATE TABLE `${newTable}` LIKE `${oldTable}` ")
int createNewTable(@Param("newTable") String newTable,@Param("oldTable") String oldTable);
/**
* 把旧数据复制到新表
* @param newTable
* @param oldTable
* @return
*/
@Insert("INSERT INTO `${newTable}` SELECT * FROM `${oldTable}` ")
int copyTable(@Param("newTable") String newTable,@Param("oldTable") String oldTable);
/**
* 清空数据库指定的表数据
* @param tableName
* @return
*/
@Update("truncate table ${tableName}")
int truncateTable(@Param("tableName") String tableName);
}
하위 라이브러리에 대한 완벽한 솔루션, 결과 그래프 :