zoukankan      html  css  js  c++  java
  • Java MyBatis-Plus 基本使用

    原文:https://mp.baomidou.com/guide/quick-start.html



    一、初始化项目

    1、新建项目

    在idea中,新建Spring Initializr类型的项目。
    我这里创建的项目包名是这个:com.example.test5
    然后就是一路next,其它的都不用管。




    2、项目新建好后,打开自动导入包



    二、添加依赖

    1、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.3.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>test4</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>test4</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.3.2</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    2、实际添加的依赖

    实际只导入了这三个包:lombok、MyBatis-Plus、mysql

    <dependency>
    	<groupId>org.projectlombok</groupId>
    	<artifactId>lombok</artifactId>
    	<optional>true</optional>
    </dependency>
    <dependency>
    	<groupId>com.baomidou</groupId>
    	<artifactId>mybatis-plus-boot-starter</artifactId>
    	<version>3.3.2</version>
    </dependency>
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    </dependency>
    

    3、效果如图



    三、配置MySQL数据库

    1、在resources目录,删掉文件application.properties,新增文件application.yml

    # DataSource Config
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8
        username: root
        password: root
    

    2、效果如图



    四、编码

    1、在包entity下,添加类User

    1. com.example.test5下,新建包entity

    2. 然后在entity下,添加类User

    package com.example.test5.entity;
    
    import lombok.Data;
    
    @Data
    public class User {
        private Long id;
        private String name;
        private Integer age;
        private String email;
    }
    
    
    1. 效果如图

    2、在包'mapper'下,添加类UserMapper

    1. com.example.test5下,新建包mapper

    2. 然后在mapper下,添加类UserMapper

    package com.example.test5.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.example.test5.entity.User;
    
    public interface UserMapper extends BaseMapper<User> {
    }
    

    3、在SpringBoot启动类中,添加@MapperScan注解,扫描`mapper'文件夹

    1. 代码如下
    package com.example.test5;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan("com.example.test5.mapper")
    public class Test5Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Test5Application.class, args);
        }
    
    }
    
    1. 效果如图

    五、添加测试代码

    1、在测试类Test5ApplicationTests中,添加测试代码

    1. 代码如下
    package com.example.test5;
    
    import com.example.test5.entity.User;
    import com.example.test5.mapper.UserMapper;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.List;
    
    @SpringBootTest
    class Test5ApplicationTests {
    
        @Autowired
        private UserMapper userMapper;
    
        @Test
        void contextLoads() {
            System.out.println(("----- selectAll method test ------"));
            List<User> userList = userMapper.selectList(null);
            //Assert.assertEquals(5, userList.size());
            userList.forEach(System.out::println);
        }
    
    }
    
    
    1. 效果如图

    六、搭建测试环境

    1、创建表

    DROP TABLE IF EXISTS user;
    
    CREATE TABLE user
    (
    	id BIGINT(20) NOT NULL COMMENT '主键ID',
    	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    	age INT(11) NULL DEFAULT NULL COMMENT '年龄',
    	email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
    	PRIMARY KEY (id)
    );
    

    2、创建测试数据

    DELETE FROM user;
    
    INSERT INTO user (id, name, age, email) VALUES
    (1, 'Jone', 18, 'test1@baomidou.com'),
    (2, 'Jack', 20, 'test2@baomidou.com'),
    (3, 'Tom', 28, 'test3@baomidou.com'),
    (4, 'Sandy', 21, 'test4@baomidou.com'),
    (5, 'Billie', 24, 'test5@baomidou.com');
    

    3、效果如图



    七、测试

    1、先编译一下项目,然后在测试类'Test5ApplicationTests'的'contextLoads'方法上右键,选择Run contextLoads()即可

    2、测试结果的查看


    八、总结

    1、GitHub地址

    https://github.com/guxingy/MyBatis-Plus-Example

  • 相关阅读:
    java静态代码块、静态方法、静态变量、构造代码块、普通代码块
    linux复习3:linux字符界面的操作
    linux复习2:Fedora17系统的安装和删除
    算法设计与分析基础2:算法效率分析基础
    ip2Long 代码
    Spark知识点小结
    Transformation和Action
    Spark的优势
    深入理解RDD原理
    Spark集群的任务提交执行流程
  • 原文地址:https://www.cnblogs.com/guxingy/p/13447956.html
Copyright © 2011-2022 走看看