• SpringBoot SpringBoot 基础篇 3 SpringBoot 整合第三方技术 3.3 SpringBoot 整合 Mybatis


    SpringBoot

    【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】

    SpringBoot 基础篇

    3 SpringBoot 整合第三方技术

    3.3 SpringBoot 整合 Mybatis
    3.3.1 整合要求
    • 核心配置:数据库连接相关信息(连什么?连谁?什么权限)
    • 映射配置:SQL映射( XML / 纯注解)

    【有了这两个东西才能整合Mybatis】

    3.3.2 整合步骤

    创建一个新的模块工程

    在这里插入图片描述

    勾选Mybatis 和 MySQL 驱动依赖

    在这里插入图片描述

    在这里插入图片描述

    OK,一个全新的SpringBoot 工程

    上来就先把配置文件的格式改掉【yaml】

    在这里插入图片描述

    看看pom 文件

    <dependency>
        <groupId>org.mybatis.spring.bootgroupId>
        <artifactId>mybatis-spring-boot-starterartifactId>
        <version>2.2.2version>
    dependency>
    
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <scope>runtimescope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    看看MySQL 驱动的版本

    在这里插入图片描述

    可以看到是8.xx的,这个版本和MySQL 5.7 配合使用时,会出现一些问题。

    【配置数据库相关信息】

    在这里插入图片描述

    本次使用到的数据库是之前学SSM 整合时用到的数据库

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

    创建book 实体类

    package com.dingjiaxiong.domain;
    
    /**
     * ClassName: Book
     * date: 2022/10/17 10:24
     *
     * @author DingJiaxiong
     */
    
    public class Book {
    
        private Integer id;
        private String type;
        private String name;
        private String description;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        @Override
        public String toString() {
            return "Book{" +
                    "id=" + id +
                    ", type='" + type + '\'' +
                    ", name='" + name + '\'' +
                    ", 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
    • 58

    在这里插入图片描述

    实现数据层接口Dao

    package com.dingjiaxiong.dao;
    
    import com.dingjiaxiong.domain.Book;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    
    /**
     * ClassName: BookDao
     * date: 2022/10/17 10:25
     *
     * @author DingJiaxiong
     */
    
    @Mapper
    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
    • 19
    • 20

    测试这个接口

    package com.dingjiaxiong;
    
    import com.dingjiaxiong.dao.BookDao;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class Springboot05MybatisApplicationTests {
    
        @Autowired
        private BookDao bookDao;
    
        @Test
        void testGetById(){
            System.out.println(bookDao.getById(1));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    测试结果

    在这里插入图片描述

    OK, 没毛病

    【整合步骤】

    1. 创建新模块,选择Spring初始化,并配置模块相关基础信息
    2. 选择当前模块需要使用的技术集(MyBatis、MySQL)
    3. 设置数据源参数
    4. 定义数据层接口与映射配置
    5. 测试类中注入dao接口,测试功能
    3.3.3 小结
    1. 勾选MyBatis技术,也就是导入MyBatis对应的starter
    2. 数据库连接相关信息转换成配置
    3. 数据库SQL映射需要添加@Mapper被容器识别到
  • 相关阅读:
    微信小程序健康管理系统的开发与实现
    矩池云如何自定义端口,访问自己的web项目
    第4章 系统管理员模块功能实现
    Python基础内容训练5(常用的数据类型-----字典)
    所见即所得的动画效果:Animate.css
    2022年广西壮族自治区中职网络安全技能竞赛“Linux操作系统渗透测试详解”
    QT---lineEdit相关信号
    凤凰架构2——访问远程服务
    从路由器真机提取固件包(二)
    ​企业该如何做好源代码防泄密工作
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/127799442