SpringBoot(40) — SpringBoot整合MyBatis-plus

前言

在上节中我们对MyBatis-plus特性有了一个整体的认识,然后也大致讲了些MyBatisMyBatis-plus的不同之处。大家感兴趣的话,可参考以下文章
SpringBoot(39) — MyBatis-plus简介
这节我们来讲讲SpringBoot项目如何快速接入MyBatis-plus框架。
今天涉及的知识有:

  1. 添加依赖
  2. MyBatis-plus详细接入流程
    2.1 在application-test.yml中添加数据库连接配置
    2.2 建数据表实体映射类
    2.3 新建Mapper操作类
    2.4 扫描 mapper类集合
  3. 测试
  4. 遇到的问题: 找不到数据表或数据表不存在
  5. 项目结构图

运行结果如下:

======我是测试啊=====
=============Student(id=2, name=小明, age=18)
=============Student(id=3, name=小华, age=20)
=============Student(id=4, name=小黑, age=15)
=============Student(id=9, name=小黑仔, age=16)
=============Student(id=10, name=小白, age=35)
=============Student(id=26, name=小灰0, age=12)
=============Student(id=27, name=小灰1, age=13)
=============Student(id=28, name=小灰2, age=14)
=============Student(id=31, name=大学, age=20)

一. 添加依赖

这里添加依赖主要包括三部分:SpringBoot项目自身基础依赖数据库依赖MyBatis-plus框架依赖。为了更好的展示,这里我贴出pom.xml的整个依赖代码:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.1
         
    
    com
    firstpro
    0.0.1-SNAPSHOT
    firstpro
    Demo project for Spring Boot

    
        UTF-8
        UTF-8
        1.8
    

    
        
        
            org.springframework.boot
            spring-boot-starter
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
        
            org.projectlombok
            lombok
            1.18.20
            provided
        
        
            junit
            junit
            test
        


        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
            com.alibaba
            druid
            1.2.6
        
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.3.4
        


    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


这里我引用的mybatis-plus版本为3.4.3.4

二. MyBatis-plus 详细接入流程

2.1 在application-test.yml中添加数据库连接配置

application-test.yml中添加数据库连接配置如下:

你可能感兴趣的:(SpringBoot(40) — SpringBoot整合MyBatis-plus)