一篇带你了解MyBatisPlus的使用

导读:本篇文章讲解 一篇带你了解MyBatisPlus的使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、MybatisPlus基础简介

1.1、 简介

在这里插入图片描述

​ MyBatis-Plus(简称 MP),一个 MyBatis 的增强工具包,只做增强不做改变. 发工作、提高生产率而生。

我们的愿景是成为 Mybatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。

在这里插入图片描述

1.2、MyBatis Plus的下载

官方地址:

http://mp.baomidou.com

代码发布地址:(国外代码托管平台)

Github:

https://github.com/baomidou/mybatis-plus

Gitee:码云(国内代码托管平台)

https://gitee.com/baomidou/mybatis-plus

文档发布地址:

http://mp.baomidou.com/

MyBatis Plus 是国内团队(包米豆) 团队开发开源的!

在这里插入图片描述

1.3 、前置知识

我们将通过一个简单的 Demo 来阐述 MyBatis-Plus 的强大功能,在此之前,我们假设您已经对以下知识掌握:

Mybatis

Maven

二、环境搭建

2.1、创建测试表

其对应的数据库 Schema 脚本如下:

/*
Navicat MySQL Data Transfer

Source Server         : 124.222.12.151
Source Server Version : 80029
Source Host           : 124.222.12.151:3308
Source Database       : SSM

Target Server Type    : MYSQL
Target Server Version : 80029
File Encoding         : 65001

Date: 2022-08-31 07:28:40
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `tb_employee`
-- ----------------------------
DROP TABLE IF EXISTS `tb_employee`;
CREATE TABLE `tb_employee` (
  `id` int NOT NULL AUTO_INCREMENT,
  `last_name` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8_bin DEFAULT NULL,
  `myemail` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8_bin DEFAULT NULL,
  `gender` char(1) CHARACTER SET utf8mb3 COLLATE utf8_bin DEFAULT NULL,
  `age` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin;

-- ----------------------------
-- Records of tb_employee
-- ----------------------------
INSERT INTO `tb_employee` VALUES ('6', 'Tom', 'tom@163.com', '1', '22');
INSERT INTO `tb_employee` VALUES ('7', 'Jerry', 'jerry@163.com', '0', '25');
INSERT INTO `tb_employee` VALUES ('8', 'Black', 'black@163.com', '1', '30');
INSERT INTO `tb_employee` VALUES ('9', 'White', 'white@163.com', '0', '35');
INSERT INTO `tb_employee` VALUES ('10', 'admin1', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('11', 'admin2', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('12', 'admin3', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('13', 'admin4', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('14', 'admin5', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('15', 'admin16', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('16', 'admiin22', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('17', 'admiin22', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('18', '张三1', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('19', '张三3', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('20', '张三4', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('21', '张三5', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('22', '张三6', '32432423@qq.com', '女', '44');
INSERT INTO `tb_employee` VALUES ('23', '张三7', '32432423@qq.com', '女', '44');

2.2、创建javaBean

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_employee") //默认是以类名为查询表名字,也可以指定表名!
public class Employee {
    @TableId(type = IdType.AUTO) //指定ID是主键,并且ID是使用数据库自增!
    private Integer id;
    private String lastName;  //默认支持驼峰命名法 数据库 last_name  laseName 识别的!
    @TableField("myemail")   //如果数据库中字段和实体类属性不一致,但是又不满足驼峰命名法,可以加注解进行说明!
    private String email;
    //@TableField("gender")
    private String gender;
    private Integer age;
}

1)@TableName

2)@TableField
在这里插入图片描述

忽略实体类中的某个属性

3)@TableId

2.3、封装MybatisUtils

/**
 * 封装一个MyBatis工具类
 */
public class MyBatisUtils {

    static InputStream inputStream=null;
    static SqlSessionFactory sqlSessionFactory=null;

    //定义Threadlocal存储类型为SqlSession
    private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();

    static {
        try {
            //1.mybatis的核心配置文件
            String resources="mybatis-config.xml";
            //2.使用外部的配置文件构建sqlsession工厂
            inputStream = Resources.getResourceAsStream(resources);
            //sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //替换原来的SqlSessionFactoryBuilder 使用子类 MybatisSqlSessionFactoryBuilder
            sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 01-获取sqlSession
     * @return
     */
    public static SqlSession getSqlSession(){
        //1.从线程里面获取共享的SqlSession对象
        SqlSession sqlSession = threadLocal.get();
        //2.对象为空,创建一个新的sqlSession写入线程共享ThreadLocal中
        if (sqlSession == null) {
            sqlSession = sqlSessionFactory.openSession(true); //true:表示自动提交事务 默认值false
            //System.out.println(sqlSession + ",初始化SqlSession资源");
            threadLocal.set(sqlSession);
        }
        //3.返回有效对象
        return sqlSession;
    }

    /**
     * 02-获取sqlSession
     * @param sqlSession
     */
    public static void closeSqlSession(SqlSession sqlSession){
        try {
            if(sqlSession!=null){
                sqlSession.close();
            }
            if(inputStream!=null){
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }


}

集成 Mybatis-Plus

Mybatis-Plus 的集成非常简单,我们仅仅需要把 Mybatis自带的

SqlSession session=null;
CardMapper cardMapper=null;

@Before
public void init(){
    try {
        session = new MybatisSqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml")).openSession();
        cardMapper = session.getMapper(CardMapper.class);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

在这里插入图片描述

2.4、数据库连接配置

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://124.222.13.153:3308/SSM?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root

2.5、日志打印配置

# log4J日志框架的配置文件 文件名字不能改
# DEBUG 表示日志的级别  调试
# Console 日志打印在控制台
log4j.rootLogger=DEBUG, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n

# 哪些日志需要打印
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

2.6、Mybatis-config配置文件配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--加载外部的属性文件-->
    <properties resource="jdbc.properties"></properties>

    <!--MyBatis数据库配置信息-->
    <environments default="mysql">

        <!--数据库的配置环境:mysql-->
        <environment id="mysql">
            <!--事务管理器 JDBC-->
            <transactionManager type="JDBC"/>
            <!--数据库连接池:表示MyBatis内置的连接池,后期可以切换为Druid-->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!--指向Mapper.xml文件位置 -->
    <!--映射Mapper文件-->
    <mappers>
        <!--        <mapper resource="mappers/UserMapper.xml"></mapper>-->
        <!--        <mapper resource="mappers/AddressMapper.xml"></mapper>-->
        <!--统一引用mapper:约定:xxxMapper.xml需要放在com/bruce/mapper/-->
        <package name="com.haidi8.mapper"/>
    </mappers>

</configuration>

2.7、EmployeeMapper.java

/**
 * Mapper接口
 * 基于Mybatis: 在Mapper接口中编写CRUD相关的方法  提供Mapper接口所对应的SQL映射文件 以及 方法对应的SQL语句.
 * 基于MP:  让XxxMapper接口继承 BaseMapper接口即可.
 *           BaseMapper<T>:泛型指定的就是当前Mapper接口所操作的实体类类型
 */
public interface EmployeeMapper extends BaseMapper<Employee> {
    //继承了父接口的17个常用的数据库操作方法没如果不够,那么可以自己写!
    int addBatch(List<Employee> employees);
}

实现方式:

基于 Mybatis需要编写 EmployeeMapper 接口,并手动编写 CRUD 方法提供 EmployeeMapper.xml 映射文件,并手动编写每个方法对应的 SQL 语句.

基于 MP只需要创建 EmployeeMapper 接口, 并继承 BaseMapper 接口.这就是使用 MP需要完成的所有操作,甚至不需要创建 SQL 映射文件。

提供的方法:

在这里插入图片描述

一旦继承BaseMapper之后,系统内置了有十几个CRUD相关的方法,这些方法可以直接使用,不用自己来定义!

在MyBatis Plus的底层。这些接口的SQL语句是自动生成的,不需要去管SQL语句!说白了,直接调用就可以!

2.8、EmployeeMappper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.haidi8.mapper.EmployeeMapper">


    <insert id="addBatch">
        insert into tb_employee values
        <foreach collection="list" separator="," item="emp">
            (null,#{emp.lastName},#{emp.email},#{emp.gender},#{emp.age})
        </foreach>
    </insert>


</mapper>

特别说明: Mybatis依赖请勿加入项目配置,以免引起版本冲突!!!

Mybatis-Plus 会自动帮你维护!

MyBatisPlus包是一个综合包(包含MyBatis包,不用再导入一遍的!)

在这里插入图片描述

2.9、项目目录和pom文件

在这里插入图片描述

<?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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.haidi8.mybatis</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mybatisplus</artifactId>
    <dependencies>
        <!--数据库驱动 8.XXX mysql驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.1.1</version>
        </dependency>


        <!-- Junit单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!--log4j日志工具包-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--Lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <!-- mapper.xml文件在java目录下 -->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <!--指定src下的内容编译-->
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
    </build>


</project>

2.10、测试

package com.haidi8.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haidi8.mapper.EmployeeMapper;
import com.haidi8.pojo.Employee;
import com.haidi8.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestEmployee {

    SqlSession sqlSession = null;
    EmployeeMapper em=null;

    @Before
    public void init() {
        sqlSession = MyBatisUtils.getSqlSession();
        em = sqlSession.getMapper(EmployeeMapper.class);

    }

    @After
    public void destory(){
        sqlSession.close();
    }

    @Test
    public void testList(){
        List<Employee> employees = em.selectList(null);//全查 没有参数
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    @Test
    public void testAdd(){
        Employee employee=new Employee(null,"admiin22","32432423@qq.com","女",44);
        int count = em.insert(employee);
        System.out.println(count>0?"新增成功":"新增失败");
    }

    @Test
    public void testDeleteById(){
        int count = em.deleteById(2);
        System.out.println(count>0?"删除成功":"删除失败");
    }

    @Test
    public void tesUpdateById(){
        Employee employee=new Employee();
        employee.setId(3);
        employee.setAge(100);
        employee.setLastName("尼古拉斯赵四");
        int count = em.updateById(employee);
        System.out.println(count>0?"更新成功":"更新失败");
    }

    @Test
    public void testGetByid(){
        Employee employee = em.selectById(3);
        System.out.println(employee);
    }

    /**
     * DELETE FROM tb_employee WHERE gender = ? AND age = ?
     */
    @Test
    public void testDeleteByMap(){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("gender","0");
        map.put("age",35); //条件是并且的关系  等值判断
        int count = em.deleteByMap(map);
        System.out.println(count>0?"删除成功":"删除失败");
    }



    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE gender = ? AND age = ?
     */
    @Test
    public void testSelectByMap(){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("gender","0");
        map.put("age",35); //条件是并且的关系  等值判断
        List<Employee> employees = em.selectByMap(map);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE id IN ( ? , ? , ? )
     */
    @Test
    public void testselectBatchIds(){
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1);
        ids.add(5);
        ids.add(3);
        List<Employee> employees = em.selectBatchIds(ids);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    /**
     * DELETE FROM tb_employee WHERE id IN ( ? , ? , ? )
     */
    @Test
    public void testsdeleteBatchIds(){
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1);
        ids.add(5);
        ids.add(3);
        int count= em.deleteBatchIds(ids);
        System.out.println(count>0?"删除成功":"删除失败");
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE last_name = ? AND age < ?
     */
    @Test
    public void test1(){
        QueryWrapper qw=new QueryWrapper(); //条件构造器
        qw.eq("last_name","Tom");  // last_name = ?
        qw.lt("age",100);  // age < ?
        List<Employee> employees = em.selectList(qw);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE age BETWEEN ? AND ? OR gender = ?
     */
    @Test
    public void test2() {
        QueryWrapper<Employee> qw = new QueryWrapper<Employee>();
        qw.between("age",18,20); // age BETWEEN ? AND ?
        qw.or();  // or
        qw.eq("gender",1); // gender = ?
        List<Employee> emps =em.selectList(qw);
        for (Employee e : emps) {
            System.out.println(e);
        }
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE age BETWEEN ? AND ? AND id < ?
     */
    @Test
    public void testBetweenAnd(){
        List<Employee> employees = em.selectList(new QueryWrapper<Employee>().between("age", 18, 40).lt("id",4));
        for (Employee e : employees) {
            System.out.println(e);
        }
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE age BETWEEN ? AND ? OR gender = ? ORDER BY age ASC
     */
    @Test
    public void test3() {
        QueryWrapper<Employee> qw = new QueryWrapper<Employee>();
        qw.between("age",18,20);  // age BETWEEN ? AND ?
        qw.or();  // OR
        qw.eq("gender",1);  // gender = ?
        qw.orderByAsc("age"); // ORDER BY age ASC
        List<Employee> emps =em.selectList(qw);
        for (Employee e : emps) {
            System.out.println(e);
        }
    }


    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE age BETWEEN ? AND ? OR gender = ? ORDER BY age ASC , id DESC
     */
    @Test
    public void test4() {
        QueryWrapper<Employee> qw = new QueryWrapper<Employee>();
        qw.between("age",18,20);
        qw.or();
        qw.eq("gender",1);
        qw.orderByAsc("age"); // ORDER BY age ASC
        qw.orderByDesc("id"); // id DESC
        List<Employee> emps =em.selectList(qw);
        for (Employee e : emps) {
            System.out.println(e);
        }
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE age > ? AND last_name LIKE ?
     * 15(String), %o%(String)
     */
    @Test
    public void test5(){
        List<Employee> emps = em.selectList(new QueryWrapper<Employee>().gt("age","15").like("last_name","o"));
        for (Employee emp : emps) {
            System.out.println(emp);
        }
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE age > ? AND last_name LIKE ?
     * 15(String), %o(String)
     */
    @Test
    public void test6(){
        List<Employee> emps = em.selectList(new QueryWrapper<Employee>().gt("age","15").likeLeft("last_name","o"));
        for (Employee emp : emps) {
            System.out.println(emp);
        }
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE age > ? AND last_name LIKE ?
     * 15(String), o%(String)
     */
    @Test
    public void test7(){
        List<Employee> emps = em.selectList(new QueryWrapper<Employee>().gt("age","15").likeRight("last_name","o"));
        for (Employee emp : emps) {
            System.out.println(emp);
        }
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE age > ? AND last_name IS NOT NULL
     */
    @Test
    public void test8(){
        List<Employee> emps = em.selectList(new QueryWrapper<Employee>().gt("age","15").isNotNull("last_name"));
        for (Employee emp : emps) {
            System.out.println(emp);
        }
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE age > ? AND last_name IS NULL
     */
    @Test
    public void test9(){
        List<Employee> emps = em.selectList(new QueryWrapper<Employee>().gt("age","15").isNull("last_name"));
        for (Employee emp : emps) {
            System.out.println(emp);
        }
    }

    /**
     * SELECT COUNT( 1 ) FROM tb_employee WHERE last_name LIKE ?
     *  %b%(String)
     */
    @Test
    public void testCount(){
        Integer count = em.selectCount(new QueryWrapper<Employee>().like("last_name", "b"));
        System.out.println(count);
    }

    /**
     * SELECT max(age),min(age) FROM tb_employee
     */
    @Test
    public void test10(){
        List<Map<String, Object>> maps = em.selectMaps(new QueryWrapper<Employee>().select("max(age),min(age)"));
        for (Map<String, Object> map : maps) {
            System.out.println(map);
        }
    }

    @Test
    public void testGroupBy(){
        QueryWrapper<Employee> qw = new QueryWrapper<Employee>().select("gender", "max(age)","min(age)").groupBy("gender");
        List<Map<String, Object>> maps = em.selectMaps(qw);
        System.out.println(maps);
    }

    /**
     * SELECT
     *     gender,
     *     max(age),
     *     min(age)
     * FROM
     *     tb_employee
     * GROUP BY
     *     gender
     * HAVING
     *     max(age) > 30
     */
    @Test
    public void testHaving(){
        QueryWrapper<Employee> qw =
                new QueryWrapper<Employee>().select("gender", "max(age)","min(age)").groupBy("gender").having("max(age)>30");
        List<Map<String, Object>> maps = em.selectMaps(qw);
        System.out.println(maps);
    }

    /**
     * SELECT id,last_name,myemail AS email,gender,age FROM tb_employee WHERE id NOT IN (?,?,?)
     */
    @Test
    public void testNotIn(){
        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(3);
        QueryWrapper<Employee> qw = new QueryWrapper<Employee>().notIn("id",ids);
        List<Employee> employees = em.selectList(qw);
        for (Employee e : employees) {
            System.out.println(e);
        }
    }

    @Test
    public void testaddBatch(){
        Employee employee1=new Employee(null,"张三1","32432423@qq.com","女",44);
        Employee employee2=new Employee(null,"张三3","32432423@qq.com","女",44);
        Employee employee3=new Employee(null,"张三4","32432423@qq.com","女",44);
        Employee employee4=new Employee(null,"张三5","32432423@qq.com","女",44);
        Employee employee5=new Employee(null,"张三6","32432423@qq.com","女",44);
        Employee employee6=new Employee(null,"张三7","32432423@qq.com","女",44);

        List<Employee> list=new ArrayList<Employee>();
        list.add(employee1);
        list.add(employee2);
        list.add(employee3);
        list.add(employee4);
        list.add(employee5);
        list.add(employee6);
        int count = em.addBatch(list);
        System.out.println(count>0?"批量成功":"批量失败");
    }

}


主意:Having必须和Group by一起使用!Having作用就是:对分组之后的数据再次筛选

2.11、通用 CRUD 小结

  1. 以上是基本的 CRUD 操作,如您所见,我们仅仅需要继承一个 BaseMapper 即可实现大部分单表 CRUD 操作。BaseMapper 提供了多达 17 个方法给大家使用, 可以极其方便的实现单一、批量、分页等操作。极大的减少开发负担,难道这就是 MP 的强大之处了吗?

  2. 提出需求:现有一个需求,我们需要分页查询 tbl_employee 表中,年龄在 18~50 之间性别为男且姓名为 xx 的所有用户,这时候我们该如何实现上述需求呢?

    MyBatis : 需要在 SQL 映射文件中编写带条件查询的 SQL,并基于 PageHelper 插件完成分页. 实现以上一个简单的需求,往往需要我们做很多重复单调的工作。普通的 Mapper 能够解决这类痛点吗?

    MP: 依旧不用编写 SQL 语句, MP 提供了功能强大的条件构造器EntityWrapper

2.12、分页查询(现在是控制台版本)

在这里插入图片描述

配置分页插件(需要配置MyBatis Plus分页插件)

<plugins>
    <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/>
</plugins>

在这里插入图片描述

三、踩坑

3.1 、依赖是否导入

在这里插入图片描述

3.2、使用MyBatisPlus要继承BassMapper接口

在这里插入图片描述

3.3、MyBatis和MyBatisPlus获取SqlSessionFactory的方式是不一样的

在这里插入图片描述

3.4、log4j的配置

在这里插入图片描述

3.5 、主键自增MyBatisPlus要对主键ID要配置注解识别

在这里插入图片描述

3.6、使用MybatisPlus过程中出现数据库表字段和实体类字段不一样的注解解决方式

在这里插入图片描述

3.7、BaseMapper接口提供的常见方法

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/94162.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!