javacomparator_mybatis是做什么的

2022-09-22 19:44:30 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

Myabatis-Plus集成异常

下面贴出错误信息 : java.lang.NoSuchMethodError: com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(Ljava/lang/CharSequence;)Z

代码语言:javascript复制
11:29:34.886 [main] DEBUG org.springframework.boot.context.logging.ClasspathLoggingApplicationListener - Application failed to start with classpath: unknown
11:29:35.129 [background-preinit] DEBUG org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator - Loaded expression factory via original TCCL
11:29:35.188 [background-preinit] DEBUG org.hibernate.validator.internal.engine.ValidatorFactoryImpl - HV000234: Using org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator as ValidatorFactory-scoped message interpolator.
11:29:35.188 [background-preinit] DEBUG org.hibernate.validator.internal.engine.ValidatorFactoryImpl - HV000234: Using org.hibernate.validator.internal.engine.resolver.JPATraversableResolver as ValidatorFactory-scoped traversable resolver.
11:29:35.188 [background-preinit] DEBUG org.hibernate.validator.internal.engine.ValidatorFactoryImpl - HV000234: Using org.hibernate.validator.internal.util.ExecutableParameterNameProvider as ValidatorFactory-scoped parameter name provider.
11:29:35.189 [background-preinit] DEBUG org.hibernate.validator.internal.engine.ValidatorFactoryImpl - HV000234: Using org.hibernate.validator.internal.engine.DefaultClockProvider as ValidatorFactory-scoped clock provider.
11:29:35.189 [background-preinit] DEBUG org.hibernate.validator.internal.engine.ValidatorFactoryImpl - HV000234: Using org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory as ValidatorFactory-scoped script evaluator factory.
11:29:37.476 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.NoSuchMethodError: com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(Ljava/lang/CharSequence;)Z
  at com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor.postProcessEnvironment(SafetyEncryptProcessor.java:55)
  at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:179)
  at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:165)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
  at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
  at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:75)
  at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
  at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:347)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
  at com.lenovo.hawk.HawkPaDashboardApplication.main(HawkPaDashboardApplication.java:38)
Disconnected from the target VM, address: '127.0.0.1:56249', transport: 'socket'

可以看到是没有isNotBlank这个方法, mybatis-plus依赖包找不到这个方法,即版本兼容问题,mybatis-plus 的版本与mybatis-plus-boot-starter 版本不兼容

代码语言:javascript复制
         /**
         * 处理加密内容
         */
        if (StringUtils.isNotBlank(mpwKey)) {
            HashMap<String, Object> map = new HashMap<>();
            for (PropertySource<?> ps : environment.getPropertySources()) {
                if (ps instanceof OriginTrackedMapPropertySource) {
                    OriginTrackedMapPropertySource source = (OriginTrackedMapPropertySource) ps;
                    for (String name : source.getPropertyNames()) {
                        Object value = source.getProperty(name);
                        if (value instanceof String) {
                            String str = (String) value;
                            if (str.startsWith("mpw:")) {
                                map.put(name, AES.decrypt(str.substring(4), mpwKey));
                            }
                        }
                    }
                }
            }   

版本兼容问题解决

修改mybatis-plus解决 , 下面贴出集成的mybatis-plus全部依赖

代码语言:javascript复制
	    <!-- mybatis-plus集成依赖包 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <!-- mybatis-plus分页插件 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.4.0</version>
        </dependency>

mybatis-plus集成分页插件问题

由于我使用的是最新的3.4版本,所以PaginationInterceptor 在该jar包中过期,下面贴出解决方案

代码语言:javascript复制
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis-plus 集成分页插件配置
 * @Auther Hsg
 * @Date 2020/09/07
 */
@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor paginationInterceptor(){
        return new MybatisPlusInterceptor();
    }

}

封装查询方法修改

由于使用的版本比较新,对应的 new EntityWrapper<>()方法不可用替换为 new QueryWrapper<>()给出一个简单的用例

代码语言:javascript复制
@Service
@Transactional(readOnly = false)
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    CompanyMapper companyMapper;

    public Integer findCount(){
        return companyMapper.findCount();
    }

    @Override
    public IPage<Company> findByPage() {
        Page<Company> page = new Page<>(0, 2);
        QueryWrapper<Company> query = new QueryWrapper<>();
        query.orderByDesc("name");
        IPage<Company> pageResult = companyMapper.selectPage(page, query);
        return pageResult;
    }

}

配置相关(yaml)

由于项目刚刚搭建完成,此处贴出一些重要的配置信息,后续额外配置需要在一一添加

代码语言:javascript复制
#配置mybatis-plus相关属性
mybatis-plus:
  #配置mapper.xml文件读取位置信息
  mapper-locations: classpath*:mapper/*.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.lenovo.hawk.mapper
  # 全局mapper映射数据库配置
  global-config:
    db-config:
      #主键生成策略
      id-type: UUID
      #数据库大写下划线转换
      capital-mode: true
      #配置逻辑删除字段
      logic-delete-value: 1
      logic-not-delete-value: 0
    refresh: true
  configuration:
    #配置返回数据库(column下划线 -> java驼峰式)
    map-underscore-to-camel-case: true
    #缓存配置false,默认全部重新读取
    cache-enabled: false

至此Mybatis-Plus集成完成,实现简单的CRUD以及分页查询功能

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/169996.html原文链接:https://javaforall.cn

0 人点赞