MyBatis-Plus——Mapper接口中使用自定义的CRUD方法及Mapper.xml映射文件

本文涉及的产品
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: MyBatis-Plus——Mapper接口中使用自定义的CRUD方法及Mapper.xml映射文件

1.案例详解


首先在Navicat中创建一张表。


创建一个SpringBoot工程,在pom文件中添加所需依赖。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>5.1.9</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

编写数据库中student表对应的实体类。

package com.szh.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
/**
 *
 */
@TableName(value = "student")
public class Student {
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    @TableField(value = "name")
    private String name;
    @TableField(value = "age")
    private Integer age;
    @TableField(value = "email")
    private String email;
    @TableField(value = "status")
    private Integer status;
    //getter and setter
    //toString
}

编写mapper接口。

之前几篇文章中,我们的mapper接口中没有任何方法,是因为继承了MP框架中的BaseMapper模板类,可以直接使用BaseMapper中的CRUD操作方法。

那么现在我们定义的mapper接口仍然让它继承MP框架中的BaseMapper模板类,但是我对StudentMapper接口做一个扩展,添加我们自定义的CRUD操作方法。

package com.szh.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.szh.mybatisplus.entity.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
 *
 */
public interface StudentMapper extends BaseMapper<Student> {
    //自定义方法
    int insertStudent(Student student);
    Student selectStudentById(@Param("id") Integer id);
    List<Student> selectAll();
}

因为我们在mapper接口中自定义了CRUD方法,所以我们需要自行编写它对应的mapper映射文件。

SpringBoot项目中,mapper映射文件有两种存放位置(一种是放在src/main/java目录下;另一种是放在src/main/resources/mapper目录下),我这里存放在第二种情况的位置中,则需要在核心配置文件中指定mybatis-plus映射文件的类路径。

<?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.szh.mybatisplus.mapper.StudentMapper">
    <!-- 使用insert、update、delete、select标签编写sql语句 -->
    <insert id="insertStudent">
        insert into student(name,age,email,status)
        values(#{name},#{age},#{email},#{status})
    </insert>
    <select id="selectStudentById" resultType="com.szh.mybatisplus.entity.Student">
        select id,name,age,email,status
        from student
        where id=#{id}
    </select>
    <select id="selectAll" resultType="com.szh.mybatisplus.entity.Student">
        select id,name,age,email,status
        from student
    </select>
</mapper>
#配置数据库的相关连接信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=12345678
#配置对应的日志信息
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#指定MyBatis-Plus映射文件的路径
mybatis-plus.mapper-locations=classpath:mapper/*.xml

下面是一系列测试方法。首先使用@Resource注解将StudentMapper注入到Spring容器中。

然后我们分别测试在StudentMapper接口中定义的那三个方法。

@Resource
private StudentMapper mapper;
    @Test
    public void testInsert() {
        Student student=new Student();
        student.setName("张三");
        student.setAge(30);
        student.setEmail("zs@163.com");
        student.setStatus(1);
        int rows=mapper.insertStudent(student);
        System.out.println("insert的结果:"+ rows);
    }

    @Test
    public void testSelect() {
        Student student=mapper.selectStudentById(1);
        System.out.println("select的结果:" + student);
    }


    @Test
    public void testSelect2() {
        List<Student> studentList=mapper.selectAll();
        studentList.forEach(student -> System.out.println(student));
    }

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
27天前
|
缓存 Java 数据库连接
我们后端程序员不是操作MyBatis的CRUD Boy
大家好,我是南哥。一个对Java程序员进阶成长颇有研究的人,今天我们接着新的一篇Java进阶指南。为啥都戏称后端是CRUD Boy?难道就因为天天怼着数据库CRUD吗?要我说,是这个岗位的位置要的就是你CRUD,你不得不CRUD。哪有公司天天能给你搭建高并发、高可用、大数据框架的活呢,一条业务线总要成长吧,慢慢成熟了就要装修工来缝缝补补、美化美化,也就是CRUD的活。不能妄自菲薄CRUD Boy,我们是后端工程师。今天来指南下操作数据库之MyBatis框架。
我们后端程序员不是操作MyBatis的CRUD Boy
|
23天前
|
Java 数据库连接 Maven
文本,使用SpringBoot工程创建一个Mybatis-plus项目,Mybatis-plus在编写数据层接口,用extends BaseMapper<User>继承实体类
文本,使用SpringBoot工程创建一个Mybatis-plus项目,Mybatis-plus在编写数据层接口,用extends BaseMapper<User>继承实体类
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
|
21天前
|
XML 开发工具 数据格式
自定义 DSL 流程图(含XML 描述邮件,XML 描述流程图)
自定义 DSL 流程图(含XML 描述邮件,XML 描述流程图)
15 0
自定义 DSL 流程图(含XML 描述邮件,XML 描述流程图)
|
1月前
|
SQL XML 数据库
后端数据库开发高级之通过在xml文件中映射实现动态SQL
后端数据库开发高级之通过在xml文件中映射实现动态SQL
27 3
MybatisPlus--IService接口基本用法,MP提供了Service接口,save(T) 这里的意思是新增了一个T, saveBatch 是批量新增的意思,saveOrUpdate是增或改
MybatisPlus--IService接口基本用法,MP提供了Service接口,save(T) 这里的意思是新增了一个T, saveBatch 是批量新增的意思,saveOrUpdate是增或改
|
21天前
|
XML Java 数据格式
支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法
支付系统----微信支付20---创建案例项目--集成Mybatis-plus的补充,target下只有接口的编译文件,xml文件了,添加日志的写法
接口模板,文本常用的接口Controller层,常用的controller层模板,Mybatisplus的相关配置
接口模板,文本常用的接口Controller层,常用的controller层模板,Mybatisplus的相关配置
|
1月前
|
存储 Java 数据库连接
SSMP整合案例第三步 业务层service开发及基于Mybatis的接口功能拓展
SSMP整合案例第三步 业务层service开发及基于Mybatis的接口功能拓展
15 0
|
2月前
|
算法 Java 数据库连接
Spring+MySQL+数据结构+集合,Alibaba珍藏版mybatis手写文档
Spring+MySQL+数据结构+集合,Alibaba珍藏版mybatis手写文档