詳解MyBatisPlus邏輯刪除與唯一索引沖突問題
在開發(fā)中,我們經(jīng)常會(huì)有邏輯刪除和唯一索引同時(shí)使用的情況。但當(dāng)使用mybatis plus時(shí),如果同時(shí)使用邏輯刪除和唯一索引,會(huì)報(bào)數(shù)據(jù)重復(fù)Duplicate entry的問題。
舉個(gè)例子:
原來數(shù)據(jù)庫結(jié)構(gòu):
這里location_id是唯一索引
CREATE TABLE `eam_location` ( `id` int(11) NOT NULL AUTO_INCREMENT, `location_id` varchar(50) UNIQUE NOT NULL COMMENT ’位置代碼’, `location_level` tinyint(1) NOT NULL COMMENT ’位置級(jí)別’, `location_name` varchar(50) NOT NULL COMMENT ’位置名稱’, `parent_location_id` varchar(50) COMMENT ’上級(jí)位置代碼’, `delete_flag` tinyint(1) DEFAULT 0 COMMENT ’軟刪除’, `version` int(11) DEFAULT 1 COMMENT ’樂觀鎖’, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
這里在刪除添加某一條數(shù)據(jù)后,delete_flag變成0,當(dāng)刪除后delete_flag會(huì)變成1,再次添加相同的數(shù)據(jù)時(shí),由于數(shù)據(jù)庫檢測不到原來數(shù)據(jù),會(huì)報(bào)數(shù)據(jù)重復(fù)Duplicate entry的問題
解決辦法:參考邏輯刪除與唯一約束的需求沖突
SQL數(shù)據(jù)結(jié)構(gòu),將delete_flag用時(shí)間戳進(jìn)行表示,唯一索引變成了聯(lián)合唯一索引 UNIQUE KEY unique_location_delete_flag(location_id, delete_flag) ,當(dāng)添加一條數(shù)據(jù)時(shí),delete_flag變成null,當(dāng)刪除數(shù)據(jù)時(shí),delete_flag變成刪除時(shí)的一個(gè)時(shí)間戳。再次添加相同數(shù)據(jù)時(shí),由于添加的數(shù)據(jù)是聯(lián)合唯一索引unique_location_delete_flag ,delete_flag為null,不會(huì)產(chǎn)生沖突,多次刪除也是,完美解決問題。
CREATE TABLE `eam_location` ( `id` int(11) NOT NULL AUTO_INCREMENT, `location_id` varchar(50) NOT NULL COMMENT ’位置代碼’, `location_level` tinyint(1) NOT NULL COMMENT ’位置級(jí)別’, `location_name` varchar(50) NOT NULL COMMENT ’位置名稱’, `parent_location_id` varchar(50) COMMENT ’上級(jí)位置代碼’, `delete_flag` datetime COMMENT ’軟刪除’, `version` int(11) DEFAULT 1 COMMENT ’樂觀鎖’, PRIMARY KEY (`id`), UNIQUE KEY `unique_location_delete_flag`(`location_id`, `delete_flag`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
entity類:
@AllArgsConstructor@NoArgsConstructor@Builder(toBuilder = true)@Data@EqualsAndHashCode(callSuper = false)public class EamEquipmentType implements Serializable { private static final long serialVersionUID = 1L; /** * 數(shù)據(jù)庫自增id */ @TableId(value = 'id', type = IdType.AUTO) private Integer id; /** * 設(shè)備類型編號(hào) */ private String typeId; /** * 設(shè)備類型 */ private String typeName; /** * 設(shè)備廠商 */ private String manufacture; /** * 設(shè)備型號(hào) */ private String model; /** * 標(biāo)準(zhǔn)設(shè)備bom 0:未創(chuàng)建 1:已創(chuàng)建 */ private Boolean typeBom; /** * 標(biāo)準(zhǔn)設(shè)備bom id */ private Integer typeBomId; /** * 創(chuàng)建時(shí)間 */ private LocalDateTime createTime; /** * 軟刪除 */ @TableLogic() @TableField(fill = FieldFill.INSERT) private LocalDateTime deleteFlag; /** * 樂觀鎖 */ @Version @TableField(fill = FieldFill.INSERT) private Integer version;
yml配置文件:
mybatis-plus: global-config: db-config: logic-delete-value: 'now()' #邏輯刪除值是個(gè)db獲取時(shí)間的函數(shù) logic-not-delete-value: 'null' #邏輯未刪除值為字符串 'null'
到此這篇關(guān)于詳解MyBatisPlus邏輯刪除與唯一索引沖突問題的文章就介紹到這了,更多相關(guān)MyBatisPlus邏輯刪除與唯一索引沖突內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. navicat for mysql導(dǎo)出數(shù)據(jù)庫的方法2. 啟動(dòng)Oracle常見疑難問題分析3. 數(shù)據(jù)庫相關(guān)的幾個(gè)技能:ACCESS轉(zhuǎn)SQL4. Navicat Premium操作MySQL數(shù)據(jù)庫(執(zhí)行sql語句)5. Access數(shù)據(jù)庫安全的幾個(gè)問題6. navicat for mysql導(dǎo)出sql文件的方法7. Windows下不能啟動(dòng)mysql服務(wù)--錯(cuò)誤總結(jié)8. mybatis plus代碼生成工具的實(shí)現(xiàn)代碼9. MySQL5.7 mysqldump備份與恢復(fù)的實(shí)現(xiàn)10. Centos7 mysql數(shù)據(jù)庫安裝及配置實(shí)現(xiàn)教程
