基于SpringBoot3从零配置MybatisPlus

基于SpringBoot3从零配置MybatisPlus记录

系列文章指路👉

系列文章-基于SpringBoot3创建项目并配置常用的工具和一些常用的类

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>

至此,问题解决:
res1

res2

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
mybatis-plus是一个基于mybatis的增强工具,它简化了mybatis配置和使用。在Spring Boot项目中使用mybatis-plus,需要进行一些配置。 1. 引入依赖:在`pom.xml`文件中添加mybatis-plus的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> ``` 2. 配置数据源:在`application.properties`或`application.yml`文件中配置数据库连接信息,例如: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 配置mybatis-plus:在`application.properties`或`application.yml`文件中添加mybatis-plus配置,例如: ```yaml mybatis-plus.mapper-locations=classpath:mapper/*.xml ``` 这里的`mapper/*.xml`表示mapper文件存放在`resources/mapper`目录下。 4. 创建Mapper接口:创建一个继承自`BaseMapper`的Mapper接口,例如: ```java @Repository public interface UserMapper extends BaseMapper<User> { } ``` 这里的`User`是实体类,`UserMapper`继承自`BaseMapper`,可以直接使用mybatis-plus提供的方法进行数据库操作。 5. 创建实体类:创建一个与数据库表对应的实体类,例如: ```java @Data public class User { private Long id; private String name; private Integer age; } ``` 这里使用了Lombok的`@Data`注解,简化了实体类的编写。 6. 创建Mapper XML文件:在`resources/mapper`目录下创建与Mapper接口对应的XML文件,例如`UserMapper.xml`,并在其中定义SQL语句,例如: ```xml <?xml version="1.0" encoding="UTF-8" ?> <mapper namespace="com.example.mapper.UserMapper"> <select id="selectById" resultType="com.example.entity.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 这里的`namespace`指定了Mapper接口的全限定名,`selectById`是方法名,`resultType`指定了返回结果的类型。 7. 使用Mapper:在需要使用Mapper的地方注入Mapper,并调用方法进行数据库操作,例如: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User getUserById(Long id) { return userMapper.selectById(id); } } ``` 这里的`userMapper.selectById(id)`就是使用mybatis-plus提供的方法进行数据库查询。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小雅痞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值