springboot 3 下连接 mysql 数据库以及整合 mybatis-plus

小灰灰 2023-09-27 14:21 104阅读 0赞

测试示例

在 Springboot 3.x版本下整合 mysql 以及mybatis-plus,并进行简单测试,解决实现过程中所遇到的常见 Bug。

引入依赖

  1. <!-- 数据库驱动 -->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. </dependency>
  6. <!-- lombok -->
  7. <dependency>
  8. <groupId>org.projectlombok</groupId>
  9. <artifactId>lombok</artifactId>
  10. </dependency>
  11. <!-- mybatis-plus 3.5.3 才支持 spring boot 3-->
  12. <dependency>
  13. <groupId>com.baomidou</groupId>
  14. <artifactId>mybatis-plus-boot-starter</artifactId>
  15. <version>3.5.3</version>
  16. </dependency>

配置 application.yml

  1. # mysql 配置
  2. datasource:
  3. driver-class-name: com.mysql.cj.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/community?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
  5. username: root
  6. password: yumuing
  7. type: com.zaxxer.hikari.HikariDataSource
  8. hikari:
  9. maximum-pool-size: 15
  10. minimum-idle: 5
  11. idle-timeout: 30000
  12. # mybatis-plus 配置
  13. mybatis-plus:
  14. configuration:
  15. map-underscore-to-camel-case: true
  16. auto-mapping-behavior: full
  17. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  18. use-generated-keys: true
  19. mapper-locations: classpath*:mapper/**/*Mapper.xml
  20. type-aliases-package: top.yumuing.community.entity

创建 MySQL 数据库

创建 community 数据库,运行以下 SQL 代码完成数据表创建:

  1. INSERT INTO user (id, name, email) VALUES
  2. (1, 'Jone', 'test1@baomidou.com'),
  3. (2, 'Jack','test2@baomidou.com'),
  4. (3, 'Tom','test3@baomidou.com'),
  5. (4, 'Sandy', 'test4@baomidou.com'),
  6. (5, 'Billie','test5@baomidou.com');
  7. Query OK, 5 rows affected (0.01 sec)
  8. Records: 5 Duplicates: 0 Warnings: 0
  9. mysql> DROP TABLE IF EXISTS `user`;
  10. CREATE TABLE `user` (
  11. `id` int DEFAULT NULL,
  12. `name` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  13. `age` int DEFAULT NULL
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

测试 MySQL 连接

  1. @Resource
  2. DataSource dataSource;
  3. @Test
  4. void contextLoadsOne() throws Exception{
  5. System.out.println("获取的数据库连接为:"+dataSource.getConnection());
  6. }

控制台输出以下内容即为 MySQL 连接配置成功:

  1. 获取的数据库连接为:HikariProxyConnection@54056059 wrapping com.mysql.cj.jdbc.ConnectionImpl@5611bba

测试整合 Mybatis-plus 简单配置

  1. @Resource
  2. private UserMapper userMapper;
  3. @Test
  4. void contextLoadsTwo() {
  5. List<User> list = userMapper.selectList(null);
  6. list.forEach(item-> System.out.println(item));
  7. }

控制台输出以下内容即为配置 Mybatis-plus 成功

  1. User(id=1, name=张三, age=20)
  2. User(id=2, name=李四, age=22)
  3. User(id=3, name=王五, age=30)

当然,我们也可以使用 MybatisX 插件对 mapper.class、mapper.xml、service.class、serviceImpl.class 进行生成,步骤如下:

MybatisX使用

并且,我们也能够使用 MybatisX 插件实现 xml 文件中,mapper 对应的 SQL 代码,在 mapper.class 中写出想要在 xml 中生成代码的方法名,一定要以 select、insert、update等等进行开头,之后就会有对应提示,选中即可,完成后,往前几格子,方法名变红,选择报错点右键选中以下内容即可生成相关代码:

屏幕截图\_20230208\_000238

xml 文件中 selectAllByIdOrderByAge 生成代码如下:

  1. <sql id="Base_Column_List">
  2. id,name,age
  3. </sql>
  4. <select id="selectAllByIdOrderByAge" resultMap="BaseResultMap">
  5. select
  6. <include refid="Base_Column_List"/>
  7. from user
  8. where
  9. id = #{id,jdbcType=NUMERIC}
  10. order by age
  11. </select>

测试代码如下:

  1. @Test
  2. void cotextLoadsFour(){
  3. List<User> users = userMapper.selectAllByIdOrderByAge(3);
  4. users.forEach(item-> System.out.println(item));
  5. }

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 自然也会报红。

b0f43edf4f047f5c2e83f1bc91615da6.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

  1. 检查项目结构,主启动类位置是否正确
  2. 把自动装配@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 包,解决方法如下:

  1. 引入 junit 依赖

    1. <dependency>
    2. <groupId>junit</groupId>
    3. <artifactId>junit</artifactId>
    4. <version>4.12</version>
    5. </dependency>
  2. 导入正确包 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. 代码如下:

  1. @Autowired
  2. DataSource dataSource;

修改 @Autowired 为 @Resource 即可解决

Could not autowire. No beans of ‘UserMapper’ type found.

  1. @Autowired
  2. private UserMapper userMapper;

修改 @Autowired 为 @Resource 即可解决
求点赞转发

发表评论

表情:
评论列表 (有 0 条评论,104人围观)

还没有评论,来说两句吧...

相关阅读