springboot 3 下连接 mysql 数据库以及整合 mybatis-plus
测试示例
在 Springboot 3.x版本下整合 mysql 以及mybatis-plus,并进行简单测试,解决实现过程中所遇到的常见 Bug。
引入依赖
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- mybatis-plus 3.5.3 才支持 spring boot 3-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
配置 application.yml
# mysql 配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/community?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
username: root
password: yumuing
type: com.zaxxer.hikari.HikariDataSource
hikari:
maximum-pool-size: 15
minimum-idle: 5
idle-timeout: 30000
# mybatis-plus 配置
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
use-generated-keys: true
mapper-locations: classpath*:mapper/**/*Mapper.xml
type-aliases-package: top.yumuing.community.entity
创建 MySQL 数据库
创建 community 数据库,运行以下 SQL 代码完成数据表创建:
INSERT INTO user (id, name, email) VALUES
(1, 'Jone', 'test1@baomidou.com'),
(2, 'Jack','test2@baomidou.com'),
(3, 'Tom','test3@baomidou.com'),
(4, 'Sandy', 'test4@baomidou.com'),
(5, 'Billie','test5@baomidou.com');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int DEFAULT NULL,
`name` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`age` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
测试 MySQL 连接
@Resource
DataSource dataSource;
@Test
void contextLoadsOne() throws Exception{
System.out.println("获取的数据库连接为:"+dataSource.getConnection());
}
控制台输出以下内容即为 MySQL 连接配置成功:
获取的数据库连接为:HikariProxyConnection@54056059 wrapping com.mysql.cj.jdbc.ConnectionImpl@5611bba
测试整合 Mybatis-plus 简单配置
@Resource
private UserMapper userMapper;
@Test
void contextLoadsTwo() {
List<User> list = userMapper.selectList(null);
list.forEach(item-> System.out.println(item));
}
控制台输出以下内容即为配置 Mybatis-plus 成功
User(id=1, name=张三, age=20)
User(id=2, name=李四, age=22)
User(id=3, name=王五, age=30)
当然,我们也可以使用 MybatisX 插件对 mapper.class、mapper.xml、service.class、serviceImpl.class 进行生成,步骤如下:
![](https://img-blog.csdnimg.cn/img_convert/f46c6671e32594655662c99d6d57246c.webp?x-oss-process=image/format,png)
并且,我们也能够使用 MybatisX 插件实现 xml 文件中,mapper 对应的 SQL 代码,在 mapper.class 中写出想要在 xml 中生成代码的方法名,一定要以 select、insert、update等等进行开头,之后就会有对应提示,选中即可,完成后,往前几格子,方法名变红,选择报错点右键选中以下内容即可生成相关代码:
![](https://img-blog.csdnimg.cn/img_convert/c93ec9632c8aaa9d66aa9394dd1db1a8.webp?x-oss-process=image/format,png)
xml 文件中 selectAllByIdOrderByAge 生成代码如下:
<sql id="Base_Column_List">
id,name,age
</sql>
<select id="selectAllByIdOrderByAge" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user
where
id = #{id,jdbcType=NUMERIC}
order by age
</select>
测试代码如下:
@Test
void cotextLoadsFour(){
List<User> users = userMapper.selectAllByIdOrderByAge(3);
users.forEach(item-> System.out.println(item));
}
Bug 总结
Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
springboot 3版本整合 mybatis 3.0.5版本控制台报错 Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required,NestedIOException 这个类在 Spring 6 版本中直接删除了。对的,直接删除了。而 MyBatis 老版本还没有同步更新,所以直接就报红了。而整合 mybatis 的 mybatis-plus 自然也会报红。
![](https://img-blog.csdnimg.cn/img_convert/adf0975eb7276208463bc461c1be87b4.webp?x-oss-process=image/format,png)
2022 年 11 月26 日凌晨,mybatis-spring-boot 正式发布 3.0.0 版本,完全支持 Spring Boot 3 了。mybatis-plus 也在 2022.12.28 的 3.5.3 支持了 Spring Boot 3 。最好解决办法就是升级版本。
Could not autowire. No beans of ‘DataSource’ type found
检查项目结构,主启动类位置是否正确
把自动装配@Autowired换成@Resource
Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl
该报错是配置文件路径错误,重点检查url路径,3306后的 test 为数据库名,注意修改成数据库已有数据库名
mysql8.x版本URL为 jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
Cannot resolve method 'assertEquals' in 'Assert'
在测试方法中,使用该方法报错,没引入 import org.junit.Assert; 的Assert 包,解决方法如下:
引入 junit 依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
导入正确包 import org.junit.Assert; 的Assert
Injection of resource dependencies failed
测试方法控制台输出:Injection of resource dependencies failed
确定报错对象为 userMapper,发现没有指定 MapperScan,解决如下:
在启动类加入:@MapperScan("top.yumuing.community.mapper") 即可
Could not autowire. No beans of 'DataSource' type found.
编译报错:Could not autowire. No beans of 'DataSource' type found. 代码如下:
@Autowired
DataSource dataSource;
修改 @Autowired 为 @Resource 即可解决
Could not autowire. No beans of 'UserMapper' type found.
@Autowired
private UserMapper userMapper;
修改 @Autowired 为 @Resource 即可解决
相关文章
- Python——MySQL操作,使用mysql.connector
- Mysql:is not allowed to connect to this MySQL server
- 【Mysql 学习】mysql 的使用入门
- 【C/C++学院】(23)Mysql数据库编程--C语言编程实现mysql客户端
- 【MySQL】Can't get stat of './mysql/slow_log.CSV' (Errcode: 2)
- MySQL 之 库操作
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
- MySQL选择数据库use与mysql_select_db使用详解
- MySQL和MSSQL下,text 、ntext、 image、blob的比较
- mysql 有报错 ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists
- MySQL -- 单行函数
- MySQL内核月报 2015.01-MySQL · 捉虫动态· mysql client crash一例
- PHP中MySQL、MySQLi和PDO的用法和区别
- Starting MySQL. ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).
- Atitit postgresql data type 数据类型与mysql对应表 数据库常用数据类型 PostgreMysql 整数intgreterInt 小数numericFL
- 【高可用MySQL解决方案】centos7配置mysql主从复制
- MySQL Study之--Mysql无法启动“mysql.host”
- SpringBoot整合SpringMVC、持久层技术MyBatis、连接mysql数据库技术
- SpringBoot(Spring、SpringMVC)集成Mybatis操作Mysql数据库
- 【MySQL笔记】mysql来源安装/配置步骤和支持中国gbk/gb2312编码配置
- MySQL(6)数据表中数据的增删改查
- MySQL 获取当前时间戳
- SpringBoot实例②springboot+jdbcTemplate小例子实现增删查改
- 【MySql】MySQL索引15连问(相信大家看完肯定会有帮助)
- MySQL报错Column count of mysql.user is wrong. Expected 43, found 42. Created with MySQL 50568, now run