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','女');

在这里插入图片描述

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

  • 省略

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

        <!--MyBatis-Plus-->
   <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>
      <!--MySQL驱动,如果手动选择了,则不需要添加-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

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

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);
    }

}

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 + '\'' +
                '}';
    }
}

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> {
}

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));
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值