美文网首页
Springboot与MyBatis-Plus整合开发基础

Springboot与MyBatis-Plus整合开发基础

作者: JohnYuCN | 来源:发表于2021-08-01 18:18 被阅读0次

    官网的入门案例缺失和过时的东西太多,本文进行了纠正,并增加了ISerivce的使用入门


    秦皇岛开埠码头-2021年7月21日.jpg

    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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.example</groupId>
        <artifactId>mybatis-plus-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.3</version>
            <relativePath/>
        </parent>
        <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>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.3.1</version>
            </dependency>
        </dependencies>
    </project>
    

    2. application.yml:

    spring:
      datasource:
        driver-class-name: org.h2.Driver
        url: jdbc:h2:mem:test
        username: root
        password: test
      sql:
        init:
          schema-locations:
            - classpath:db/schema-h2.sql
          data-locations:
            - classpath:db/data-h2.sql
    

    3. schema-h2.sql和data-h2.sql,使用自动增长主键,方便后期测试使用

    db/schema-h2.sql:

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

    db/data-h2.sql:

    DELETE FROM user;
    
    INSERT INTO user (name, age, email) VALUES
    ('John', 100, 'test1@baomidou.com'),
    ('Jack', 20, 'test2@baomidou.com'),
    ('Tom', 28, 'test3@baomidou.com'),
    ('Sandy', 21, 'test4@baomidou.com'),
    ('Billie', 24, 'test5@baomidou.com');
    

    4. cn.johnyu.pojo.User

    package cn.johnyu.pojo;
    import lombok.Data;
    
    @Data
    public class User {
        private Long id;
        private String name;
        private Integer age;
        private String email;
    }
    
    

    4. cn.johnyu.mapper.UserMapper

    package cn.johnyu.mapper;
    
    import cn.johnyu.pojo.User;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import org.springframework.stereotype.Component;
    
    @Component
    public interface UserMapper extends BaseMapper<User> {
    
    }
    

    5. 引导类:

    package cn.johnyu;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan("cn.johnyu.mapper")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
    

    5. cn.johnyu.service.UserService

    CURD核心功能: https://baomidou.com/guide/crud-interface.html

    import cn.johnyu.mapper.UserMapper;
    import cn.johnyu.pojo.User;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.stereotype.Service;
    
    //ServiceImpl是IService的实现类,同时注入了UserMapper
    @Service
    public class UserService extends ServiceImpl<UserMapper, User>{
    //    自动注入UserMapper对象
    //    以下代码是代理生成的,可以不必硬编码
    //    @Autowired
    //    private BaseMapper<User> baseMapper;
    }
    

    6. 测试类:

    package cn.johnyu;
    
    import cn.johnyu.mapper.UserMapper;
    import cn.johnyu.pojo.User;
    import cn.johnyu.service.UserService;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import static org.junit.jupiter.api.Assertions.*;
    import java.util.List;
    
    @SpringBootTest
    public class SampleTest {
        @Autowired
        private UserMapper userMapper;
        @Autowired
        private UserService userService;
        @Test
        public void testSelect() {
            System.out.println(("----- selectAll method test ------"));
            List<User> userList = userMapper.selectList(null);
    
            assertEquals(5, userList.size());
            userList.forEach(System.out::println);
        }
        @Test
        public void testSelect2() {
            User user= userService.getById(1);
            assertEquals(user.getName(),"John");
        }
    }
    

    相关文章

      网友评论

          本文标题:Springboot与MyBatis-Plus整合开发基础

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