MybatisPlus-问题汇总

150 阅读3分钟

本文是系列文章,目录:
一、MybatisPlus-基本使用
二、MybatisPlus-进阶使用-条件构造器
三、MybatisPlus-进阶使用-自定义sql
四、MybatisPlus-进阶使用-Service接口(1)-基本使用
五、MybatisPlus-进阶使用-Service接口(2)-自定义service
六、MybatisPlus-进阶使用-Service接口(3)- 批量新增
七、MybatisPlus-进阶使用-逻辑删除
八、MybatisPlus-进阶使用-枚举处理器
九、MybatisPlus-进阶使用-JSON类型处理器
十、MybatisPlus-进阶使用-配置文件加密
十一、MybatisPlus-插件功能-分页插件(1)
十二、MybatisPlus-插件功能-分页插件(2)-通用分页封装
十三、MybatisPlus-插件功能-乐观锁插件
十四、MybatisPlus-插件功能-sql性能分析
十五、MybatisPlus-自动填充字段
MybatisPlus-问题汇总

问题一、从mybatis转为使用mybatis-plus

之前我做了一个简单的项目,一直用的mybatis。最近想新增加点功能,顺便改为使用mybatis-plus。因为一直说mybatis-plus是在 MyBatis 的基础上只做增强不做改变,无侵入性。想着过程应该很丝滑。但是还是出现了一点问题。我们直接看问题。

image.png 错误的信息显示的是 “无效的绑定语句

如果我们使用的是mybatis的话,应该比较好解决,直接去看xml文件与dao层接口是否对应的上即可。

但是我们的mybatis-plus是自动帮我们实现了selectList。去网上找了一下,说实体与表名不一致,依赖版本等等问题。但是试过很多中办法,还是没能解决。

最后我想到了是不是原来的mybatis配置影响了。看原来的mybatis配置:

package com.pino.demo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan("com.pino.demo.mapper")
public class MybatisConfig {
    @Autowired
    private DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
       SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
       sessionFactory.setDataSource(dataSource);
       sessionFactory.setTypeAliasesPackage("cn.com.home.web.**.model"); //扫描model
       PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
       sessionFactory.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
       return sessionFactory.getObject();
    }
}

后来经过调试,是sqlSessionFactory影响了。在sqlSessionFactory中我们配置了datasourceTypeAliasesPackageMapperLocations。 最终有两种办法可以解决这个问题:

第一种办法(推荐): 那我们把原来配置在sqlSessionFactory的信息放到mybatis-plus中配置。问题也可以得到解决。

  1. 删除原mybatis中sqlSessionFactory配置
  2. 代替为在mybatis-plus中配置

在application.yaml中配置

mybatis-plus:
  mapper-locations: classpath:/sqlmap/*.xml
  type-aliases-package: cn.com.home.web.model

第二种办法:

我们在配置sqlSessionFactory的时候,要换成mybatis-plus中另一个Bean工厂类,叫做MybatisSqlSessionFactoryBean,该类拷贝了SqlSessionFactoryBean,并且重写了自己的自定义加载方法buildSqlSessionFactory。在这个方法中会加载mapperLocations

buildSqlSessionFactory.java

if (this.mapperLocations != null) {
    if (this.mapperLocations.length == 0) {
        LOGGER.warn(() -> "Property 'mapperLocations' was specified but matching resources are not found.");
    } else {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }
            try {
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                    targetConfiguration, mapperLocation.toString(), targetConfiguration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new IOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }
            LOGGER.debug(() -> "Parsed mapper file: '" + mapperLocation + "'");
        }
    }
} else {
    LOGGER.debug(() -> "Property 'mapperLocations' was not specified.");
}

最终修改后的配置:

SqlSessionFactory改为MybatisSqlSessionFactoryBean

package com.pino.demo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan("com.pino.demo.mapper")
public class MybatisConfig {
    @Autowired
    private DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
       MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
       sessionFactory.setDataSource(dataSource);
       sessionFactory.setTypeAliasesPackage("cn.com.home.web.**.model"); //扫描model
       PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
       sessionFactory.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
       return sessionFactory.getObject();
    }
}

这个办法可能导致分页插件失效,暂未能解决,有小伙伴解决了,记得说一下哦