數據庫表歷史數據備份(定時任務)

■一、數據庫表數據備份

此處的數據庫表數據備份指的並不是整個數據庫的數據備份,而是針對某些數據量比較大的業務錶的歷史數據備份。數據庫中基礎表、用戶表、字典表等等表的數據量都是非常小的,只有個別業務表因業務操作頻繁數據量巨大。假如我們的業務表並不需要保存過長時間的業務數據時,可以將一定期限前的數據保存到歷史表中。這樣業務表中數據量就能夠保持到一個合理水平,從而提高該業務表的查詢效率。

備份數據邏輯圖:

■二、數據備份邏輯設計

表定義

接收報文(wdi_receive_msg_01)

No 物理名 邏輯名稱 數據類型 主鍵唯一 非空 默認值 註釋
1 id 物理主鍵 INT4 TRUE TRUE 序列值  
2 msg_text 報文內容 TEXT FALSE TRUE NULL 接收數據內容
3 msg_length 報文大小 INT4 FALSE TRUE NULL  
4 receive_servicecd 接收報文服務編碼 VARCHAR(100) FALSE FALSE NULL  
5 connect_add 接收地址 CHAR(30) FALSE TRUE NULL 接收地址
6 create_user 創建者 VARCHAR(50) FALSE TRUE NULL  
7 create_time 創建時間 TIMESTAMP FALSE TRUE NULL  
8 update_user 修改者 VARCHAR(50) FALSE TRUE NULL  
9 update_time 修改時間 TIMESTAMP FALSE TRUE NULL  

接收報文歷史(wdi_receive_msg_history_01)

No 物理名 邏輯名稱 數據類型 主鍵唯一 非空 默認值 註釋
1 id 物理主鍵 INT4 TRUE TRUE   業務表數據
2 msg_text 報文內容 TEXT FALSE TRUE NULL 業務表數據
3 msg_length 報文大小 INT4 FALSE TRUE NULL 業務表數據
4 receive_servicecd 接收報文服務編碼 VARCHAR(100) FALSE FALSE NULL 業務表數據
5 connect_add 接收地址 CHAR(30) FALSE TRUE NULL 業務表數據
6 create_user 創建者 VARCHAR(50) FALSE TRUE NULL 業務表數據
7 create_time 創建時間 TIMESTAMP FALSE TRUE NULL 業務表數據
8 update_user 修改者 VARCHAR(50) FALSE TRUE NULL 業務表數據
9 update_time 修改時間 TIMESTAMP FALSE TRUE NULL 業務表數據
10 backup_user 備份者 VARCHAR(50) FALSE TRUE NULL 備份時追加數據
11 backup_time 備份時間 TIMESTAMP FALSE TRUE NULL 備份時追加數據

歷史數據(wdi_history)

No 物理名 邏輯名稱 數據類型 主鍵唯一 非空 默認值 註釋
1 id 物理主鍵 INT4 TRUE TRUE 序列值  
2 table_name 業務表名稱 VARCHAR(100) FALSE TRUE NULL 處理業務表名
3 table_minid 表的最小ID值 INT4 FALSE FALSE NULL 處理日表id的最小值
4 table_maxid 表的最大ID值 INT4 FALSE FALSE NULL 處理日表id的最大值
5 opt_date 處理日期 DATE FALSE TRUE NULL  
6 backup_flag 備份標誌 CHAR(1) FALSE TRUE NULL Y:已備份/N:未備份
7 create_user 創建者 VARCHAR(50) FALSE TRUE NULL  
8 create_time 創建時間 TIMESTAMP FALSE TRUE NULL  
9 update_user 修改者 VARCHAR(50) FALSE TRUE NULL  
10 update_time 修改時間 TIMESTAMP FALSE TRUE NULL  

1.在數據備份時我們需要解決以下幾個問題。

1)由於我們的數據備份是通過每天的定時任務完成的,如果其中某些天程序沒有執行,那麼後續的定時任務要把前面沒有執行的數據處理掉。
2)業務表的主鍵ID是通過PostgreSQL的sequence生成的。這就存在一個問題當sequence達到最大值後,他將重新從1開始取值。這個特殊的數據變化也要在程序中處理掉。

2.處理邏輯設計

1)每天需要將業務表中最小id和最大id放入【歷史數據(wdi_history)】中。
  每天0點時獲取業務表的最大id,並將該id作爲昨天的最大id和今天的最小id更新到【歷史數據】表中。

插入歷史數據表數據有以下幾種情況:

2)備份數據只需要處理15天前的業務數據 最小id->最大id。

3)刪除歷史表中180天以前的數據。

■三、代碼實現

1)定時任務配置類

package infosky.wdis.task;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置定時任務
 */
@Configuration
public class QuartzConfig {

	/**
	 * 使用jobDetail包裝job
	 * @return
	 */
	@Bean
	public JobDetail backupJobDetail(){
		return JobBuilder.newJob(BackupJob.class).withIdentity("backupJob").storeDurably().build();
	}

	/**
	 * 把jobDetail註冊到Cron表達式的trigger上去
	 * @return
	 */
	@Bean
	public Trigger backupJobCronTrigger(){
		CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("1 0 0 * * ?");

		return TriggerBuilder.newTrigger()
				.forJob(backupJobDetail())
				.withIdentity("backupJobCronTrigger")
				.withSchedule(cronScheduleBuilder)
				.build();
	}
}

2)數據備份類

package infosky.wdis.task;
import infosky.wdis.common.WdiConstants;
import infosky.wdis.dao.WdiBackupDataMapper;
import infosky.wdis.dao.WdiHistoryMapper;
import infosky.wdis.dao.WdiReceiveMsgHistoryMapper;
import infosky.wdis.dao.WdiSendMsgHistoryMapper;
import infosky.wdis.entity.WdiBackupData;
import infosky.wdis.entity.WdiHistory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.transaction.annotation.Transactional;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 定時任務
 * 數據備份
 *
 * 2020/01/29
 */
@Transactional
public class BackupJob extends QuartzJobBean {

	private static final Logger logger = LoggerFactory.getLogger(BackupJob.class);
	@Autowired
	WdiHistoryMapper wdiHistoryMapper;
	@Autowired
	WdiReceiveMsgHistoryMapper wdiReceiveMsgHistoryMapper;
	@Autowired
	WdiSendMsgHistoryMapper wdiSendMsgHistoryMapper;
	@Autowired
	WdiBackupDataMapper wdiBackupDataMapper;

	/**
	 * 定時任務處理
	 * @param jobExecutionContext
	 * @throws JobExecutionException
	 */
	@Override
	protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		logger.info("########## BackupJob/executeInternal start ##########");

		// 1.將今天的最小id和昨天的最大id登錄到【wdi_history】表中
		saveIdByTablename("wdi_receive_msg_01");
		saveIdByTablename("wdi_receive_msg_02");
		saveIdByTablename("wdi_receive_msg_03");
		saveIdByTablename("wdi_receive_msg_04");
		saveIdByTablename("wdi_receive_msg_05");
		saveIdByTablename("wdi_receive_msg_06");
		saveIdByTablename("wdi_receive_msg_07");
		saveIdByTablename("wdi_receive_msg_08");
		saveIdByTablename("wdi_receive_msg_09");
		saveIdByTablename("wdi_receive_msg_10");
		saveIdByTablename("wdi_receive_msg_11");
		saveIdByTablename("wdi_receive_msg_12");
		saveIdByTablename("wdi_receive_msg_13");
		saveIdByTablename("wdi_receive_msg_14");
		saveIdByTablename("wdi_receive_msg_15");
		saveIdByTablename("wdi_receive_msg_16");
		saveIdByTablename("wdi_receive_msg_17");
		saveIdByTablename("wdi_receive_msg_18");
		saveIdByTablename("wdi_receive_msg_19");
		saveIdByTablename("wdi_receive_msg_20");
		saveIdByTablename("wdi_send_msg_01");
		saveIdByTablename("wdi_send_msg_02");
		saveIdByTablename("wdi_send_msg_03");
		saveIdByTablename("wdi_send_msg_04");
		saveIdByTablename("wdi_send_msg_05");
		saveIdByTablename("wdi_send_msg_06");
		saveIdByTablename("wdi_send_msg_07");
		saveIdByTablename("wdi_send_msg_08");
		saveIdByTablename("wdi_send_msg_09");
		saveIdByTablename("wdi_send_msg_10");
		saveIdByTablename("wdi_send_msg_11");
		saveIdByTablename("wdi_send_msg_12");
		saveIdByTablename("wdi_send_msg_13");
		saveIdByTablename("wdi_send_msg_14");
		saveIdByTablename("wdi_send_msg_15");
		saveIdByTablename("wdi_send_msg_16");
		saveIdByTablename("wdi_send_msg_17");
		saveIdByTablename("wdi_send_msg_18");
		saveIdByTablename("wdi_send_msg_19");
		saveIdByTablename("wdi_send_msg_20");

		// 2.將業務表數據備份到歷史表中
		backupData("wdi_receive_msg_01","wdi_receive_msg_history_01");
		backupData("wdi_receive_msg_02","wdi_receive_msg_history_02");
		backupData("wdi_receive_msg_03","wdi_receive_msg_history_03");
		backupData("wdi_receive_msg_04","wdi_receive_msg_history_04");
		backupData("wdi_receive_msg_05","wdi_receive_msg_history_05");
		backupData("wdi_receive_msg_06","wdi_receive_msg_history_06");
		backupData("wdi_receive_msg_07","wdi_receive_msg_history_07");
		backupData("wdi_receive_msg_08","wdi_receive_msg_history_08");
		backupData("wdi_receive_msg_09","wdi_receive_msg_history_09");
		backupData("wdi_receive_msg_10","wdi_receive_msg_history_10");
		backupData("wdi_receive_msg_11","wdi_receive_msg_history_11");
		backupData("wdi_receive_msg_12","wdi_receive_msg_history_12");
		backupData("wdi_receive_msg_13","wdi_receive_msg_history_13");
		backupData("wdi_receive_msg_14","wdi_receive_msg_history_14");
		backupData("wdi_receive_msg_15","wdi_receive_msg_history_15");
		backupData("wdi_receive_msg_16","wdi_receive_msg_history_16");
		backupData("wdi_receive_msg_17","wdi_receive_msg_history_17");
		backupData("wdi_receive_msg_18","wdi_receive_msg_history_18");
		backupData("wdi_receive_msg_19","wdi_receive_msg_history_19");
		backupData("wdi_receive_msg_20","wdi_receive_msg_history_20");
		backupData("wdi_send_msg_01","wdi_send_msg_history_01");
		backupData("wdi_send_msg_02","wdi_send_msg_history_02");
		backupData("wdi_send_msg_03","wdi_send_msg_history_03");
		backupData("wdi_send_msg_04","wdi_send_msg_history_04");
		backupData("wdi_send_msg_05","wdi_send_msg_history_05");
		backupData("wdi_send_msg_06","wdi_send_msg_history_06");
		backupData("wdi_send_msg_07","wdi_send_msg_history_07");
		backupData("wdi_send_msg_08","wdi_send_msg_history_08");
		backupData("wdi_send_msg_09","wdi_send_msg_history_09");
		backupData("wdi_send_msg_10","wdi_send_msg_history_10");
		backupData("wdi_send_msg_11","wdi_send_msg_history_11");
		backupData("wdi_send_msg_12","wdi_send_msg_history_12");
		backupData("wdi_send_msg_13","wdi_send_msg_history_13");
		backupData("wdi_send_msg_14","wdi_send_msg_history_14");
		backupData("wdi_send_msg_15","wdi_send_msg_history_15");
		backupData("wdi_send_msg_16","wdi_send_msg_history_16");
		backupData("wdi_send_msg_17","wdi_send_msg_history_17");
		backupData("wdi_send_msg_18","wdi_send_msg_history_18");
		backupData("wdi_send_msg_19","wdi_send_msg_history_19");
		backupData("wdi_send_msg_20","wdi_send_msg_history_20");

		// 3.刪除180天以上數據
		deleteBackupData("wdi_receive_msg_history_01");
		deleteBackupData("wdi_receive_msg_history_02");
		deleteBackupData("wdi_receive_msg_history_03");
		deleteBackupData("wdi_receive_msg_history_04");
		deleteBackupData("wdi_receive_msg_history_05");
		deleteBackupData("wdi_receive_msg_history_06");
		deleteBackupData("wdi_receive_msg_history_07");
		deleteBackupData("wdi_receive_msg_history_08");
		deleteBackupData("wdi_receive_msg_history_09");
		deleteBackupData("wdi_receive_msg_history_10");
		deleteBackupData("wdi_receive_msg_history_11");
		deleteBackupData("wdi_receive_msg_history_12");
		deleteBackupData("wdi_receive_msg_history_13");
		deleteBackupData("wdi_receive_msg_history_14");
		deleteBackupData("wdi_receive_msg_history_15");
		deleteBackupData("wdi_receive_msg_history_16");
		deleteBackupData("wdi_receive_msg_history_17");
		deleteBackupData("wdi_receive_msg_history_18");
		deleteBackupData("wdi_receive_msg_history_19");
		deleteBackupData("wdi_receive_msg_history_20");
		deleteBackupData("wdi_send_msg_history_01");
		deleteBackupData("wdi_send_msg_history_02");
		deleteBackupData("wdi_send_msg_history_03");
		deleteBackupData("wdi_send_msg_history_04");
		deleteBackupData("wdi_send_msg_history_05");
		deleteBackupData("wdi_send_msg_history_06");
		deleteBackupData("wdi_send_msg_history_07");
		deleteBackupData("wdi_send_msg_history_08");
		deleteBackupData("wdi_send_msg_history_09");
		deleteBackupData("wdi_send_msg_history_10");
		deleteBackupData("wdi_send_msg_history_11");
		deleteBackupData("wdi_send_msg_history_12");
		deleteBackupData("wdi_send_msg_history_13");
		deleteBackupData("wdi_send_msg_history_14");
		deleteBackupData("wdi_send_msg_history_15");
		deleteBackupData("wdi_send_msg_history_16");
		deleteBackupData("wdi_send_msg_history_17");
		deleteBackupData("wdi_send_msg_history_18");
		deleteBackupData("wdi_send_msg_history_19");
		deleteBackupData("wdi_send_msg_history_20");

		logger.info("########## BackupJob/executeInternal end ##########");
	}

	/**
	 * 獲取業務表表最大id,並將該值插入到wdi_history中
	 *
	 * @param tablename 業務表名
	 */
	public void saveIdByTablename(String tablename){
		// 1.獲取業務表中最大的id
		Integer maxId = wdiHistoryMapper.getMaxId(tablename);
		if(maxId == null){
			maxId = 0;
		}

		// 2.將1獲取的id保存到wdi_history中,前一天的maxid和今天的minid
		// 2.1.處理前一天的wdi_history數據
		WdiHistory wdiHistory = new WdiHistory();
		// 表名稱
		wdiHistory.setTableName(tablename);
		// 處理日期
		Calendar yesterday = Calendar.getInstance();
		yesterday.add(Calendar.DATE, -1);
		wdiHistory.setOptDate(yesterday.getTime());
		// 獲取當前日期前一天的wdi_history數據
		WdiHistory yesterdayWdiHistory = wdiHistoryMapper.getWdiHistory(wdiHistory);
		// 前一天wdi_history數據不存在
		if(yesterdayWdiHistory == null){
			// 如果不存在就插入
			WdiHistory insWdiHistory = new WdiHistory();
			// 表名稱
			insWdiHistory.setTableName(tablename);
			// 表的ID值
			insWdiHistory.setTableMaxid(maxId);
			// 處理日期
			insWdiHistory.setOptDate(yesterday.getTime());
			// 備份標誌
			insWdiHistory.setBackupFlag("N");
			// 創建者
			insWdiHistory.setCreateUser(WdiConstants.SYSTEM_TASK_BACKUPJOB);
			// 創建時間
			insWdiHistory.setCreateTime(new Date());
			// 修改者
			insWdiHistory.setUpdateUser(WdiConstants.SYSTEM_TASK_BACKUPJOB);
			// 修改時間
			insWdiHistory.setUpdateTime(new Date());

			wdiHistoryMapper.insertSelective(insWdiHistory);
		}else{
			// 如果存在就更新
			WdiHistory updWdiHistory = new WdiHistory();
			// 表名稱
			updWdiHistory.setTableName(tablename);
			// 處理日期
			updWdiHistory.setOptDate(yesterday.getTime());
			// 表的ID值
			updWdiHistory.setTableMaxid(maxId);
			// 修改者
			updWdiHistory.setUpdateUser(WdiConstants.SYSTEM_TASK_BACKUPJOB);
			// 修改時間
			updWdiHistory.setUpdateTime(new Date());

			wdiHistoryMapper.updateByTableNameOptTime(updWdiHistory);
		}

		// 2.2.插入當天wdi_history數據
		WdiHistory todayWdiHistory = new WdiHistory();
		// 表名稱
		todayWdiHistory.setTableName(tablename);
		// 表的ID值
		todayWdiHistory.setTableMinid(maxId);
		// 處理日期
		todayWdiHistory.setOptDate(new Date());
		// 備份標誌
		todayWdiHistory.setBackupFlag("N");
		// 創建者
		todayWdiHistory.setCreateUser(WdiConstants.SYSTEM_TASK_BACKUPJOB);
		// 創建時間
		todayWdiHistory.setCreateTime(new Date());
		// 修改者
		todayWdiHistory.setUpdateUser(WdiConstants.SYSTEM_TASK_BACKUPJOB);
		// 修改時間
		todayWdiHistory.setUpdateTime(new Date());

		wdiHistoryMapper.insertSelective(todayWdiHistory);
	}

	/**
	 * 備份數據
	 * @param tablename        業務表名
	 * @param historyTablename 歷史表名
	 */
	public void backupData(String tablename,String historyTablename){
		Calendar day = Calendar.getInstance();
		// 獲取15天以前的id值
		day.add(Calendar.DATE, -15);

		// 設定查詢條件
		WdiHistory wdiHistory = new WdiHistory();
		wdiHistory.setTableName(tablename);
		wdiHistory.setOptDate(day.getTime());
		List<WdiHistory> wdiHistoryBefore15daysOld = wdiHistoryMapper.getWdiHistoryLst(wdiHistory);

		// 如果15天以前的DiHistory存在,處理繼續
		if(wdiHistoryBefore15daysOld != null && wdiHistoryBefore15daysOld.size() > 0){
			// ====================================================================
			//  maxId | minId maxId| minId     |     maxId | minId maxId| minId
			// 本處理會出現以下幾種情況
			// 1.minId,maxId都存在,當maxId>minId時爲正常情況。當maxId<minId時,說明seq跨越了最大值,需要特殊處理
			// 2.minId存在,但是maxId不存在。這種情況說明定時處理在這一天執行了,插入了minId,但是第二天定時任務
			//   沒有執行,所以沒有插入maxId。這種情況不做處理。
			// 3.minId不存在,但是maxId存在。產生這種情況有2種可能。
			//   A:當定時任務第一次執行的時候,會插入前一天maxId,但是不會有minId。
			//   B:本定時任務隔了幾天後又開始執行。這個時候我們要找到這一次定時任務前面的執行結果。那個結果中只有minId,
			//     而沒有maxId,我們需要處理從哪個minId開始,到這次的maxId的數據。
			// ====================================================================
			for(int i=0;i<wdiHistoryBefore15daysOld.size();i++){
				WdiHistory tmpWdiHistory = wdiHistoryBefore15daysOld.get(i);
				WdiBackupData wbd = new WdiBackupData();
				wbd.setTbnm(tablename);
				wbd.setTbnm_history(historyTablename);
				Integer minId = tmpWdiHistory.getTableMinid();
				Integer maxId = tmpWdiHistory.getTableMaxid();
				// 判斷最大最小id是否存在
				if(minId != null && maxId != null){
					// 最大與最小id都存在
					// 判斷最大id與最小id的大小關係
					if(maxId >= minId){
						// 正常情況
						backupTable(wbd,minId,maxId);
					}else{
						// 跨越seq最大值情況
						// a:minId->seq最大值(2147483647)    b:0->maxId
						backupTable(wbd,minId,2147483647);
						backupTable(wbd,0,maxId);
					}
					// 更新wdi_history表
					tmpWdiHistory.setUpdateUser(WdiConstants.SYSTEM_TASK_BACKUPJOB);
					tmpWdiHistory.setUpdateTime(new Date());
					wdiHistoryMapper.updWdiHistoryByTableNameOptDate(tmpWdiHistory);
				}else if(minId != null && maxId == null){
					// 最小id存在,最大id不存在 ※無需處理
				}else if(minId == null && maxId != null){
					// 最小id不存在,最大id存在  ※2種情況。1.第一次插入數據,沒有最小id。2.程序執行中間有些天沒有有啓動。
					// 本條DiHistory是表中第一條數據
					if(i == 0){
						// 處理數據0->maxId
						backupTable(wbd,0,maxId);
						// 更新wdi_history表
						tmpWdiHistory.setUpdateUser(WdiConstants.SYSTEM_TASK_BACKUPJOB);
						tmpWdiHistory.setUpdateTime(new Date());
						wdiHistoryMapper.updWdiHistoryByTableNameOptDate(tmpWdiHistory);
					}else{
						// 獲取minId
						Integer beforeMinId = wdiHistoryBefore15daysOld.get(i-1).getTableMinid();
						// 比較beforeMinId 與 maxId大小關係
						if(maxId >= beforeMinId){
							// 正常情況
							backupTable(wbd,beforeMinId,maxId);
						}else{
							// 跨越seq最大值情況
							// a:beforeMinId->seq最大值(2147483647)    b:0->maxId
							backupTable(wbd,beforeMinId,2147483647);
							backupTable(wbd,0,maxId);
						}
						// 更新wdi_history表 當條數據
						tmpWdiHistory.setUpdateUser(WdiConstants.SYSTEM_TASK_BACKUPJOB);
						tmpWdiHistory.setUpdateTime(new Date());
						wdiHistoryMapper.updWdiHistoryByTableNameOptDate(tmpWdiHistory);

						// 更新wdi_history表 前一條數據(存在minId的數據)
						WdiHistory beforeTmpWdiHistory = wdiHistoryBefore15daysOld.get(i-1);
						beforeTmpWdiHistory.setUpdateUser(WdiConstants.SYSTEM_TASK_BACKUPJOB);
						beforeTmpWdiHistory.setUpdateTime(new Date());
						wdiHistoryMapper.updWdiHistoryByTableNameOptDate(beforeTmpWdiHistory);
					}
				}else{
					// 最小最大id都不存在 ※理論上不會出現這種狀況
				}
			}
		}
	}

	/**
	 * 備份業務表數據到歷史表,並刪除已備份業務數據
	 *
	 * @param wdiBackupData 需備份業務表及歷史表信息
	 * @param minId         備份的起始id
	 * @param maxId         備份的終止id
	 */
	public void backupTable(WdiBackupData wdiBackupData,Integer minId,Integer maxId){
		while(true){
			if(minId + 1000 <= maxId){
				wdiBackupData.setMinId(minId);
				wdiBackupData.setMaxId(minId + 1000);
				// 分批備份數據
				wdiBackupDataMapper.backUpData(wdiBackupData);
				// 刪除業務表已備份數據
				wdiBackupDataMapper.deleteData(wdiBackupData);
				minId = minId + 1000;
			}else{
				wdiBackupData.setMinId(minId);
				wdiBackupData.setMaxId(maxId);
				// 分批備份數據
				wdiBackupDataMapper.backUpData(wdiBackupData);
				// 刪除業務表已備份數據
				wdiBackupDataMapper.deleteData(wdiBackupData);
				break;
			}
		}
	}

	/**
	 * 刪除歷史表數據
	 * @param historyTablename 歷史表名
	 */
	public void deleteBackupData(String historyTablename){
		wdiHistoryMapper.deleteHistoryMsgByTableName(historyTablename);
	}
}

3)SQL  WdiHistoryMapper.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="infosky.wdis.dao.WdiHistoryMapper">
  <resultMap id="BaseResultMap" type="infosky.wdis.entity.WdiHistory">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="table_name" jdbcType="VARCHAR" property="tableName" />
    <result column="table_minid" jdbcType="INTEGER" property="tableMinid" />
    <result column="table_maxid" jdbcType="INTEGER" property="tableMaxid" />
    <result column="opt_date" jdbcType="DATE" property="optDate" />
    <result column="backup_flag" jdbcType="CHAR" property="backupFlag" />
    <result column="create_user" jdbcType="VARCHAR" property="createUser" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_user" jdbcType="VARCHAR" property="updateUser" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, table_name, table_minid, table_maxid, opt_date, backup_flag, create_user, create_time, 
    update_user, update_time
  </sql>
  <select id="selectByExample" parameterType="infosky.wdis.entity.WdiHistoryExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    'true' as QUERYID,
    <include refid="Base_Column_List" />
    from wdi_history
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from wdi_history
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from wdi_history
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="infosky.wdis.entity.WdiHistoryExample">
    delete from wdi_history
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="infosky.wdis.entity.WdiHistory">
    insert into wdi_history (id, table_name, table_minid, 
      table_maxid, opt_date, backup_flag, 
      create_user, create_time, update_user, 
      update_time)
    values (#{id,jdbcType=INTEGER}, #{tableName,jdbcType=VARCHAR}, #{tableMinid,jdbcType=INTEGER}, 
      #{tableMaxid,jdbcType=INTEGER}, #{optDate,jdbcType=DATE}, #{backupFlag,jdbcType=CHAR}, 
      #{createUser,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateUser,jdbcType=VARCHAR}, 
      #{updateTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="infosky.wdis.entity.WdiHistory">
    insert into wdi_history
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="tableName != null">
        table_name,
      </if>
      <if test="tableMinid != null">
        table_minid,
      </if>
      <if test="tableMaxid != null">
        table_maxid,
      </if>
      <if test="optDate != null">
        opt_date,
      </if>
      <if test="backupFlag != null">
        backup_flag,
      </if>
      <if test="createUser != null">
        create_user,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateUser != null">
        update_user,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="tableName != null">
        #{tableName,jdbcType=VARCHAR},
      </if>
      <if test="tableMinid != null">
        #{tableMinid,jdbcType=INTEGER},
      </if>
      <if test="tableMaxid != null">
        #{tableMaxid,jdbcType=INTEGER},
      </if>
      <if test="optDate != null">
        #{optDate,jdbcType=DATE},
      </if>
      <if test="backupFlag != null">
        #{backupFlag,jdbcType=CHAR},
      </if>
      <if test="createUser != null">
        #{createUser,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null">
        #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByExampleSelective" parameterType="map">
    update wdi_history
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.tableName != null">
        table_name = #{record.tableName,jdbcType=VARCHAR},
      </if>
      <if test="record.tableMinid != null">
        table_minid = #{record.tableMinid,jdbcType=INTEGER},
      </if>
      <if test="record.tableMaxid != null">
        table_maxid = #{record.tableMaxid,jdbcType=INTEGER},
      </if>
      <if test="record.optDate != null">
        opt_date = #{record.optDate,jdbcType=DATE},
      </if>
      <if test="record.backupFlag != null">
        backup_flag = #{record.backupFlag,jdbcType=CHAR},
      </if>
      <if test="record.createUser != null">
        create_user = #{record.createUser,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updateUser != null">
        update_user = #{record.updateUser,jdbcType=VARCHAR},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update wdi_history
    set id = #{record.id,jdbcType=INTEGER},
      table_name = #{record.tableName,jdbcType=VARCHAR},
      table_minid = #{record.tableMinid,jdbcType=INTEGER},
      table_maxid = #{record.tableMaxid,jdbcType=INTEGER},
      opt_date = #{record.optDate,jdbcType=DATE},
      backup_flag = #{record.backupFlag,jdbcType=CHAR},
      create_user = #{record.createUser,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      update_user = #{record.updateUser,jdbcType=VARCHAR},
      update_time = #{record.updateTime,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="infosky.wdis.entity.WdiHistory">
    update wdi_history
    <set>
      <if test="tableName != null">
        table_name = #{tableName,jdbcType=VARCHAR},
      </if>
      <if test="tableMinid != null">
        table_minid = #{tableMinid,jdbcType=INTEGER},
      </if>
      <if test="tableMaxid != null">
        table_maxid = #{tableMaxid,jdbcType=INTEGER},
      </if>
      <if test="optDate != null">
        opt_date = #{optDate,jdbcType=DATE},
      </if>
      <if test="backupFlag != null">
        backup_flag = #{backupFlag,jdbcType=CHAR},
      </if>
      <if test="createUser != null">
        create_user = #{createUser,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null">
        update_user = #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="infosky.wdis.entity.WdiHistory">
    update wdi_history
    set table_name = #{tableName,jdbcType=VARCHAR},
      table_minid = #{tableMinid,jdbcType=INTEGER},
      table_maxid = #{tableMaxid,jdbcType=INTEGER},
      opt_date = #{optDate,jdbcType=DATE},
      backup_flag = #{backupFlag,jdbcType=CHAR},
      create_user = #{createUser,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_user = #{updateUser,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <!-- =================================== 以下爲手動追加方法 =================================== -->
  <select id="getMaxId" parameterType="String" resultType="java.lang.Integer">
    select tb.id as maxId
      from ${tablename} tb
     order by create_time desc
     limit 1
  </select>

  <select id="getWdiHistory" parameterType="infosky.wdis.entity.WdiHistory" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" />
      from wdi_history
     where table_name = #{tableName}
       and opt_date =  #{optDate}
  </select>

  <select id="getWdiHistoryLst" parameterType="infosky.wdis.entity.WdiHistory" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" />
      from wdi_history
     where table_name = #{tableName}
       and opt_date <![CDATA[  <= ]]> #{optDate}
       and backup_flag = 'N'
     order by create_time asc
  </select>

  <update id="updateByTableNameOptTime" parameterType="infosky.wdis.entity.WdiHistory">
    update wdi_history
       set table_maxid=#{tableMaxid},
           update_user=#{updateUser},
           update_time=#{updateTime}
     where table_name=#{tableName}
       and opt_date=#{optDate}
  </update>

  <delete id="deleteHistoryMsgByTableName" parameterType="java.lang.String">
    delete from ${tablename}
     where update_time <![CDATA[ < ]]>(current_timestamp - interval'180 day')
  </delete>

  <update id="updWdiHistoryByTableNameOptDate" parameterType="infosky.wdis.entity.WdiHistory">
    update wdi_history
       set backup_flag='Y',
           update_user=#{updateUser},
           update_time=#{updateTime}
     where table_name=#{tableName}
       and opt_date=#{optDate}
  </update>

  <select id="getWdiHistoryBefore" parameterType="infosky.wdis.entity.WdiHistory" resultMap="BaseResultMap">
    select id,
           table_name,
           table_minid,
           table_maxid,
           opt_date,
           backup_flag,
           create_user,
           create_time,
           update_user,
           update_time
      from wdi_history
	  where id = (select max(id) from wdi_history where table_name = #{tableName} and opt_date <![CDATA[ < ]]> #{optDate})
  </select>

</mapper>

WdiReceiveMsgHistoryMapper.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="infosky.wdis.dao.WdiReceiveMsgHistoryMapper">
  <resultMap id="BaseResultMap" type="infosky.wdis.entity.WdiReceiveMsgHistory">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="msg_text" jdbcType="VARCHAR" property="msgText" />
    <result column="msg_length" jdbcType="INTEGER" property="msgLength" />
    <result column="receive_servicecd" jdbcType="VARCHAR" property="receiveServicecd" />
    <result column="connect_add" jdbcType="CHAR" property="connectAdd" />
    <result column="create_user" jdbcType="VARCHAR" property="createUser" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_user" jdbcType="VARCHAR" property="updateUser" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="backup_user" jdbcType="VARCHAR" property="backupUser" />
    <result column="backup_time" jdbcType="TIMESTAMP" property="backupTime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, msg_text, msg_length, receive_servicecd, connect_add, create_user, create_time, 
    update_user, update_time, backup_user, backup_time
  </sql>
  <select id="selectByExample" parameterType="infosky.wdis.entity.WdiReceiveMsgHistoryExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    'true' as QUERYID,
    <include refid="Base_Column_List" />
    from wdi_receive_msg_history_01
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from wdi_receive_msg_history_01
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from wdi_receive_msg_history_01
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="infosky.wdis.entity.WdiReceiveMsgHistoryExample">
    delete from wdi_receive_msg_history_01
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="infosky.wdis.entity.WdiReceiveMsgHistory">
    insert into wdi_receive_msg_history_01 (id, msg_text, msg_length, 
      receive_servicecd, connect_add, create_user, 
      create_time, update_user, update_time, 
      backup_user, backup_time)
    values (#{id,jdbcType=INTEGER}, #{msgText,jdbcType=VARCHAR}, #{msgLength,jdbcType=INTEGER}, 
      #{receiveServicecd,jdbcType=VARCHAR}, #{connectAdd,jdbcType=CHAR}, #{createUser,jdbcType=VARCHAR}, 
      #{createTime,jdbcType=TIMESTAMP}, #{updateUser,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP}, 
      #{backupUser,jdbcType=VARCHAR}, #{backupTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="infosky.wdis.entity.WdiReceiveMsgHistory">
    insert into wdi_receive_msg_history_01
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="msgText != null">
        msg_text,
      </if>
      <if test="msgLength != null">
        msg_length,
      </if>
      <if test="receiveServicecd != null">
        receive_servicecd,
      </if>
      <if test="connectAdd != null">
        connect_add,
      </if>
      <if test="createUser != null">
        create_user,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateUser != null">
        update_user,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="backupUser != null">
        backup_user,
      </if>
      <if test="backupTime != null">
        backup_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="msgText != null">
        #{msgText,jdbcType=VARCHAR},
      </if>
      <if test="msgLength != null">
        #{msgLength,jdbcType=INTEGER},
      </if>
      <if test="receiveServicecd != null">
        #{receiveServicecd,jdbcType=VARCHAR},
      </if>
      <if test="connectAdd != null">
        #{connectAdd,jdbcType=CHAR},
      </if>
      <if test="createUser != null">
        #{createUser,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null">
        #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="backupUser != null">
        #{backupUser,jdbcType=VARCHAR},
      </if>
      <if test="backupTime != null">
        #{backupTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByExampleSelective" parameterType="map">
    update wdi_receive_msg_history_01
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.msgText != null">
        msg_text = #{record.msgText,jdbcType=VARCHAR},
      </if>
      <if test="record.msgLength != null">
        msg_length = #{record.msgLength,jdbcType=INTEGER},
      </if>
      <if test="record.receiveServicecd != null">
        receive_servicecd = #{record.receiveServicecd,jdbcType=VARCHAR},
      </if>
      <if test="record.connectAdd != null">
        connect_add = #{record.connectAdd,jdbcType=CHAR},
      </if>
      <if test="record.createUser != null">
        create_user = #{record.createUser,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updateUser != null">
        update_user = #{record.updateUser,jdbcType=VARCHAR},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.backupUser != null">
        backup_user = #{record.backupUser,jdbcType=VARCHAR},
      </if>
      <if test="record.backupTime != null">
        backup_time = #{record.backupTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update wdi_receive_msg_history_01
    set id = #{record.id,jdbcType=INTEGER},
      msg_text = #{record.msgText,jdbcType=VARCHAR},
      msg_length = #{record.msgLength,jdbcType=INTEGER},
      receive_servicecd = #{record.receiveServicecd,jdbcType=VARCHAR},
      connect_add = #{record.connectAdd,jdbcType=CHAR},
      create_user = #{record.createUser,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      update_user = #{record.updateUser,jdbcType=VARCHAR},
      update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      backup_user = #{record.backupUser,jdbcType=VARCHAR},
      backup_time = #{record.backupTime,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="infosky.wdis.entity.WdiReceiveMsgHistory">
    update wdi_receive_msg_history_01
    <set>
      <if test="msgText != null">
        msg_text = #{msgText,jdbcType=VARCHAR},
      </if>
      <if test="msgLength != null">
        msg_length = #{msgLength,jdbcType=INTEGER},
      </if>
      <if test="receiveServicecd != null">
        receive_servicecd = #{receiveServicecd,jdbcType=VARCHAR},
      </if>
      <if test="connectAdd != null">
        connect_add = #{connectAdd,jdbcType=CHAR},
      </if>
      <if test="createUser != null">
        create_user = #{createUser,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null">
        update_user = #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="backupUser != null">
        backup_user = #{backupUser,jdbcType=VARCHAR},
      </if>
      <if test="backupTime != null">
        backup_time = #{backupTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="infosky.wdis.entity.WdiReceiveMsgHistory">
    update wdi_receive_msg_history_01
    set msg_text = #{msgText,jdbcType=VARCHAR},
      msg_length = #{msgLength,jdbcType=INTEGER},
      receive_servicecd = #{receiveServicecd,jdbcType=VARCHAR},
      connect_add = #{connectAdd,jdbcType=CHAR},
      create_user = #{createUser,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_user = #{updateUser,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      backup_user = #{backupUser,jdbcType=VARCHAR},
      backup_time = #{backupTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

WdiSendMsgHistoryMapper.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="infosky.wdis.dao.WdiSendMsgHistoryMapper">
  <resultMap id="BaseResultMap" type="infosky.wdis.entity.WdiSendMsgHistory">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="receive_msg_table_name" jdbcType="VARCHAR" property="receiveMsgTableName" />
    <result column="receive_msg_id" jdbcType="INTEGER" property="receiveMsgId" />
    <result column="receive_connect_add" jdbcType="CHAR" property="receiveConnectAdd" />
    <result column="msg_text" jdbcType="VARCHAR" property="msgText" />
    <result column="msg_length" jdbcType="INTEGER" property="msgLength" />
    <result column="receive_servicecd" jdbcType="VARCHAR" property="receiveServicecd" />
    <result column="send_connect_add" jdbcType="CHAR" property="sendConnectAdd" />
    <result column="operation_finish" jdbcType="CHAR" property="operationFinish" />
    <result column="create_user" jdbcType="VARCHAR" property="createUser" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_user" jdbcType="VARCHAR" property="updateUser" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="backup_user" jdbcType="VARCHAR" property="backupUser" />
    <result column="backup_time" jdbcType="TIMESTAMP" property="backupTime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, receive_msg_table_name, receive_msg_id, receive_connect_add, msg_text, msg_length, 
    receive_servicecd, send_connect_add, operation_finish, create_user, create_time, 
    update_user, update_time, backup_user, backup_time
  </sql>
  <select id="selectByExample" parameterType="infosky.wdis.entity.WdiSendMsgHistoryExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    'true' as QUERYID,
    <include refid="Base_Column_List" />
    from wdi_send_msg_history_01
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from wdi_send_msg_history_01
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from wdi_send_msg_history_01
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="infosky.wdis.entity.WdiSendMsgHistoryExample">
    delete from wdi_send_msg_history_01
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="infosky.wdis.entity.WdiSendMsgHistory">
    insert into wdi_send_msg_history_01 (id, receive_msg_table_name, receive_msg_id, 
      receive_connect_add, msg_text, msg_length, 
      receive_servicecd, send_connect_add, operation_finish, 
      create_user, create_time, update_user, 
      update_time, backup_user, backup_time
      )
    values (#{id,jdbcType=INTEGER}, #{receiveMsgTableName,jdbcType=VARCHAR}, #{receiveMsgId,jdbcType=INTEGER}, 
      #{receiveConnectAdd,jdbcType=CHAR}, #{msgText,jdbcType=VARCHAR}, #{msgLength,jdbcType=INTEGER}, 
      #{receiveServicecd,jdbcType=VARCHAR}, #{sendConnectAdd,jdbcType=CHAR}, #{operationFinish,jdbcType=CHAR}, 
      #{createUser,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateUser,jdbcType=VARCHAR}, 
      #{updateTime,jdbcType=TIMESTAMP}, #{backupUser,jdbcType=VARCHAR}, #{backupTime,jdbcType=TIMESTAMP}
      )
  </insert>
  <insert id="insertSelective" parameterType="infosky.wdis.entity.WdiSendMsgHistory">
    insert into wdi_send_msg_history_01
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="receiveMsgTableName != null">
        receive_msg_table_name,
      </if>
      <if test="receiveMsgId != null">
        receive_msg_id,
      </if>
      <if test="receiveConnectAdd != null">
        receive_connect_add,
      </if>
      <if test="msgText != null">
        msg_text,
      </if>
      <if test="msgLength != null">
        msg_length,
      </if>
      <if test="receiveServicecd != null">
        receive_servicecd,
      </if>
      <if test="sendConnectAdd != null">
        send_connect_add,
      </if>
      <if test="operationFinish != null">
        operation_finish,
      </if>
      <if test="createUser != null">
        create_user,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateUser != null">
        update_user,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="backupUser != null">
        backup_user,
      </if>
      <if test="backupTime != null">
        backup_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="receiveMsgTableName != null">
        #{receiveMsgTableName,jdbcType=VARCHAR},
      </if>
      <if test="receiveMsgId != null">
        #{receiveMsgId,jdbcType=INTEGER},
      </if>
      <if test="receiveConnectAdd != null">
        #{receiveConnectAdd,jdbcType=CHAR},
      </if>
      <if test="msgText != null">
        #{msgText,jdbcType=VARCHAR},
      </if>
      <if test="msgLength != null">
        #{msgLength,jdbcType=INTEGER},
      </if>
      <if test="receiveServicecd != null">
        #{receiveServicecd,jdbcType=VARCHAR},
      </if>
      <if test="sendConnectAdd != null">
        #{sendConnectAdd,jdbcType=CHAR},
      </if>
      <if test="operationFinish != null">
        #{operationFinish,jdbcType=CHAR},
      </if>
      <if test="createUser != null">
        #{createUser,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null">
        #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="backupUser != null">
        #{backupUser,jdbcType=VARCHAR},
      </if>
      <if test="backupTime != null">
        #{backupTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByExampleSelective" parameterType="map">
    update wdi_send_msg_history_01
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.receiveMsgTableName != null">
        receive_msg_table_name = #{record.receiveMsgTableName,jdbcType=VARCHAR},
      </if>
      <if test="record.receiveMsgId != null">
        receive_msg_id = #{record.receiveMsgId,jdbcType=INTEGER},
      </if>
      <if test="record.receiveConnectAdd != null">
        receive_connect_add = #{record.receiveConnectAdd,jdbcType=CHAR},
      </if>
      <if test="record.msgText != null">
        msg_text = #{record.msgText,jdbcType=VARCHAR},
      </if>
      <if test="record.msgLength != null">
        msg_length = #{record.msgLength,jdbcType=INTEGER},
      </if>
      <if test="record.receiveServicecd != null">
        receive_servicecd = #{record.receiveServicecd,jdbcType=VARCHAR},
      </if>
      <if test="record.sendConnectAdd != null">
        send_connect_add = #{record.sendConnectAdd,jdbcType=CHAR},
      </if>
      <if test="record.operationFinish != null">
        operation_finish = #{record.operationFinish,jdbcType=CHAR},
      </if>
      <if test="record.createUser != null">
        create_user = #{record.createUser,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updateUser != null">
        update_user = #{record.updateUser,jdbcType=VARCHAR},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.backupUser != null">
        backup_user = #{record.backupUser,jdbcType=VARCHAR},
      </if>
      <if test="record.backupTime != null">
        backup_time = #{record.backupTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update wdi_send_msg_history_01
    set id = #{record.id,jdbcType=INTEGER},
      receive_msg_table_name = #{record.receiveMsgTableName,jdbcType=VARCHAR},
      receive_msg_id = #{record.receiveMsgId,jdbcType=INTEGER},
      receive_connect_add = #{record.receiveConnectAdd,jdbcType=CHAR},
      msg_text = #{record.msgText,jdbcType=VARCHAR},
      msg_length = #{record.msgLength,jdbcType=INTEGER},
      receive_servicecd = #{record.receiveServicecd,jdbcType=VARCHAR},
      send_connect_add = #{record.sendConnectAdd,jdbcType=CHAR},
      operation_finish = #{record.operationFinish,jdbcType=CHAR},
      create_user = #{record.createUser,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      update_user = #{record.updateUser,jdbcType=VARCHAR},
      update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      backup_user = #{record.backupUser,jdbcType=VARCHAR},
      backup_time = #{record.backupTime,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="infosky.wdis.entity.WdiSendMsgHistory">
    update wdi_send_msg_history_01
    <set>
      <if test="receiveMsgTableName != null">
        receive_msg_table_name = #{receiveMsgTableName,jdbcType=VARCHAR},
      </if>
      <if test="receiveMsgId != null">
        receive_msg_id = #{receiveMsgId,jdbcType=INTEGER},
      </if>
      <if test="receiveConnectAdd != null">
        receive_connect_add = #{receiveConnectAdd,jdbcType=CHAR},
      </if>
      <if test="msgText != null">
        msg_text = #{msgText,jdbcType=VARCHAR},
      </if>
      <if test="msgLength != null">
        msg_length = #{msgLength,jdbcType=INTEGER},
      </if>
      <if test="receiveServicecd != null">
        receive_servicecd = #{receiveServicecd,jdbcType=VARCHAR},
      </if>
      <if test="sendConnectAdd != null">
        send_connect_add = #{sendConnectAdd,jdbcType=CHAR},
      </if>
      <if test="operationFinish != null">
        operation_finish = #{operationFinish,jdbcType=CHAR},
      </if>
      <if test="createUser != null">
        create_user = #{createUser,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null">
        update_user = #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="backupUser != null">
        backup_user = #{backupUser,jdbcType=VARCHAR},
      </if>
      <if test="backupTime != null">
        backup_time = #{backupTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="infosky.wdis.entity.WdiSendMsgHistory">
    update wdi_send_msg_history_01
    set receive_msg_table_name = #{receiveMsgTableName,jdbcType=VARCHAR},
      receive_msg_id = #{receiveMsgId,jdbcType=INTEGER},
      receive_connect_add = #{receiveConnectAdd,jdbcType=CHAR},
      msg_text = #{msgText,jdbcType=VARCHAR},
      msg_length = #{msgLength,jdbcType=INTEGER},
      receive_servicecd = #{receiveServicecd,jdbcType=VARCHAR},
      send_connect_add = #{sendConnectAdd,jdbcType=CHAR},
      operation_finish = #{operationFinish,jdbcType=CHAR},
      create_user = #{createUser,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_user = #{updateUser,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      backup_user = #{backupUser,jdbcType=VARCHAR},
      backup_time = #{backupTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

WdiBackupDataMapper.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="infosky.wdis.dao.WdiBackupDataMapper">

    <resultMap id="DiBackupData" type="infosky.wdis.entity.WdiBackupData">
        <result column="minId" property="minId"/>
        <result column="maxId" property="maxId"/>
    </resultMap>

    <select id="getMinMaxID" parameterType="infosky.wdis.entity.WdiBackupData" resultMap="DiBackupData">
        select min(tb.id) as minId,
               max(tb.id) as maxId
          from ${tbnm} tb
         where tb.update_time <![CDATA[ < ]]> (current_timestamp - interval '15 day')
    </select>

    <insert id="backUpData"  parameterType="infosky.wdis.entity.WdiBackupData">
        insert into ${tbnm_history} 
        (select *,
                'SYSTEM_TASK_BACKUPJOB' as backup_user,
                current_timestamp as backup_time 
           from ${tbnm} 
          where id <![CDATA[ >= ]]> #{minId} 
            and id <![CDATA[ <= ]]> #{maxId})
    </insert>
    
    <delete id="deleteData" parameterType="infosky.wdis.entity.WdiBackupData">
        delete from ${tbnm} tb
         where id <![CDATA[ >= ]]> #{minId} 
           and id <![CDATA[ <= ]]> #{maxId}
    </delete>
    
    <select id="getMaxId" parameterType="String" resultType="java.lang.Integer">
        select max(tb.id) as maxId
          from ${tableName} tb
    </select>
</mapper>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章