springboot+mybatis+mybatisplus整合实现基本的crud操作

星空流年      2022-05-10     319

关键词:

SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作

1> 数据准备

--  创建测试表
CREATE TABLE `tb_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_name` varchar(20) NOT NULL COMMENT '用户名',
`password` varchar(20) NOT NULL COMMENT '密码',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
-- 插入测试数据
INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES
('1', 'zhangsan', '123456', '张三', '18', 'test1@qq.com');
INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES
('2', 'lisi', '123456', '李四', '20', 'test2@qq.com');
INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES
('3', 'wangwu', '123456', '王五', '28', 'test3@qq.com');
INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES
('4', 'zhaoliu', '123456', '赵六', '21', 'test4@qq.com');
INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`) VALUES
('5', 'sunqi', '123456', '孙七', '24', 'test5@qq.com');

2> 创建工程,导入依赖

pom.xml文件如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.darren</groupId>
    <artifactId>mp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mp</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--简化代码的工具包-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--mybatis-plus的springboot支持-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3> 添加打印Log的配置文件 log4j.properties

log4j.rootLogger=DEBUG,A1

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n

说明:对于这部分,可以添加,也可以不加,不加会有以下提示信息,建议还是添加

 4> 编写 application.yml 配置文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
    username: root
    password: root

5> 编写 pojo实体类

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("tb_user")
public class User {
    @TableId(type = IdType.AUTO) //指定id类型为自增长
    private Long id;

    private String userName;

    private String password;

    private String name;

    private Integer age;

    private String email;
}

说明:

@TableId注解 指定数据库id的生成策略,
对于数据库Id的生成策略,MybatisPlus提供有以下策略,当然这部分小伙伴们也可以自行查看源码。
package com.baomidou.mybatisplus.annotation;

import lombok.Getter;

/**
 * 生成ID类型枚举类
 *
 * @author hubin
 * @since 2015-11-10
 */
@Getter
public enum IdType {
    /**
     * 数据库ID自增
     */
    AUTO(0),
    /**
     * 该类型为未设置主键类型(将跟随全局)
     */
    NONE(1),
    /**
     * 用户输入ID
     * <p>该类型可以通过自己注册自动填充插件进行填充</p>
     */
    INPUT(2),

    /* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
    /**
     * 全局唯一ID (idWorker)
     */
    ID_WORKER(3),
    /**
     * 全局唯一ID (UUID)
     */
    UUID(4),
    /**
     * 字符串全局唯一ID (idWorker 的字符串表示)
     */
    ID_WORKER_STR(5);

    private final int key;

    IdType(int key) {
        this.key = key;
    }
}
补充:
MybatisPlus实体类中常用的注解,还有一个 @TableField ,此注解主要解决以下两个问题:
1、对象中的属性名和字段名不一致的问题(非驼峰)
2、对象中的属性字段在表中不存在的问题

6> 编写Mapper 映射接口

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.darren.mp.pojo.User;

public interface UserMapper extends BaseMapper<User> {
}

说明:这里的Mapper接口通过集成 MybatisPlus 提供的 BaseMapper接口来实现对于单表的各种CURD操作,BaseMapper接口中包含的方法有:

/**
 * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
 * <p>这个 Mapper 支持 id 泛型</p>
 *
 * @author hubin
 * @since 2016-01-23
 */
public interface BaseMapper<T> extends Mapper<T> {

    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     */
    int insert(T entity);

    /**
     * 根据 ID 删除
     *
     * @param id 主键ID
     */
    int deleteById(Serializable id);

    /**
     * 根据 columnMap 条件,删除记录
     *
     * @param columnMap 表字段 map 对象
     */
    int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /**
     * 根据 entity 条件,删除记录
     *
     * @param wrapper 实体对象封装操作类(可以为 null)
     */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);

    /**
     * 删除(根据ID 批量删除)
     *
     * @param idList 主键ID列表(不能为 null 以及 empty)
     */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    /**
     * 根据 ID 修改
     *
     * @param entity 实体对象
     */
    int updateById(@Param(Constants.ENTITY) T entity);

    /**
     * 根据 whereEntity 条件,更新记录
     *
     * @param entity        实体对象 (set 条件值,可以为 null)
     * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
     */
    int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

    /**
     * 根据 ID 查询
     *
     * @param id 主键ID
     */
    T selectById(Serializable id);

    /**
     * 查询(根据ID 批量查询)
     *
     * @param idList 主键ID列表(不能为 null 以及 empty)
     */
    List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    /**
     * 查询(根据 columnMap 条件)
     *
     * @param columnMap 表字段 map 对象
     */
    List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /**
     * 根据 entity 条件,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询总记录数
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 entity 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询全部记录
     * <p>注意: 只返回第一个字段的值</p>
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 entity 条件,查询全部记录(并翻页)
     *
     * @param page         分页查询条件(可以为 RowBounds.DEFAULT)
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * 根据 Wrapper 条件,查询全部记录(并翻页)
     *
     * @param page         分页查询条件
     * @param queryWrapper 实体对象封装操作类
     */
    IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}

7、编写测试用例

7.1 插入操作

import com.darren.mp.mapper.UserMapper;
import com.darren.mp.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTest {
    @Autowired
    private UserMapper userMapper;

    /**
     * 插入操作:
     *
     * 插入一条记录
     * entity 实体对象
     * int insert(T entity);
     */
    @Test
    public void testInsert(){
        User user = new User();
        user.setAge(20);
        user.setEmail("test@qq.com");
        user.setName("lily");
        user.setUserName("lily");
        user.setPassword("123456");
        int result = this.userMapper.insert(user); //返回的result是受影响的行数,并不是自增后的id,下同
        System.out.println("result: " + result);
        System.out.println(user.getId()); //自增后的id会回填到对象中
    }
}

测试结果:

 7.2 更新操作

在MybatisPlus中,更新操作有2种,一种是根据id更新,另一种是根据条件更新。

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.darren.mp.mapper.UserMapper;
import com.darren.mp.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTest {
    @Autowired
    private UserMapper userMapper;

    /**
     * 更新操作
     * 1、根据 ID 修改
     *
     * entity 实体对象
     * int updateById(@Param(Constants.ENTITY) T entity)
     */
    @Test
    public void testUpdateById(){
        User user = new User();
        user.setId(6L);
        user.setAge(30);
        int result = userMapper.updateById(user);
        System.out.println("result: "+result);
    }

    /**
     * 更新操作
     * 2、根据 whereEntity 条件,更新记录
     *
     * entity  实体对象 (set 条件值,可以为 null)
     * updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
     * int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
     */
    @Test
    public void testUpdate(){
        User user = new User();
        user.setEmail("zhangsan@qq.com");//更新的字段

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name","张三");//更新的条件

        //执行更新操作
        int result = userMapper.update(user, queryWrapper);
        System.out.println("result: "+result);
    }
}

测试结果:

1、根据Id更新

2、根据条件更新

 7.3 删除操作

MybatisPlus提供的删除操作有:根据Id删除、根据Map集合删除、根据条件删除、根据Id集合批量删除

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.darren.mp.mapper.UserMapper;
import com.darren.mp.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTest {
    @Autowired
    private UserMapper userMapper;
    
    /**
     * 删除操作
     * 1、根据 ID 删除
     *
     * id为主键ID
     * int deleteById(Serializable id);
     */
    @Test
    public void testDeleteById(){
        int result = userMapper.deleteById(6L);
        System.out.println("result : "+result);
    }

    /**
     * 删除操作
     * 2、根据 columnMap 条件,删除记录
     *
     * columnMap 表字段 map 对象
     * int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
     */
    @Test
    public void testDeleteByMap(){
        HashMap<String, Object> columnMap = new HashMap<>();
        columnMap.put("age",21);
        columnMap.put("name", "张三");

        //将columnMap中的元素设置为删除的条件,多个条件之间为 AND 关系
        int result = userMapper.deleteByMap(columnMap);
        System.out.println("result : "+result);
    }

    /**
     * 删除操作
     * 3、根据 entity 条件,删除记录
     *
     * wrapper 实体对象封装操作类(可以为 null)
     * int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
     */
    @Test
    public void testDelete(){
        //用法一:
        //QueryWrapper<User> wrapper = new QueryWrapper<>();
        //wrapper.eq("user_name", "lisi")
        //.eq("password", "123456");

        //用法二:
        User user = new User();
        user.setUserName("lisi");
        user.setPassword("123456");

        QueryWrapper<User> wrapper = new QueryWrapper<>(user);

        // 将实体对象包装为操作条件,根据包装条件执行删除,多个条件之间为 AND 关系
        int result = userMapper.delete(wrapper);
        System.out.println("result: " + result);
    }

    /**
     * 删除操作
     * 4、删除(根据ID 批量删除)
     *
     * idList 主键ID列表(不能为 null 以及 empty)
     * int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
     */
    @Test
    public void  testDeleteBatchIds(){
        // 根据id集合批量删除数据
        int result = userMapper.deleteBatchIds(Arrays.asList(3L, 4L));
        System.out.println("result:" + result);
    }
}

测试结果:

1、根据Id删除

 2、根据Map集合删除

 3、根据条件删除

 4、根据Id集合批量删除

 7.4 查询操作

MybatisPlus提供了多种查询操作,包括根据id查询、批量查询、查询单条数据、查询列表、分页查询等操作,这里只列出了常用的操作。

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.darren.mp.mapper.UserMapper;
import com.darren.mp.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTest {
    @Autowired
    private UserMapper userMapper;
    
    /**
     * 查询操作
     * 1、根据 ID 查询
     *
     * id 主键ID
     * T selectById(Serializable id);
     */
    @Test
    public void testSelectById(){
        User user = userMapper.selectById(5L);
        System.out.println(user);
    }

    /**查看详情  

springboot整合其他框架--springboot整合mybatis(代码片段)

1.SpringBoot整合Mybatis需求:SpringBoot整合MyBatis。实现步骤:搭建SpringBoot工程引入mybatis起步依赖,添加mysql驱动编写DataSource和MyBatis相关配置定义表和实体类编写dao和mapper文件/纯注解开发测试1.0公共步骤1.0.1搭建SpringBoot... 查看详情

springboot:springboot整合mybatis案例

文章目录SpringBoot整合Mybatis案例一、导入依赖二、编写配置文件三、编写功能代码 查看详情

springboot(48)—mybatis-plus基本配置

...了MyBatis-plus的删除数据,大家有兴趣的话可参看以下文章SpringBoot(40)—SpringBoot整合MyBatis-plusSpringBoot(41)—MyBatis-plus常用查询SpringBoot(42)—MyBatis-plus查询数据表中一列数据的部分字段SpringBoot(43)—MyBatis-plus一些特殊查询SpringBoot(44)—M... 查看详情

springboot整合mybatis-plus

springboot整合mybatis-plus简介mybatis增强工具包,简化CRUD操作。文档http://mp.baomidou.comhttp://mybatis.plus优点|Advantages无侵入:Mybatis-Plus在Mybatis的基础上进行扩展,只做增强不做改变,引入Mybatis-Plus不会对您现有的Mybatis构架产生任何影响... 查看详情

mybatis--springboot整合mybatis

1、添加依赖<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.0.0</version></dependency>2、a 查看详情

springboot整合mybatis(注解版)

详情可以参考Mybatis官方文档  http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/(1)、引入Mybatis针对SpringBoot制作的Starter依赖1<dependency>2<groupId>org.mybatis.spring.boot</grou 查看详情

springboot整合mybatis

springboots使用的版本是2.0.1,注意不同版本可能有差异,并不一定通用添加Mybatis的起步依赖:<!--mybatis起步依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter< 查看详情

springboot整合mybatis之annotation

...需要下载前面一篇文章的代码,在前一章代码上进行修改.SpringBoot整合Mybatis(注解方式)复制前一个项目,修改配置文件,mybatis的相关配置为:mybatis:type-aliases-package:con.mybatis.springboot_mybatis.modelconfiguration:map-underscore-to-camel-case:truelog-im 查看详情

springboot集成mybatis

1.在pom中添加依赖:#mybatis依赖<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version& 查看详情

springboot09:整合mybatis

官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/Maven仓库地址:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter/2.1.2整合Mybatis新建一个模块导入依赖& 查看详情

mybatis-plus:快速开始(springboot+mybatis+mybatis)

01:Mybatis-Plus:了解Mybatis-Plus、快速开始(Mybatis+Mybatis-Plus,Mybatis-Plus自动做了属性映射)02:Mybatis-Plus:快速开始(Spring+Mybatis+Mabatis-Plus)03&#x 查看详情

springboot+mybatis整合

    LZ今天自己搭建了下Springboot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多。其实只用Springboot也可以开发,但是对于多表多条件分页查询,Springboot就有点力不从心了,所以LZ把Mybatis整合进去,不得不说,现在的框架... 查看详情

springboot+mybatis

springboot引入mybatis要植入mvn依赖:<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version> 查看详情

springboot+mybatis整合

  LZ今天自己搭建了下Springboot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多。其实只用Springboot也可以开发,但是对于多表多条件分页查询,Springboot就有点力不从心了,所以LZ把Mybatis整合进去,不得不说,现在的框架搭建真的... 查看详情

springboot+mybatis

一.官网介绍  在Mybatis官方用法中,介绍了使用Mybatis的过程:先创建出一个SqlSessionFactory实例通过SqlSessionFactory实例获取一个SqlSession实例SqlSession包含了对数据库执行命令的全部方法,此时我们可以通过SqlSession执行映射的SQL语... 查看详情

springboot使用mybatis分页插件

  springboot整合mybatis可以使用springboot配置文件的形式,但是配置不了mybatis-config.xml文件(能够配置,但是不扫描),因此数据源和mybatis使用bean的形式处理,实现分页。 一、添加数据源bean,代码如下packagetjresearch;importcom... 查看详情

springboot与mybatis整合(*)

在pom.xml文件中加入数据库、spring-mybatis整合<!--springboot整合mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId> 查看详情