美文网首页springboot
SpringBoot 整合MyBatis-Plus

SpringBoot 整合MyBatis-Plus

作者: 月弦 | 来源:发表于2019-09-27 16:08 被阅读0次

    前言

    MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。可参考https://mp.baomidou.com/guide/

    一、建库建表

    • 建库
    CREATE DATABASE test;
    
    • 建表
      建表并插入数据。
    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)
    );
    
    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');
    

    二、pom.xml文件

    添加lombok依赖和mybatis-plus依赖。

        <properties>
            <java.version>1.8</java.version>
            <mybatis.plus.version>3.2.0</mybatis.plus.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <!--lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
    
            <!--mybatis-plus-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis.plus.version}</version>
            </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-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    

    三、配置文件

    使用yml配置

    spring:
      #DataSource Config
      datasource:
        url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
        schema: classpath:db/schema-mysql.sql
        data: classpath:db/data-mysql.sql
    

    四、实体类

    @Data
    public class User {
        private Long id;
        private String name;
        private Integer age;
        private String email;
    }
    

    五、mapper类

    public interface UserMapper extends BaseMapper<User> {
    
    }
    

    六、controller类

    @RestController
    public class UserController {
    
        @Autowired
        private UserMapper userMapper;
    
        @GetMapping("/users")
        public List<User> list() {
            return userMapper.selectList(null);
        }
    }
    

    七、启动类

    在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

    @SpringBootApplication
    @MapperScan("com.huzh.springbootmybatisplus.mapper")
    public class SpringbootMybatisPlusApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootMybatisPlusApplication.class, args);
        }
    
    }
    

    相关文章

      网友评论

        本文标题:SpringBoot 整合MyBatis-Plus

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