我們在使用mybatis-plus
時,一般設備邏輯刪除是非常簡單的,基本上在yaml
等配置文件中做一下配置。然后在字段上注解@TableLogic
就可以了。有不清楚的,可以參考https://www.xiangcaowuyu.net/java/mybatis-plus-logical-deletion.html
但是今天在項目中,發現一個問題,就是明明也正確的進行了配置,但是在進行數據庫操作時,發現邏輯刪除並沒有生效。
問題描述
先說一下問題先想,數據庫指定的字段可以使用,但是指定是否邏輯刪除的值時還是mybatis-plus
默認的0
和1
,並不是我指定的N
和Y
。
配置文件
先來看下我的配置文件。
- mybatisPlus:
- # 搜索指定包別名
- typeAliasesPackage: net.xiangcaowuyu.**.domain
- # 加載全局的配置文件
- configLocation: classpath:mybatis/mybatis-config.xml
- global-config:
- db-config:
- # 配置邏輯刪除
- logic-delete-field: del_flag
- logic-not-delete-value: N
- logic-delete-value: Y
通過配置文件,我指定數據庫標記邏輯刪除的字段為del_flag
,如果已經刪除,標記為Y
,如果沒有刪除(默認值)就是N
。
實體
通過提取的公共實體,標記邏輯刪除字段,如下
- public class BaseEntity implements Serializable {
- private static final long serialVersionUID = 1L;
- /**
- * 搜索值
- */
- private String searchValue;
- /**
- * 創建者
- */
- private Long createBy;
- /**
- * 創建時間
- */
- private Date createTime;
- /**
- * 更新者
- */
- private Long updateBy;
- /**
- * 更新時間
- */
- private Date updateTime;
- /**
- * 刪除標志(Y-已刪除,N-未刪除)
- */
- private String delFlag;
- /**
- * 開始時間
- */
- private String beginTime;
- /**
- * 結束時間
- */
- private String endTime;
- }
使用
調用了一個update
方法
- postMapper.updatePost(post);
在進行更新操作時,mybatis-plus
會追加where條件防止更新到已刪除數據,且使用wrapper.entity
生成的where
條件會忽略該字段。也就是說,我本來的方法對應的sql
可能是
- update xx set xx where xx=xx
如果我配置的邏輯刪除沒有問題的話,mybatis-plus
生成的sql
應該是
- update xx set xx where xx=xx and del_flag = 'N'
但是實際我測試發現,生成的sql
卻是
- update xx set xx where xx=xx and del_flag = '0'
可以看到,雖然邏輯刪除的字段是對的,但是實際上,對應字段是否刪除的值還是mybatis-plus
默認的,並不是我們設置的。
問題分析
其實這個問題之前還是好的,讓我想到應該是最近配置的SqlSessionFactory
的問題。
- public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
- String typeAliasesPackage = env.getProperty("mybatis-plus.typeAliasesPackage");
- String mapperLocations = env.getProperty("mybatis-plus.mapperLocations");
- String configLocation = env.getProperty("mybatis-plus.configLocation");
- typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
- VFS.addImplClass(SpringBootVFS.class);
- final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean ();
- sessionFactory.setDataSource(dataSource);
- sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
- sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
- sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
- sessionFactory.setPlugins(mybatisSqlInterceptor(),mybatisPlusInterceptor());
- return sessionFactory.getObject();
- }
我這里重新注入了MybatisSqlSessionFactoryBean
,但是並沒有對它的配置進行修改,這就導致了我配置文件里的東西並沒有加載。
解決
解決辦法也很簡單,兩種方式我們分別說下。
方式一
方式一是在我們實體邏輯刪除的注解上加上刪除和未刪除對應的值。
- /**
- * 刪除標志(Y-已刪除,N-未刪除)
- */
- private String delFlag;
方式二
方式二就是,我們在MybatisSqlSessionFactoryBean
的bean
里,把我們配置文件里的配置加上。對SqlSessionFactory
改造如下
- public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
- String typeAliasesPackage = env.getProperty("mybatis-plus.typeAliasesPackage");
- String mapperLocations = env.getProperty("mybatis-plus.mapperLocations");
- String configLocation = env.getProperty("mybatis-plus.configLocation");
- typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
- VFS.addImplClass(SpringBootVFS.class);
- final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean ();
- sessionFactory.setDataSource(dataSource);
- sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
- sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
- sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
- sessionFactory.setPlugins(mybatisSqlInterceptor(),mybatisPlusInterceptor());
- sessionFactory.setGlobalConfig(globalConfig());
- return sessionFactory.getObject();
- }
- public MybatisSqlInterceptor mybatisSqlInterceptor() {
- MybatisSqlInterceptor mybatisSqlInterceptor = new MybatisSqlInterceptor();
- Properties properties = new Properties();
- mybatisSqlInterceptor.setProperties(properties);
- return mybatisSqlInterceptor;
- }
- /**
- * 邏輯刪除插件
- */
- public GlobalConfig globalConfig() {
- GlobalConfig globalConfig = new GlobalConfig();
- GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
- dbConfig.setLogicDeleteValue("Y");
- dbConfig.setLogicNotDeleteValue("N");
- globalConfig.setDbConfig(dbConfig);
- return globalConfig;
- }