• SpringBoot 4 SpringBoot 整合 Mybatis


    SpringBoot

    【黑马程序员2022新版SSM框架教程_Spring+SpringMVC+Maven高级+SpringBoot+MyBatisPlus企业实用开发技术】

    4 SpringBoot 整合 Mybatis

    4.1 回顾Spring 整合 Mybatis

    Spring 整合 Mybatis 需要定义很多配置类:

    • SpringConfig 配置类

      • 导入 JdbcConfig 配置类
      • 导入 MybatisConfig 配置类

      在这里插入图片描述

    • JdbcConfig 配置类

    定义数据源(加载properties配置项:driver、url、username、password)

    在这里插入图片描述

    • MybatisConfig 配置类

      • 定义 SqlSessionFactoryBean

      • 定义映射配置

        在这里插入图片描述

    4.2 SpringBoot 整合 Mybatis
    4.2.1 创建新模块

    在这里插入图片描述

    勾选当前模块需要使用的技术集

    在这里插入图片描述

    直接创建

    在这里插入图片描述

    4.2.2 定义实体类
    package com.dingjiaxiong.domain;
    
    /**
     * ClassName: Book
     * date: 2022/9/21 19:53
     *
     * @author DingJiaxiong
     */
    
    public class Book {
        private Integer id;
        private String name;
        private String type;
        private String description;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        @Override
        public String toString() {
            return "Book{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", type='" + type + '\'' +
                    ", description='" + description + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    4.2.3 定义dao 接口
    package com.dingjiaxiong.dao;
    
    import com.dingjiaxiong.domain.Book;
    import org.apache.ibatis.annotations.Select;
    
    /**
     * ClassName: BookDao
     * date: 2022/9/21 19:54
     *
     * @author DingJiaxiong
     */
    
    public interface BookDao {
    
        @Select("select * from tbl_book where id = #{id}")
        public Book getById(Integer id);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    4.2.4 定义测试类
    package com.dingjiaxiong;
    
    import com.dingjiaxiong.dao.BookDao;
    import com.dingjiaxiong.domain.Book;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class Springboot08MybatisApplicationTests {
    
        @Autowired
        private BookDao bookDao;
    
        @Test
        void testGetById(){
            Book book = bookDao.getById(1);
            System.out.println(book);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    4.2.5 编写配置

    在 application.yml 配置文件中配置

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ssm_db
        username: root
        password: 200039
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    4.2.6 测试

    在这里插入图片描述

    可以看到报错了

    在这里插入图片描述

    错误信息显示在 Spring 容器中没有 BookDao 类型的 bean 。

    原因是 Mybatis 会扫描接口并创建接口的代码对象交给 Spring 管理,但是现在并没有告诉 Mybatis 哪个是 dao 接口。而要解决这个问题需要在 BookDao 接口上使用 @Mapper , BookDao 接口改进为

    在这里插入图片描述

    注意:SpringBoot 版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区
    jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC ,或在MySQL数据库端配置时区解决此问题

    4.2.7 使用Druid 数据源

    导入 Druid 依赖

    在这里插入图片描述

    在 application.yml 配置文件配置

    可以通过 spring.datasource.type 来配置使用什么数据源。

    在这里插入图片描述

    4.2.8 再次运行测试

    在这里插入图片描述

    配个时区

    在这里插入图片描述

    再试一次

    在这里插入图片描述

    OK。

  • 相关阅读:
    MultipartFile文件上传
    剑指Offer51数组中的逆序对(相关话题:线段树)
    MySQL基础
    TS初体验
    【校招VIP】测试方案之测试需求分析
    基础数据结构之链表相关的一些问题
    博客系统页面设计
    部署LVS-DR群集
    hutool工具实践-缓存
    Integer对象的大小比较
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/127400565