下面是一个Spring项目实现日期分表(按天表)的简单示例。这个示例采用了MyBatis
作为ORM框架和MySQL
数据库。
Controller
import com.alibaba.fastjson.JSONObject;
import your.package.LogService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class LogController {
@Autowired
private LogService logService;
@PostMapping("/log/save")
@ApiOperation(value = "保存日志信息")
public String saveLog(@RequestBody JSONObject jsonObject) {
logService.saveLog(jsonObject);
return "ok";
}
@PostMapping("/log/createTable")
@ApiOperation(value = "创建日志信息表")
public String createTable() {
logService.createdLogTable();
return "ok";
}
}
Service
import cn.hutool.core.date.format.FastDateFormat;
import com.alibaba.fastjson.JSONObject;
import your.package.LogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
@Slf4j
public class LogService {
@Autowired
private LogMapper logMapper;
public void saveLog(JSONObject jsonObject) {
try {
Date now = new Date();
String dateStr = FastDateFormat.getInstance("yyyyMMdd").format(now);//20210728
String data = (String) jsonObject.get("data");
logMapper.insert(dateStr, data);
} catch (Exception e) {
log.error("api log 写入失败:{}", e.getMessage());
}
}
@Scheduled(cron = "0 0 0 * * ?")
public void createdLogTable() {
Date now = new Date();
String dateStr = FastDateFormat.getInstance("yyyyMMdd").format(now);//20210728
logMapper.createdLogTable(dateStr);
}
}
Mapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Repository;
@Repository
public interface LogMapper extends BaseMapper<T> {
/**
* 添加记录
* @param date
* @param data
* @return
*/
int insert(@Param("date") String date, @Param("data") String data);
void createdLogTable(@Param("date") String date);
}
Xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="your.package.LogMapper">
<insert id="insert">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into t_log_${date}(data) values (#{data})
</insert>
<update id="createdLogTable">
CREATE TABLE if not exists t_log_${date} (
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
`data` varchar(128) NOT NULL DEFAULT '' COMMENT '日志',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='日志记录表'
</update>
</mapper>
总结
请注意,实际项目中分表的逻辑可能更复杂。根据业务情况和数据规模,可能还需要考虑到事务一致性、查询优化、表的创建和维护策略等问题。通常分表方案还会搭配使用数据中间件,例如ShardingSphere等,以提供更灵活且自动化的分表能力。
这个例子仅提供了按天分表保存日志的一种实现方式,提供作为概念演示。在真实的生产环境中,还需要进一步考量性能、事务和拓展性等问题。