初始Mybatis-Plus 进行CRUD

发布于:2022-12-15 ⋅ 阅读:(478) ⋅ 点赞:(0)

目录

1. Mybatis-Plus 的概述

1.1 简介

1.2 特性

1.3 框架结构

2. Mybatis-Plus增删改查

2.1 项目搭建

2.2 项目配置

2.3 导入数据 

2.4 创建实体类 

2.5 创建接口

2.6 测试准备

2.7 增删改查

2.7.1 insert

2.7.2 updateById

2.7.3 selectById

2.7.4 selectByMap

2.7.5 selectBatchIds

2.7.6 deleteById

2.7.7 deleteByMap

2.7.8 deleteBatchIds

3. MyBatis-Plus3注解介绍

3.1 @TableName

3.2 @TableId

3.3、@TableField

3.4、@Version

3.5、@EnumValue

3.6、@TableLogic

3.7、@SqlParser

3.8、@KeySequence


1. Mybatis-Plus 的概述

1.1 简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。我们的愿景是成为 MyBatis 最好的搭档,就像魂斗罗中的1P、2P,基友搭配,效率翻倍。

1.2 特性

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作
  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
  • 支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作
  • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )
  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用
  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询
  • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
  • 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
  • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

1.3 框架结构

2. Mybatis-Plus增删改查

2.1 项目搭建

项目建完成之后 打开pom.xml文件添加以下依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

 在src/main/java目录中,创建以下包

  • com.jmh.mybatisplus01.pojo
  • com.jmh.mybatisplus01.mapper

2.2 项目配置

  • 配置跟目录包下的 MybatisPlus01Application 文件配置 MapperScan 注解
@SpringBootApplication
@MapperScan("com.jmh.mybatisplus01.mapper")
public class MybatisPlus01Application {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlus01Application.class, args);
    }

}
  • Spring MVC 工程:(如果用到spring mvc并且mybatis-plus的话就配置以下内容) 

     配置 MapperScan 对象

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.caochenlei.mpdemo.mapper"/>
</bean>

    调整 SqlSessionFactory 为 MyBatis-Plus 的 SqlSessionFactory

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
</bean>
  •  配置 src/main/resources/javaapplication.properties 文件
#mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mp?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=1234

#mybatis-plus
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

2.3 导入数据 

## 创建库
CREATE DATABASE mp;
## 使用库
USE mp;
## 创建表
CREATE TABLE tbl_employee(
   id INT(11) PRIMARY KEY AUTO_INCREMENT,
   last_name VARCHAR(50),
   email VARCHAR(50),
   gender CHAR(1),
   age INT
);
## 导入数据
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tom','tom@qq.com',1,22);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Jerry','jerry@qq.com',0,25);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Black','black@qq.com',1,30);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('White','white@qq.com',0,35);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Tiger','tiger@qq.com',1,28);
INSERT INTO tbl_employee(last_name,email,gender,age) VALUES('Bobby','bobby@qq.com',0,16);
## 查询数据
SELECT * FROM tbl_employee;

2.4 创建实体类 

  • com.jmh.mybatisplus.pojo.Employee 
@TableName("tbl_employee")
public class Employee {
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    @TableField(value = "last_name")
    private String lastName;
    @TableField(value = "email")
    private String email;
    @TableField(value = "gender")
    private Integer gender;
    @TableField(value = "age")
    private Integer age;

    public Employee() {}

    public Employee(Integer id, String lastName, String email, Integer gender, Integer age) {
        this.id = id;
        this.lastName = lastName;
        this.email = email;
        this.gender = gender;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", gender=" + gender +
                ", age=" + age +
                '}';
    }
}

2.5 创建接口

  • com.jmh.mybatisplus01.mapper.EmployeeMapper 

//在对应的Mapper 接口上 基础基本的 BaseMapper<T> T是对应的pojo类
@Repository//告诉容器你是持久层的 @Repository是spring提供的注释,能够将该类注册成Bean
public interface EmployeeMapper extends BaseMapper<Employee> {
    //所有的crud都编写完成了
}

 2.6 测试准备

  • com.jmh.mybatisplus01.MpDemoApplicationTests
package com.jmh.mybatisplus01;

import com.jmh.mybatisplus01.mapper.EmployeeMapper;
import com.jmh.mybatisplus01.pojo.Employee;
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
public class MpDemoApplicationTests {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Test
    void contextLoads() {
        List<Employee> employees = employeeMapper.selectList(null);
        employees.forEach(System.out::println);
    }
}

2.7 增删改查

2.7.1 insert

  • 插入一个员工,员工姓名为“张三”、邮箱为"zhangsan@qq.com"、男性、25岁
@Test
void testInsert() {
    int result = employeeMapper.insert(new Employee(null, "zhangsan", "zhangsan@qq.com", 0, 25));
    System.out.println("result:" + result);
}

 2.7.2 updateById

  • 将id为1的员工的姓名更改为"Jennie"
@Test
void testUpdateById() {
    // 先查询
    Employee employee = employeeMapper.selectById(1);
    employee.setLastName("Jennie");
    // 再修改
    int result = employeeMapper.updateById(employee);
    System.out.println(result);
}

 2.7.3 selectById

  • 查询id为1的员工信息
@Test
void testSelectById() {
    Employee employee = employeeMapper.selectById(1);
    System.out.println(employee);
}

 2.7.4 selectByMap

  • 查询性别为男性(0)且年龄在25岁的员工信息
@Test
void testSelectByMap() {
    Map<String, Object> map = new HashMap<>();
    map.put("gender",0);
    map.put("age",25);
    List<Employee> employees = employeeMapper.selectByMap(map);
    employees.forEach(System.out::println);
}

 2.7.5 selectBatchIds

  • 查询id分别为1、2、3的员工的信息
@Test
void testSelectBatchIds() {
    List<Employee> employees = employeeMapper.selectBatchIds(Arrays.asList(1, 2, 3));
    employees.forEach(System.out::println);
}

 2.7.6 deleteById

  • 删除id为1的员工信息
@Test
void testDeleteById() {
    int result = employeeMapper.deleteById(1);
    System.out.println(result);
}

 2.7.7 deleteByMap

  • 删除性别为男性(0)且年龄在25岁的员工信息
@Test
void testDeleteByMap() {
    Map<String, Object> map = new HashMap<>();
    map.put("gender", 0);
    map.put("age", 25);
    int result = employeeMapper.deleteByMap(map);
    System.out.println(result);
}

 2.7.8 deleteBatchIds

  • 删除id分别为4、5、6的员工的信息
@Test
void testDeleteBatchIds() {
    int result = employeeMapper.deleteBatchIds(Arrays.asList(4, 5, 6));
    System.out.println(result);
}

3. MyBatis-Plus3注解介绍

 3.1 @TableName

 描述:表名注解 

属性 类型 必须指定 默认值 描述
value String “” 表名
schema String “” schema
keepGlobalPrefix boolean false 是否保持使用全局的 tablePrefix 的值(如果设置了全局 tablePrefix 且自行设置了 value 的值)
resultMap String “” xml 中 resultMap 的 id
autoResultMap boolean false
是否自动构建 resultMap 并使用(如果设置 resultMap 则不会进行 resultMap 的自动构建并注入)

 3.2 @TableId

描述:主键注解

属性 类型 必须指定 默认值 描述
value String “” 主键字段名
type Enum IdType.NONE 主键类型

IdType

描述
AUTO 数据库ID自增
NONE 无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
INPUT insert前自行set主键值
ASSIGN_ID 分配ID(主键类型为Number(Long和Integer)或String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)
ASSIGN_UUID 分配UUID,主键类型为String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认default方法)

3.3、@TableField

描述:字段注解(非主键)

属性 类型 必须指定 默认值 描述
value String “” 数据库字段名
el String “” 映射为原生 #{ ... } 逻辑,相当于写在 xml 里的 #{ ... } 部分
exist boolean true 是否为数据库表字段
condition String “” 字段 where 实体查询比较条件,有值设置则按设置的值为准,没有则为默认全局的 %s=#{%s}
update String “” 字段 update set 部分注入, 例如:update="%s+1":表示更新时会set version=version+1(该属性优先级高于 el 属性)
insertStrategy
Enum
N
DEFAULT 举例:NOT_NULL: insert into table_a(<if test="columnProperty != null">column</if>) values (<if test="columnProperty != null">#{columnProperty}</if>)
updateStrategy Enum N DEFAULT

举例:IGNORED: update table_a set

column=#{columnProperty}

whereStrategy Enum N DEFAULT 举例:NOT_EMPTY: where <if test="columnProperty != null and columnProperty!=''">column=#{columnProperty}</if>
fill Enum FieldFill.DEFAULT 字段自动填充策略
select boolean true 是否进行 select 查询
keepGlobalFormat boolean false 是否保持使用全局的 format 进行处理
jdbcType JdbcType JdbcType.UNDEFINED JDBC类型 (该默认值不代表会按照该值生效)
typeHandler Class<? extends TypeHandler> UnknownTypeHandler.class 类型处理器 (该默认值不代表会按照该值生效)
numericScale String “” 指定小数点后保留的

 FieldStrategy

描述
IGNORED 忽略判断
NOT_NULL 非NULL判断
NOT_EMPTY 非空判断(只对字符串类型字段,其他类型字段依然为非NULL判断)
DEFAULT 追随全局配置

 FieldFill

描述
DEFAULT 默认不处理
INSERT 插入时填充字段
UPDATE 更新时填充字段
INSERT_UPDATE 插入和更新时填充字段

3.4、@Version

描述:乐观锁注解、标记 @Verison 在字段上

3.5、@EnumValue

描述:通枚举类注解(注解在枚举字段上)

3.6、@TableLogic

描述:表字段逻辑处理注解(逻辑删除)

属性 类型 必须指定 默认值 描述
value String “” 逻辑未删除值
delval String “” 逻辑删除值

3.7、@SqlParser

描述:租户注解,支持method上以及mapper接口上

属性 类型 必须指定 默认值 描述
filter boolean false true: 表示过滤SQL解析,即不会进入ISqlParser解析链,否则会进解析链并追加例如tenant_id等条件

3.8、@KeySequence

描述:序列主键策略 oracle

属性:value、resultMap

属性 类型 必须指定 默认值 描述
value String “” 序列名
clazz Class Long.class id的类型, 可以指定String.class,这样返回的Sequence值是字符串"1"

网站公告

今日签到

点亮在社区的每一天
去签到