记一次关于springboot1.5.6与mybatis-plus2.x的坑

第一次集成

一开始,本想着直接从之前的springmvc项目中把mybatisplus的maven依赖直接扒过来,扔进pom文件,在yml文件上配置一下mapper相关xml的路径,然后在application中加上mapperScanner的注解和相关的注解(@Service,@Mapper),就直接去跑了。如果只是mybatis的话,这样是完全可以运行的。但这是mybatis-plus…

问题发生的原因

一开始我在没有使用到mybatis-plus的封装方法的时候,仅以mybatis的方式是可以正常访问自己写在xml的相关方法的,但当我要使用mybatis-plus封装好的insert方法的时候,问题就出现了:

invalid-bound-statement-not-found ,这个方法未绑定;
为什么会出现这个问题呢?一开始细查了xml的配置路径,相关注解,发现都没错。这时候开始怀疑配置文件出了问题了。
上网百度了一下,发现大部分人的配置是这样写的

mybatis-plus:
	mapper-locations:classpath:mapper/**/*Mapper.xml

而我是这样写的

mybatis:
	mapper-locations:classpath:mapper/**/*.xml

于是我便按着网友们的方式去更改,结果不用问,当然是不行的=.=
这时候出现了更糟糕的情况:我连在xml内写好的方法也无法访问了,无奈之下只能改回原来的样子,起码我自己写的能访问

怀引用的依赖有错误或冲突

既然都怀疑依赖有问题了,那就去mybatisplus的官网找找样例是怎么样的吧。
去了之后,心情很复杂,嗯。他们写的我都copy过来了,但是这次报错了,报的是这个错误

 Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required

这个是sqlSessionFactory未注入。。。。他们的依赖是这样的

<dependencies>
  <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>最新版本号</version>
  </dependency>
</dependencies>
<!-- 如果mapper.xml是放在src/main/java目录下,需配置以下-->
<build>
  <resources>
      <resource>
          <directory>src/main/java</directory>
          <filtering>false</filtering>
          <includes>
              <include>**/mapper/*.xml</include>
          </includes>
      </resource>
  </resources>
</build>

由于项目原因,我不能使用jdk8,所以只能使用MP2.x版本,MP3.x版本jdk需要8或以上才能运行。在苦于怎么解决的时候,我尝试性的把mybatis的依赖重新加入了pom文件,然后。。。。神奇的不报Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required的错误了。。。。。但是依然是访问不了MP封装好的方法。。。。

更改依赖方式

因为springboot版本中使用的是mybatis-plus-boot-starter作为依赖的方式,而在mybatis-plus-boot-starter中,据说是包含了jdbc、mybatis、mybatis-plus的封装,那我换个方式,一个个分散来加入进去也行吧,反正我已经有mybaytis依赖了。说不定就是冲突了呢?
于是,我把依赖改成了

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- MP 核心库 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.1.8</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
            <version>1.3.2</version>
        </dependency>

然后出现了另一个没有见过的报错,仔细一想,应该是这样方式还需要自己去设置MP的相关配置项。于是便新建了一个配置类:

@Configuration
public class MybatisPlusConfig {
    private final DataSource dataSource;
    private final MybatisProperties properties;
    private ResourceLoader resourceLoader = new DefaultResourceLoader();
    @Autowired(required = false)
    private Interceptor[] interceptors;

    @Autowired(required = false)
    private DatabaseIdProvider databaseIdProvider;
    @Autowired
    public MybatisPlusConfig(ResourceLoader resourceLoader, MybatisProperties properties, @Qualifier("dataSource") DataSource dataSource) {
        this.resourceLoader = resourceLoader;
        this.properties = properties;
        this.dataSource = dataSource;
    }

    @Bean
    public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
        MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
        mybatisPlus.setDataSource(dataSource);
        mybatisPlus.setVfs(SpringBootVFS.class);
        if (StringUtils.hasText(this.properties.getConfigLocation())) {
            mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
        }
        mybatisPlus.setConfiguration(properties.getConfiguration());
        if (!ObjectUtils.isEmpty(this.interceptors)) {
            mybatisPlus.setPlugins(this.interceptors);
        }
        MybatisConfiguration mc = new MybatisConfiguration();
        mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        mybatisPlus.setConfiguration(mc);
        if (this.databaseIdProvider != null) {
            mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
        }
        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
            mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        }
        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }
        if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
            mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
        }
        return mybatisPlus;
    }
}

请注意不要导错包了
配置完成了。OK,点击RUN,运行成功。Yes! 调用MP封装好的方法,OK。收工

注:如果@Autowired注入失败,那是因为你没有配置扫描,需要在application上添加注解

@ComponentScan("com.*")

以下是相关类和配置的一些截图
application
在这里插入图片描述
config
在这里插入图片描述
在这里插入图片描述
yml

在这里插入图片描述
pom
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值