提升开发效率的一款mybatis开发神器
点击上方 好好学java ,选择 星标 公众号
重磅资讯、干货,第一时间送达
重磅资讯、干货,第一时间送达
今日推荐:硬刚一周,3W字总结,一年的经验告诉你如何准备校招!
个人原创100W+访问量博客:点击前往,查看更多
个人原创100W+访问量博客:点击前往,查看更多
以前在开发的时候,使用mybatis的时候,经常都需要先配置xml映射文件,然后每条sql操作都需要自己进行手动编写,对于一些复杂的sql这么来操作确实有必要,但是如果只是一些非常简单的insert,update,delete,select这类型的语句而言,也需要开发人员花费额外的时间进行手动编写的话,确实费时又费力。
能否为mybatis特别定制一套能够自动为我们生成一些简单sql功能,同时又支持我们进行自定义sql设置功能的强大框架呢?
mybatis plus因此诞生了。

mybatis plus是一款专门针对于传统MyBatis开发中sql需要手动进行映射配置繁琐缺点的一款框架技术,这款框架技术提供了十分丰富的api供开发者们使用,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
Mybatis plus到底有多方便呢,我们闲话不多说,直接上手代码实例来进行演示:
首先我们需要导入一些相关的pom依赖配置
<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">
<modelVersion>4.0.0modelVersion>
<groupId>com.sisegroupId>
<artifactId>mybatis-plusartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatisplus-spring-boot-starterartifactId>
<version>1.0.5version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plusartifactId>
<version>2.3version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
导入了jar包之后,为了方便测试,我们首先在数据库里面搭建相关的数据表信息
CREATE TABLE `teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`teacher_name` varchar(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`teacher_pwd` varchar(60) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=gbk;
该表对应的实体类:
package com.sise.model;
import com.baomidou.mybatisplus.annotations.TableName;
/**
* @author idea
* @data 2019/5/24
*/
@TableName(value = "teacher")
public class Teacher {
private int id;
private String teacherName;
private String teacherPwd;
public int getId() {
return id;
}
public Teacher() {
}
public Teacher(int id) {
this.id = id;
}
public Teacher setId(int id) {
this.id = id;
return this;
}
public String getTeacherName() {
return teacherName;
}
public Teacher setTeacherName(String teacherName) {
this.teacherName = teacherName;
return this;
}
public String getTeacherPwd() {
return teacherPwd;
}
public Teacher setTeacherPwd(String teacherPwd) {
this.teacherPwd = teacherPwd;
return this;
}
@Override
public String toString() {
return "Teacher{" +
"id=" + id +
", teacherName='" + teacherName + '\'' +
", teacherPwd='" + teacherPwd + '\'' +
'}';
}
}
通常我们在开发的时候都会自定义一个Dao层,mybatis plus里面提供了一个叫做BaseMapper的接口,内部已经提供了相当多的crud操作函数的封装。可以来仔细查看一下该接口的内容:
/**
* Copyright (c) 2011-2020, hubin (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.mapper;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
/**
*
* Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
*
*
* 这个 Mapper 支持 id 泛型
*
*
* @author hubin
* @Date 2016-01-23
*/
public interface BaseMapper<T> {
/**
*
* 插入一条记录
*
*
* @param entity 实体对象
* @return int
*/
Integer insert(T entity);
/**
*
* 插入一条记录
*
*
* @param entity 实体对象
* @return int
*/
Integer insertAllColumn(T entity);
/**
*
* 根据 ID 删除
*
*
* @param id 主键ID
* @return int
*/
Integer deleteById(Serializable id);
/**
*
* 根据 columnMap 条件,删除记录
*
*
* @param columnMap 表字段 map 对象
* @return int
*/
Integer deleteByMap(@Param("cm") Map columnMap);
/**
*
* 根据 entity 条件,删除记录
*
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return int
*/
Integer delete(@Param("ew") Wrapper wrapper);
/**
*
* 删除(根据ID 批量删除)
*
*
* @param idList 主键ID列表
* @return int
*/
Integer deleteBatchIds(@Param("coll") Collection idList);
/**
*
* 根据 ID 修改
*
*
* @param entity 实体对象
* @return int
*/
Integer updateById(@Param("et") T entity);
/**
*
* 根据 ID 修改
*
*
* @param entity 实体对象
* @return int
*/
Integer updateAllColumnById(@Param("et") T entity);
/**
*
* 根据 whereEntity 条件,更新记录
*
*
* @param entity 实体对象
* @param wrapper 实体对象封装操作类(可以为 null)
* @return
*/
Integer update(@Param("et") T entity, @Param("ew") Wrapper wrapper);
/**
*
* 根据 whereEntity 条件,更新记录
*
*
* @param setStr set字符串
* @param wrapper 实体对象封装操作类(可以为 null)
* @return
*/
Integer updateForSet(@Param("setStr") String setStr, @Param("ew") Wrapper wrapper);
/**
*
* 根据 ID 查询
*
*
* @param id 主键ID
* @return T
*/
T selectById(Serializable id);
/**
*
* 查询(根据ID 批量查询)
*
*
* @param idList 主键ID列表
* @return List
*/
List selectBatchIds(@Param("coll") Collection idList);
/**
*
* 查询(根据 columnMap 条件)
*
*
* @param columnMap 表字段 map 对象
* @return List
*/
List selectByMap(@Param("cm") Map columnMap);
/**
*
* 根据 entity 条件,查询一条记录
*
*
* @param entity 实体对象
* @return T
*/
T selectOne(@Param("ew") T entity);
/**
*
* 根据 Wrapper 条件,查询总记录数
*
*
* @param wrapper 实体对象
* @return int
*/
Integer selectCount(@Param("ew") Wrapper wrapper);
/**
*
* 根据 entity 条件,查询全部记录
*
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return List
*/
List selectList(@Param("ew") Wrapper wrapper);
/**
*
* 根据 Wrapper 条件,查询全部记录
*
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return List
*/
List