由于每天生成的日志文件比较多,可以分表保存,每个月自动生成下个月的表(使用spring定时器,案例用的是xml配置,现在都是用注解配置)。

考虑到上线时的意外,在项目初始化的时候就生成下2个月的表,使用监听器。里面复制表的语句有判断是否表存在。

动态生成表名的方法可以参考一下。这样log的查询和插入语句就要动态更换表名,这里就不写了。

生成表的语句是用hibernate

/**
 * 使用spring集成的石英调度,动态生成日志表
 */
public class GenerateLogsTableTask extends QuartzJobBean {

    //
    private LogService logService ;
    
    public void setLogService(LogService logService) {
        this.logService = logService;
    }

    /**
     * 执行任务
     */
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        String tableName =  LogUtil.generateLogTableName(1);
        String sql = "create table if not exists " + tableName + " like logs";
        logService.executeSQL(sql);
        System.out.println(tableName + " 生成了! " );
        
        tableName =  LogUtil.generateLogTableName(2);
        sql = "create table if not exists " + tableName + " like logs";
        logService.executeSQL(sql);
        System.out.println(tableName + " 生成了! " );
    }
}
GenerateLogsTableTask

相关文章:

  • 2021-09-17
  • 2022-02-20
  • 2021-10-16
  • 2021-06-11
  • 2021-10-23
  • 2021-07-07
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2022-02-08
  • 2021-10-08
  • 2021-07-10
  • 2021-07-24
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
相关资源
相似解决方案