• mybatisplus逻辑删除不生效的解决办法


    我们在使用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. }
  • 相关阅读:
    mysql保存中文乱码的原因和解决办法
    NetSetMan IP地址切换工具
    使用批处理文件,自动设置计算机IP地址
    神逸之作:国产快速启动软件神品ALTRun
    Apache详细介绍
    利用sqoop对mysql执行DML操作
    Mysql定时清空表
    azkaban group分组,权限
    azkaban使用
    sqoop无法导出parquet文件到mysql
  • 原文地址:https://www.cnblogs.com/exmyth/p/15594622.html
Copyright © 2020-2023  润新知