mybatis-plus邏輯刪除不生效的解決辦法


我們在使用mybatis-plus時,一般設備邏輯刪除是非常簡單的,基本上在yaml等配置文件中做一下配置。然后在字段上注解@TableLogic就可以了。有不清楚的,可以參考https://www.xiangcaowuyu.net/java/mybatis-plus-logical-deletion.html
但是今天在項目中,發現一個問題,就是明明也正確的進行了配置,但是在進行數據庫操作時,發現邏輯刪除並沒有生效。

問題描述

先說一下問題先想,數據庫指定的字段可以使用,但是指定是否邏輯刪除的值時還是mybatis-plus默認的01,並不是我指定的NY

配置文件

先來看下我的配置文件。

  1. mybatisPlus:
  2. # 搜索指定包別名
  3. typeAliasesPackage: net.xiangcaowuyu.**.domain
  4. # 加載全局的配置文件
  5. configLocation: classpath:mybatis/mybatis-config.xml
  6. global-config:
  7. db-config:
  8. # 配置邏輯刪除
  9. logic-delete-field: del_flag
  10. logic-not-delete-value: N
  11. logic-delete-value: Y

通過配置文件,我指定數據庫標記邏輯刪除的字段為del_flag,如果已經刪除,標記為Y,如果沒有刪除(默認值)就是N

實體

通過提取的公共實體,標記邏輯刪除字段,如下

  1. @Data
  2. public class BaseEntity implements Serializable {
  3. private static final long serialVersionUID = 1L;
  4. /**
  5. * 搜索值
  6. */
  7. private String searchValue;
  8. /**
  9. * 創建者
  10. */
  11. @TableField(fill = FieldFill.INSERT)
  12. private Long createBy;
  13. /**
  14. * 創建時間
  15. */
  16. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  17. @TableField(fill = FieldFill.INSERT)
  18. private Date createTime;
  19. /**
  20. * 更新者
  21. */
  22. @TableField(fill = FieldFill.UPDATE)
  23. private Long updateBy;
  24. /**
  25. * 更新時間
  26. */
  27. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  28. @TableField(fill = FieldFill.UPDATE)
  29. private Date updateTime;
  30. /**
  31. * 刪除標志(Y-已刪除,N-未刪除)
  32. */
  33. @TableLogic
  34. private String delFlag;
  35. /**
  36. * 開始時間
  37. */
  38. @JsonIgnore
  39. @TableField(exist = false)
  40. private String beginTime;
  41. /**
  42. * 結束時間
  43. */
  44. @JsonIgnore
  45. @TableField(exist = false)
  46. private String endTime;
  47. }

使用

調用了一個update方法

  1. postMapper.updatePost(post);

在進行更新操作時,mybatis-plus會追加where條件防止更新到已刪除數據,且使用wrapper.entity生成的where條件會忽略該字段。也就是說,我本來的方法對應的sql可能是

  1. update xx set xx where xx=xx

如果我配置的邏輯刪除沒有問題的話,mybatis-plus生成的sql應該是

  1. update xx set xx where xx=xx and del_flag = 'N'

但是實際我測試發現,生成的sql卻是

  1. update xx set xx where xx=xx and del_flag = '0'

可以看到,雖然邏輯刪除的字段是對的,但是實際上,對應字段是否刪除的值還是mybatis-plus默認的,並不是我們設置的。

問題分析

其實這個問題之前還是好的,讓我想到應該是最近配置的SqlSessionFactory的問題。

  1. @Bean
  2. public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
  3. String typeAliasesPackage = env.getProperty("mybatis-plus.typeAliasesPackage");
  4. String mapperLocations = env.getProperty("mybatis-plus.mapperLocations");
  5. String configLocation = env.getProperty("mybatis-plus.configLocation");
  6. typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
  7. VFS.addImplClass(SpringBootVFS.class);
  8. final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean ();
  9. sessionFactory.setDataSource(dataSource);
  10. sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
  11. sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
  12. sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
  13. sessionFactory.setPlugins(mybatisSqlInterceptor(),mybatisPlusInterceptor());
  14. return sessionFactory.getObject();
  15. }

我這里重新注入了MybatisSqlSessionFactoryBean,但是並沒有對它的配置進行修改,這就導致了我配置文件里的東西並沒有加載。

解決

解決辦法也很簡單,兩種方式我們分別說下。

方式一

方式一是在我們實體邏輯刪除的注解上加上刪除和未刪除對應的值。

  1. /**
  2. * 刪除標志(Y-已刪除,N-未刪除)
  3. */
  4. @TableLogic(value = "N",delval = "Y")
  5. private String delFlag;

方式二

方式二就是,我們在MybatisSqlSessionFactoryBeanbean里,把我們配置文件里的配置加上。對SqlSessionFactory改造如下

  1. @Bean
  2. public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
  3. String typeAliasesPackage = env.getProperty("mybatis-plus.typeAliasesPackage");
  4. String mapperLocations = env.getProperty("mybatis-plus.mapperLocations");
  5. String configLocation = env.getProperty("mybatis-plus.configLocation");
  6. typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
  7. VFS.addImplClass(SpringBootVFS.class);
  8. final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean ();
  9. sessionFactory.setDataSource(dataSource);
  10. sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
  11. sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
  12. sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
  13. sessionFactory.setPlugins(mybatisSqlInterceptor(),mybatisPlusInterceptor());
  14. sessionFactory.setGlobalConfig(globalConfig());
  15. return sessionFactory.getObject();
  16. }
  17. @Bean
  18. public MybatisSqlInterceptor mybatisSqlInterceptor() {
  19. MybatisSqlInterceptor mybatisSqlInterceptor = new MybatisSqlInterceptor();
  20. Properties properties = new Properties();
  21. mybatisSqlInterceptor.setProperties(properties);
  22. return mybatisSqlInterceptor;
  23. }
  24. /**
  25. * 邏輯刪除插件
  26. */
  27. @Bean
  28. public GlobalConfig globalConfig() {
  29. GlobalConfig globalConfig = new GlobalConfig();
  30. GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
  31. dbConfig.setLogicDeleteValue("Y");
  32. dbConfig.setLogicNotDeleteValue("N");
  33. globalConfig.setDbConfig(dbConfig);
  34. return globalConfig;
  35. }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM