ibatis|ibatis 数据增删改查一日一表的情况

假设数据库表为user_log_xxx(日期20220512),数据有id,name ,createTime

/**UserLog.java*/ import java.io.Serializable; import java.util.Date; public class User implements Serializable{ /**id*/ private Integer id; /**昵称*/ private String name; /**开始时间*/ private Date ceateTime; /** 开始时间 */ private Date startTime; /** 结束时间 */ private Date endTime; /** 开始时间字符串格式 */ private String sStartTime; /** 结束时间字符串格式 */ private String sEndTime; /** 表名后缀 */ private String tableNameSuffix = DateService.getTableSuffixByType("", DateService.DATE_BY_DAY); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; }public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; this.sStartTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTime); } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; this.sEndTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endTime); } public String getTableNameSuffix() { return tableNameSuffix; } public void setTableNameSuffix(String tableNameSuffix) { this.tableNameSuffix = tableNameSuffix; } public String getSStartTime() { return sStartTime; } public void setSStartTime(String startTime) { sStartTime = startTime; } public String getSEndTime() { return sEndTime; } public void setSEndTime(String endTime) { sEndTime = endTime; } }

DateService.java
public class DateService { /** 默认时间*/ private static java.util.Date defaultDate = null; public static final int DATE_BY_MONTH = 1; //按月分表 public static final int DATE_BY_WEEK = 2; //按周分表 public static final int DATE_BY_DAY = 3; //按天分表 }

UserLog.xml
create table if not exists user_log_$tableNameSuffix$ like user_log SELECT table_name FROM information_schema.TABLES WHERE table_name = #tableName#AND table_schema = "game_log"; INSERT INTO user_log_$tableNameSuffix$ (ID,Name,create_Time) VALUES (#id#,#name#,#createTime#) SELECT * FROM $tableName$ WHERE DATE_FORMAT(BET_TIME,'%Y%m%d') = #date# = #startTime# ]]> ORDER BY ID ASC LIMIT #start#,#onePageNum#

UserLogDAO.java
import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import IUserLogDAO ; import com.domain.UserLog; public class UserLogDAO extends SqlMapClientDaoSupport implements IUserLogDAO { private static String lastTableSuffixName = ""; public void createTable(String tableNameSuffix) { this.getSqlMapClientTemplate().insert("UserLog.createTable", tableNameSuffix); }/**创建数据*/ public void createUserLog(UserLog userLog) { if (lastTableSuffixName.equals(userLog.getTableNameSuffix()) == false) {// lastTableSuffixName = userLog.getTableNameSuffix(); createTable(userLog.getTableNameSuffix()); } userLog.setEndTime(new Date()); this.getSqlMapClientTemplate().insert("UserLog.insert", userLog); }public String checkTableIsExists(String tableName) { String trueName = (String)this.getSqlMapClientTemplate().queryForObject("UserLog.checkTableIsExists", tableName); return trueName; }/**分页查询 */ public List getUserLogByPage(String date,Date startTime, Date endTime,Integer start, Integer onePageNum) { String tableName = "user_log__" + date; if (this.checkTableIsExists(tableName) != null) { Map params = new HashMap(); params.put("tableName", tableName); params.put("date", date); params.put("start", start); params.put("onePageNum", onePageNum); params.put("startTime", startTime); params.put("endTime", endTime); return this.getSqlMapClientTemplate().queryForList("UserLog.getUserLogByPage", params); }else{ return null; } } }

IUserLogDAO.java
import java.util.Date; import java.util.List; import java.util.Map; import com.domain.UserLog; public interface IUserLogDAO { public void createTable(String tableNameSuffix); /**创建数据*/ public void createUserLog(UserLog userLog); public String checkTableIsExists(String tableName); /**分页查询 */ public List getUserLogByPage(String date,Date startTime, Date endTime,Integer start, Integer onePageNum); }

【ibatis|ibatis 数据增删改查一日一表的情况】

    推荐阅读