• 「Spring Boot 系列」08. Spring Boot整合MyBatis


    在这里插入图片描述

    目录

    Spring Boot整合MyBatis步骤:

    1. 新建Spring Boot工程,并选择需要使用的技术集

    在这里插入图片描述
    pom.xml文件

    
     
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.2.2
    
    
    
        mysql
        mysql-connector-java
        runtime
    
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    2. 设置相关参数(数据源)

    application.yml

    #配置相关信息
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver  #com.mysql.jdbc.Driver该驱动类过时
        url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
        username: root
        password: root
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3. 编写一个实体类 Person.java

    public class Person {
    
        private Integer id;
        private String name;
        private String city;
    
        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 getCity() {
            return city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "id=" + id +
                    ", name='" + name + ''' +
                    ", city='" + city + ''' +
                    '}';
        }
    }
    
    • 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

    4. 定义数据层接口与映射配置(两种方式,任意一种都可)

    • 方式一:纯注解开发

      @Mapper //SQL映射添加@Mapper注解方便被容器识别
      public interface PersonDao {

      @Select("select * from person where id = #{id}")
      public Person getById(Integer id);
      
      • 1
      • 2

      }

    • 方式二:xml开发

    1. 新建一个PersonMapper接口

      @Mapper
      public interface PersonMapper {

      public List findAll();
      
      • 1

      }

    2. resources下新建一个mapper文件夹并在里面新建 PersonMapper.xml文件

      
      
      • 1
      • 2
      • 3
    3. 在application.yml 中添加MyBatis配置

      #MyBatis配置
      mybatis:
      mapper-locations: classpath:mapper/*Mapper.xml #mapper映射文件路径
      type-aliases-package: com.ityun.springbootstudy.domain #定义实体类的别名

    5. 测试类中注入dao接口,测试功能

    @SpringBootTest
    class SpringbootStudyApplicationTests {
    
        @Autowired
        private PersonDao personDao;
    
        @Autowired
        private PersonMapper personMapper;
    
        @Test
        public void findTest() {
            System.out.println(personDao.getById(1));
        }
    
        @Test
        public void findAllTest(){
            List list = personMapper.findAll();
            System.out.println(list);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    6. 运行结果

    在这里插入图片描述在这里插入图片描述

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    计算机毕业设计JavaSteam游戏平台系统(源码+系统+mysql数据库+lw文档)
    Grade 5 Math
    转换 FLAC、APE 无损音乐格式为 iTunes 支持导入的 M4A 格式
    准备好抛弃 HTML 了吗?Dart 3.1 和 Flutter 3.13 发布
    Docker容器的5个实用案例
    移动硬盘有文件但看不见怎么恢复文件
    springboot基础(32):整合Mongodb
    程序员35岁之后有什么出路?
    MySQL3
    深度对话|Sui在商业技术堆栈中的地位
  • 原文地址:https://blog.csdn.net/m0_67400973/article/details/126080535