詳解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. SQL SERVER偏移函數(shù)(LAG、LEAD、FIRST_VALUE、LAST _VALUE、NTH_VALUE)2. DB2 與 Microsoft SQL Server 2000 之間的 SQL 數(shù)據(jù)復(fù)制3. Oracle 數(shù)據(jù)字典4. 細(xì)化解析:Oracle 10g ASM 的一點(diǎn)經(jīng)驗(yàn)5. SQL Server使用CROSS APPLY與OUTER APPLY實(shí)現(xiàn)連接查詢6. 理解 DB2 中列組統(tǒng)計(jì)信息7. DB2 XML 全文搜索之為文本搜索做準(zhǔn)備8. 把SQL SERVER里表里的數(shù)據(jù)導(dǎo)出成為insert into 腳本9. MySQL InnoDB架構(gòu)的相關(guān)總結(jié)10. DB2建立nickname導(dǎo)致CRASH解決方法

網(wǎng)公網(wǎng)安備