springboot整合mybatis遇到的问题
迪丽瓦拉
2025-05-31 01:08:37
0

文章目录

  • 1、pom.xml文件的mybatis依赖需要用starter才能跑起来
  • 2、Could not autowire. No beans of 'SysUserMapper' type found.
  • 3、Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
  • 4、mapper的一个demo
  • 5、发送请求后台错误

1、pom.xml文件的mybatis依赖需要用starter才能跑起来

去maven仓库中找只找到这个(地址:https://mvnrepository.com/artifact/org.mybatis/mybatis)
在这里插入图片描述


org.mybatismybatis3.5.13

换成starter

 org.mybatis.spring.bootmybatis-spring-boot-starter2.2.0

2、Could not autowire. No beans of ‘SysUserMapper’ type found.

① 在mapper上面加注解

@Repository(value = "SysUserMapper" )

在这里插入图片描述② 在service的实现类上面关掉自动注入bean

 @Autowired(required=false)

在这里插入图片描述
③ 把设置的自动注入问题改成warring
在这里插入图片描述

3、Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

翻译过来就是
在这里插入图片描述

问题所在:在查阅相关资料之后,得知是由于jdbc驱动包名引发的问题

5.x版本的驱动文件jar包对应的是:
Class.forName("com.mysql.jdbc.Driver");
语句来加载数据库驱动而我使用的是8.0x版本的数据库驱动文件,对此,需要将加载数据库驱动的语句更改为:
Class.forName("com.mysql.cj.jdbc.Driver");除此之外:
url的设置也得进行修改,原本的url如下:
String ur="jdbc:mysql://127.0.0.1:3306/student";应修改为如下:String url="jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";其中student是数据库名8.0x是不需要建立ssl连接的,你需要显示关闭,即url中的&useSSL=false;
serverTimezone=GMT%2B8"是进行时区的设置

① 查看mysql版本
在这里插入图片描述
进入application.yml修改mysql配置

 #驱动类driver-class-name: com.mysql.cj.jdbc.Driver

② 查看application.yml,url不对
在这里插入图片描述
修改为

# 地址
url: jdbc:mysql://localhost:3306/数据库名字?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true

修改后
在这里插入图片描述

4、mapper的一个demo



5、发送请求后台错误

报错显示空指针异常
在这里插入图片描述
在这里插入图片描述
后面在mapper加了一个注解就可以了

@Mapper

在这里插入图片描述

相关内容