• MyBatis-Plus中的Mapper用法



    MyBatis-Plus中的Mapper用法

    • MySQL和MyBatis-Plus包
    • SpringBoot配置信息
    • @Mapper注解
    • BaseMapper< T >接口

    1、准备数据库与表

    
    CREATE DATABASE `mybatis_plus` 
    
    USE `mybatis_plus`;
    
    DROP TABLE IF EXISTS `user`;
    
    CREATE TABLE `user` (
      `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
      `user_name` VARCHAR(30) CHARACTER SET utf8mb3 COLLATE utf8_general_ci DEFAULT NULL COMMENT '姓名',
      `age` INT DEFAULT NULL COMMENT '年龄',
      `email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
      `sex` VARCHAR(10) DEFAULT NULL,
      PRIMARY KEY (`id`)
    );
    
    
    INSERT  INTO `user`(`id`,`user_name`,`age`,`email`,`sex`) VALUES 
    (1,'李四',21,'list@qq.com','男'),
    (2,'Billie',24,'test5@qq.com','男'),
    (3,'HeXi',20,'test6@qq.com','男'),
    (4,'馨馨',21,'xinxin@qq.com','女'),
    (5,'小红',22,'xiaohong@qq.com','女'),
    (6,'兮兮',20,'xixi@qq.com','女');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    2、创建SpringBoot项目SQL(MySQL Driver)

    • 省略

    3、pom.xml导入jar包配置刷新Maven

            
       <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>3.5.2version>
            dependency>
          
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <scope>runtimescope>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4、配置application.yml

    # 连接数据库信息
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC
        username: root
        password: 111111
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5、启动类

    package com.sgz.mybatisplus;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    //@MapperScan("com.sgz.mybatisplus.mapper")   // 启动类配置@MapperScan,mapper接口配置@Mapper,二选一
    public class Day66MybatisplusMapperApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(Day66MybatisplusMapperApplication.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    6、创建pojo类

    package com.sgz.mybatisplus.pojo;
    
    /**
     * 日期:2022/8/18 - 12:12
     * 需求:
     */
    public class User {
    
        private Integer id;
        private String userName;
        private Integer age;
        private String email;
        private String sex;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", userName='" + userName + '\'' +
                    ", age=" + age +
                    ", email='" + email + '\'' +
                    ", sex='" + sex + '\'' +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    7、mapper接口继承BaseMapper< T >

    package com.sgz.mybatisplus.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.sgz.mybatisplus.pojo.User;
    import org.apache.ibatis.annotations.Mapper;
    
    /**
     * 日期:2022/8/18 - 12:16
     * 需求:
     */
    @Mapper
    public interface UserMapper extends BaseMapper<User> {
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    8、测试类

    package com.sgz.mybatisplus;
    
    import com.sgz.mybatisplus.mapper.UserMapper;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class Day66MybatisplusMapperApplicationTests {
    
        @Autowired
        private UserMapper userMapper;
    
        @Test
        void contextLoads() {
            System.out.println(userMapper.selectList(null));
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    CentOS7安装GmSSL过程记录
    cpu设计和实现(iverilog工具)
    Prometheus+Grafana部署
    uniapp封装mixins实现H5和微信小程序的微信支付
    实战一个 Jenkins 构建 CI/CD流水线 的简单配置过程哈
    Python_数据容器_列表list
    从零开始搭建Prometheus+grafana服务器&组件监控系统
    Linux中常见的问题
    ChatGPT Prompting开发实战(七)
    matlab:输出一维矩阵中所有重复元素的索引
  • 原文地址:https://blog.csdn.net/s17856147699/article/details/126354101