基于SpringBoot3从零配置MybatisPlus记录
文章目录
- 1.环境
 - 2.表数据准备
 - 3. 配置
 - pom配置
 - yml 配置
 - @MapperScan
 
- 3.问题总结
 - 问题1: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
 - 问题2:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
 - mapper-locations如何配置:
 - 最终解决方案
 
1.环境
- SpringBoot 3.0.6
 - Mysql 8.0
 - JDK 17
 
2.表数据准备
CREATE TABLE `ya_user` (
	`id` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
	`user_id` VARCHAR ( 32 ) NOT NULL COMMENT '账号',
	`user_name` VARCHAR ( 64 ) NOT NULL COMMENT '用户名',
	`user_password` VARCHAR ( 32 ) NOT NULL COMMENT '密码',
	PRIMARY KEY ( `id` ),
	UNIQUE KEY `uk_user_id` ( `user_id` ) 
) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT '用户表';
INSERT INTO `ya_user` ( `user_id`, `user_name`, `user_password` )
VALUES ( 'ya', '阿雅', '123456' );
 
3. 配置
pom配置
 注意mybatis-plus版本号为mybatis-plus 3.5.3.1。
 我刚开始使用的版本号为3.5.1一直有问题,见文末的问题总结。
<dependencies>
     ...
     <!-- mybatis-plus -->
     <dependency>
         <groupId>com.baomidou</groupId>
         <artifactId>mybatis-plus-boot-starter</artifactId>
         <version>3.5.3.1</version>
     </dependency>
    <!-- mysql driver -->
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>8.0.25</version>
     </dependency>
     ...
</dependencies>
<build>
    <resources>
	   <!-- xml放在java目录下-->
	    <resource>
	        <directory>src/main/java</directory>
	        <includes>
	            <include>**/*.xml</include>
	        </includes>
	    </resource>
	    <resource>
	        <directory>src/main/resources</directory>
	    </resource>
	</resources>
	...
</build>
 
yml 配置
server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
  mapper-locations: classpath*:com/ya/boottest/**/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
 
@MapperScan
@MapperScan("com.ya.boottest.**.mapper")
 ** 可以模糊匹配数个中间数个文件夹
 我的目录结构是这样的:有mp自动生成的mapper,有自己手写sql使用的mapper(二者也可以用一个,看个人习惯),这样写,两个文件夹的mapper都能扫描到
 启动类上使用@MapperScan 和接口上的@Mapper至少要使用一个,否则启动时就会报错:
 Error creating bean with name ‘yaUserServiceImpl’:
 Unsatisfied dependency expressed through field ‘baseMapper’:
 No qualifying bean of type ‘com.ya.boottest.user.autoCode.mapper.YaUserMapper’ available:
 expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
 {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 
3.问题总结
Attention:下述的xml,yml都是试错过程中的记录,完整的、正确的配置都在上文↑。
问题1: Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required
在mybatis-plus使用3.5.1的情况下,会报错:
 Error creating bean with name ‘yaUserMapper’ defined in file [D:\workspace\personal\boot-test\target\classes\com\ya\boottest\user\autoCode\mapper\YaUserMapper.class]:
 Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
 
还要额外引入:
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
 
问题2:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
在引入了上述依赖后,项目正常启动,但是在使用mapper查询时,会报错:
 [Request processing failed: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ya.boottest.user.mapper.UserMapper.listUser] with root cause
 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ya.boottest.user.mapper.UserMapper.listUser
绑定异常,mapper接口和mapper.xml文件没有映射绑定成功。
 这里浪费了我大量时间。
-  
首先,我的mapper和xml文件命名一致,这个无须怀疑。
 -  
mapper的namespace和select的resultType也很基础,没有问题。

 -  
其次,虽然我的xml文件在java目录,没有在resources目录中,但是我在pom文件中做了处理
 
      <!-- xml放在java目录下-->
      <resource>
          <directory>src/main/java</directory>
          <includes>
              <include>**/*.xml</include>
          </includes>
      </resource>
 
- 我怀疑是我yml里
mapper-locations配置的问题,导致扫描不到。 
mapper-locations如何配置:
我特意去恶补了 关于classpath的知识
参考文章:/和/和/**的区别;classpath和classpath的区别
classpath*可以匹配多个路径下的多个文件
 **可以模糊匹配中间多级路径
 *.xml可以模糊匹配所有xml文件
我的yml配置,应该是没错的。
mybatis-plus:
  mapper-locations: classpath*:com/ya/boottest/**/*.xml
 
最终解决方案
在我苦思冥想,自己究竟哪里不行,甚至怀疑SpringBoot3到底能不能用mybatis的时候
我在这篇文章Spring Boot 3.x- MybatisPlus集成的评论区找到了解决之法!
这是一个我觉得很完美的解决办法,既解决了我问题2中的报错,也一并解决了我问题1中的需要额外引入mybatis-spring-boot-starter的问题。
      <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>mybatis-plus-boot-starter</artifactId>
          <version>3.5.3.1</version>
      </dependency>
 
至此,问题解决:
 





















