美文网首页
SpringBoot(40) — SpringBoot整合MyB

SpringBoot(40) — SpringBoot整合MyB

作者: 奔跑的佩恩 | 来源:发表于2021-11-12 12:27 被阅读0次

    前言

    在上节中我们对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的整个依赖代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.1</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com</groupId>
        <artifactId>firstpro</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>firstpro</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <!--  开启spring-boot-starter -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!--  开启web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--开启测试-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- 自定义配置文件注解依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <!-- lombok依赖 -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.20</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
    
    
            <!-- mysql数据库连接驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <!-- druid(阿里德鲁伊)连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.2.6</version>
            </dependency>
            
            <!-- mybatis-plus -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.3.4</version>
            </dependency>
    
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

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

    二. MyBatis-plus 详细接入流程

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

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

    相关文章

      网友评论

          本文标题:SpringBoot(40) — SpringBoot整合MyB

          本文链接:https://www.haomeiwen.com/subject/viinzltx.html