2、SpringBoot+MybatisPlus整合-------BaseCRUD












开发工具:STS
代码下载链接:GitHub管理代码
版本:
Springboot:1.5.14.RELEASE
使用2.0以上的Springboot,会报出一些异常。欢迎知道异常原因的大牛解惑。
MybatisPlus:2.3
前言:
MP扩展了Mybatis对实体的一些简单的CRUD操作,我们不需要再去实现,只要在mapper接口中继承BaseMapper就可以了。
搭建框架可以参考:1、SpringBoot+MybatisPlus整合
开始测试:
BaseMapper<T>:
/**
* Copyright (c) 2011-2020, hubin (jobob@qq.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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; /**
* <p>
* Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
* </p>
* <p>
* 这个 Mapper 支持 id 泛型
* </p>
*
* @author hubin
* @Date 2016-01-23
*/
public interface BaseMapper<T> { /**
* <p>
* 插入一条记录
* </p>
*
* @param entity 实体对象
* @return int
*/
Integer insert(T entity); /**
* <p>
* 插入一条记录
* </p>
*
* @param entity 实体对象
* @return int
*/
Integer insertAllColumn(T entity); /**
* <p>
* 根据 ID 删除
* </p>
*
* @param id 主键ID
* @return int
*/
Integer deleteById(Serializable id); /**
* <p>
* 根据 columnMap 条件,删除记录
* </p>
*
* @param columnMap 表字段 map 对象
* @return int
*/
Integer deleteByMap(@Param("cm") Map<String, Object> columnMap); /**
* <p>
* 根据 entity 条件,删除记录
* </p>
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return int
*/
Integer delete(@Param("ew") Wrapper<T> wrapper); /**
* <p>
* 删除(根据ID 批量删除)
* </p>
*
* @param idList 主键ID列表
* @return int
*/
Integer deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList); /**
* <p>
* 根据 ID 修改
* </p>
*
* @param entity 实体对象
* @return int
*/
Integer updateById(@Param("et") T entity); /**
* <p>
* 根据 ID 修改
* </p>
*
* @param entity 实体对象
* @return int
*/
Integer updateAllColumnById(@Param("et") T entity); /**
* <p>
* 根据 whereEntity 条件,更新记录
* </p>
*
* @param entity 实体对象
* @param wrapper 实体对象封装操作类(可以为 null)
* @return
*/
Integer update(@Param("et") T entity, @Param("ew") Wrapper<T> wrapper); /**
* <p>
* 根据 whereEntity 条件,更新记录
* </p>
*
* @param setStr set字符串
* @param wrapper 实体对象封装操作类(可以为 null)
* @return
*/
Integer updateForSet(@Param("setStr") String setStr, @Param("ew") Wrapper<T> wrapper); /**
* <p>
* 根据 ID 查询
* </p>
*
* @param id 主键ID
* @return T
*/
T selectById(Serializable id); /**
* <p>
* 查询(根据ID 批量查询)
* </p>
*
* @param idList 主键ID列表
* @return List<T>
*/
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList); /**
* <p>
* 查询(根据 columnMap 条件)
* </p>
*
* @param columnMap 表字段 map 对象
* @return List<T>
*/
List<T> selectByMap(@Param("cm") Map<String, Object> columnMap); /**
* <p>
* 根据 entity 条件,查询一条记录
* </p>
*
* @param entity 实体对象
* @return T
*/
T selectOne(@Param("ew") T entity); /**
* <p>
* 根据 Wrapper 条件,查询总记录数
* </p>
*
* @param wrapper 实体对象
* @return int
*/
Integer selectCount(@Param("ew") Wrapper<T> wrapper); /**
* <p>
* 根据 entity 条件,查询全部记录
* </p>
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return List<T>
*/
List<T> selectList(@Param("ew") Wrapper<T> wrapper); /**
* <p>
* 根据 Wrapper 条件,查询全部记录
* </p>
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return List<T>
*/
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> wrapper); /**
* <p>
* 根据 Wrapper 条件,查询全部记录
* 注意: 只返回第一个字段的值
* </p>
*
* @param wrapper 实体对象封装操作类(可以为 null)
* @return List<Object>
*/
List<Object> selectObjs(@Param("ew") Wrapper<T> wrapper); /**
* <p>
* 根据 entity 条件,查询全部记录(并翻页)
* </p>
*
* @param rowBounds 分页查询条件(可以为 RowBounds.DEFAULT)
* @param wrapper 实体对象封装操作类(可以为 null)
* @return List<T>
*/
List<T> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper); /**
* <p>
* 根据 Wrapper 条件,查询全部记录(并翻页)
* </p>
*
* @param rowBounds 分页查询条件(可以为 RowBounds.DEFAULT)
* @param wrapper 实体对象封装操作类
* @return List<Map<String, Object>>
*/
List<Map<String, Object>> selectMapsPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper); }
我们来测试它提供的CRUD(参数里带有Wrapper的先跳过,涉及条件构造器,我们之后再探讨)
为了方便查看自动生成的sql语句,我们配置log日志级别:
logging:
level:
com:
xm:
mapper: trace
1.Select
| 方法名 | 解释 |
| selectById() | 根据主键ID查询,参数类型为:Serializable |
| selectOne() | 根据实体里面有值的属性进行查询,参数类型为实体类型,查询结果只有一个 |
| selectByMap() | 根据map封装的字段属性进行查询,参数类型为map,结果集为List |
| selectBatchIds() | 根据多个id生成的集合进行查询,参数类型为Connection,结果集为List |
代码:
package com.xm; import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map; 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 com.xm.mapper.StudentMapper;
import com.xm.pojo.Student; @RunWith(SpringRunner.class)
@SpringBootTest
public class StudentText { @Autowired
private StudentMapper studentMapper; @Test
/**
* 查询测试
*/
public void selectTest() {
/**
* selectById()
* @param Serializable id:主键ID
* sql语句:SELECT id,`name` FROM student WHERE id=?
* return: Student
*/
/*Student student = studentMapper.selectById(2);
System.out.println(student);*/ /**
* selectOne()
* @param Student entity: 学生实体
* sql语句:SELECT id,`name` FROM student WHERE `name`=?
* return: Student
*/
/*Student student = new Student();
student.setName("郭小明");
Student student2 = studentMapper.selectOne(student);
System.out.println(student2);*/ /**
* selectByMap()
* @param Map<String, Object> columnMap: 字段名为key,值为value的map
* sql语句:SELECT id,`name` FROM student WHERE name = ? AND id = ?
* return: List
*/
/*Map<String,Object> map = new HashMap<>();
map.put("name", "郭小明");
map.put("id", "2");
List<Student> students = studentMapper.selectByMap(map);
System.out.println(students);*/ /**
* selectBatchIds()
* @param Collection<? extends Serializable> idList:id组成的集合
* sql语句:SELECT id,`name` FROM student WHERE id IN ( ? , ? , ? , ? )
* return: List
*/
List<Student> stutents = studentMapper.selectBatchIds(Arrays.asList(1,2,3,4));
System.out.println(stutents);
} }
2.update
| 方法名 | 解释 |
| updateById() | 根据实体id更新实体中有值的字段,无值的属性不做修改,除id外必须含有一个属性不为null,否则执行错误 |
| updateAllColumnById() | 根据实体id更新实体中所有字段,属性为null的直接更新为null |
代码:
package com.xm; import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map; 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 com.xm.mapper.StudentMapper;
import com.xm.pojo.Student; @RunWith(SpringRunner.class)
@SpringBootTest
public class StudentText { @Autowired
private StudentMapper studentMapper; @Test
/**
* 更新测试
*/
public void updateTest() { /**
*updateById()
* @param Student entity:有id的学生实体
* sql语句:UPDATE student SET `name`=? WHERE id=?
* return: Integer
*/
/*Student student = new Student();
student.setId(23);
student.setName("寄生虫");
Integer num = studentMapper.updateById(student);
System.out.println("更新行数:"+num);*/ /**
*updateAllColumnById()
* @param Student entity:有id的学生实体
* sql语句:UPDATE student SET `name`=? WHERE id=?
* return: Integer
*/
Student student = new Student();
student.setId(22);
//student.setName("寄生虫");
Integer num = studentMapper.updateAllColumnById(student);
System.out.println("更新行数:"+num);
} }
3.delete
| 方法 | 解释 |
| deleteById() | 根据id进行删除 |
| deleteBatchId() | 根据id集合进行删除 |
| deleteByMap() | 根据属性名和值封装的map进行删除 |
代码:
package com.xm; import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map; 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 com.xm.mapper.StudentMapper;
import com.xm.pojo.Student; @RunWith(SpringRunner.class)
@SpringBootTest
public class StudentText { @Autowired
private StudentMapper studentMapper; @Test
/**
* 删除测试
*/
public void deleteTest() {
/**
*deleteById()
* @param Serializable id:学生id
* sql语句:DELETE FROM student WHERE id=?
* return: Integer
*/
/*Integer num = studentMapper.deleteById(22);
System.out.println("更新行数:"+num);*/ /**
*deleteBatchId()
* @param Collection<? extends Serializable> idList:id组成的集合
* sql语句:DELETE FROM student WHERE id IN ( ? , ? , ? )
* return: Integer
*/
/*Integer num = studentMapper.deleteBatchIds(Arrays.asList(18,19,20));
System.out.println(num);*/ /**
*deleteByMap()
* @param Map<String, Object> columnMap: 字段名为key,值为value的map
* sql语句:DELETE FROM student WHERE name = ? AND id = ?
* return: Integer
*/
Map<String,Object> map = new HashMap<>();
map.put("name", "张大萨");
map.put("id", "21");
Integer num = studentMapper.deleteByMap(map);
System.out.println(num);
} }
4.insert
| 方法 | 解析 |
| insert() | 插入一个实体,必须包含至少一个属性不为null,否则添加失败 |
| insertAllColumn() | 插入一个实体,属性可以全部为空 |
注意:添加方法里要注意主键自动生成策略是否和数据库的一致。
原因分析:主键生成策略可能不一致
解决策略:1.实体主键上添加注解@TableId(type=IdType.AUTO)
2.全局配置:
id-type: #主键类型 0:"数据库ID自增",
1:"用户输入ID",
2:"全局唯一ID (数字类型唯一ID)",
3:"全局唯一ID UUID";
代码:
package com.xm; import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map; 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 com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.enums.IdType;
import com.xm.mapper.StudentMapper;
import com.xm.pojo.Student; @RunWith(SpringRunner.class)
@SpringBootTest
public class StudentText { @Autowired
private StudentMapper studentMapper; @Test
/**
* 添加测试
*/
public void insertTest() { /**
* 测试失败:
* 原因分析:主键生成策略可能不一致
* 解决策略:1.实体主键上添加注解@TableId(type=IdType.AUTO)
* 2.全局配置:
* id-type: #主键类型 0:"数据库ID自增",
* 1:"用户输入ID",
* 2:"全局唯一ID (数字类型唯一ID)",
* 3:"全局唯一ID UUID";
*/ /**
*insert()
* @param Student entity:学生实体
* sql语句:INSERT INTO student ( `name` ) VALUES ( ? )
* return: Integer
*/
/*Student student = new Student();
student.setName("烤鸡翅");
Integer num = studentMapper.insert(student);
System.out.println("更新行数:"+num);*/ /**
*insertAllColumn()
* @param Student entity:学生实体
* sql语句:INSERT INTO student ( `name` ) VALUES ( ? )
* return: Integer
*/
Student student = new Student();
//student.setName("烤鸡翅");
Integer num = studentMapper.insertAllColumn(student);
System.out.println("更新行数:"+num); } }
2018-07-19
2、SpringBoot+MybatisPlus整合-------BaseCRUD的更多相关文章
- SpringBoot+Mybatis-Plus整合Sharding-JDBC5.1.1实现单库分表【全网最新】
一.前言 小编最近一直在研究关于分库分表的东西,前几天docker安装了mycat实现了分库分表,但是都在说mycat的bug很多.很多人还是倾向于shardingsphere,其实他是一个全家桶,有 ...
- SpringBoot+MyBatisPlus整合时提示:Invalid bound statement(not found):**.dao.UserDao.queryById
场景 在使用SpringBoot+MyBatisPlus搭建后台启动项目时,使用EasyCode自动生成代码. 在访问后台接口时提示: Invilid bound statement (not fou ...
- 3、SpringBoot+MybatisPlus整合-------代码生成器
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</art ...
- 1、SpringBoot+MybatisPlus整合
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- SpringBoot Mybatis-Plus 整合 dynamic-datasource-spring-boot-starter 对数据库进行读写分离
准备工作 对 MySql 进行主从搭建 引入 dynamic-datasource-spring-boot-starter 坐标 引入 druid-spring-boot-starter 坐标 对应框 ...
- SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作
SpringBoot+Mybatis+MybatisPlus整合实现基本的CRUD操作 1> 数据准备 -- 创建测试表 CREATE TABLE `tb_user` ( `id` ) NOT ...
- SpringBoot系列——MyBatis-Plus整合封装
前言 MyBatis-Plus是一款MyBatis的增强工具(简称MP),为简化开发.提高效率,但我们并没有直接使用MP的CRUD接口,而是在原来的基础上封装一层通用代码,单表继承我们的通用代码,实现 ...
- IDEA上创建 Maven SpringBoot+mybatisplus+thymeleaf 项目
概述 在WEB领域,Java也是在不断的探索和改进,从开始的JSP--->Struts1--->Struts2+Spring--->Spring MVC--->SpringBo ...
- 学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用
学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用 一.SpringBoot系列教程 二.SpringBoot ...
随机推荐
- Transition FrameWork
Android Transition Framework可以实现三种效果: 不同Activity之间切换时,Activityc的内容(contentView)转场动画 不同Activity之间切换时, ...
- qt安装
在以下网页选择一个国内的下载地址即可 http://download.qt.io/official_releases/qt/5.7/5.7.0/qt-opensource-linux-x64-5.7. ...
- 使用Xshell连接服务器
转载原地址:http://www.server110.com/linux/201308/830.html 1)关于Xshell 网上更多的资料里提到的SSH客户端是putty,因为简单.开源.免费.但 ...
- 聊一聊IAR的workspace文件组织
抽空偶尔做个zigbee实验其实也挺好玩的,今天我就来总结一下嵌入式IAR Embedded Workbench这个非常有效的集成开发环境的文件组织. 每一个workspace由一般是由.c文件和一个 ...
- Unity3D跨平台动态库编译---记kcp基于CMake的各平台构建实践
一 为什么需要动态库 1)提供原生代码(native code)的支持,也叫原生插件,但是我实践的是c/cpp跨平台动态库,这里不具体涉及安卓平台java库和ios平台的objectc库构建. 2)某 ...
- 详解在Hibernate中配置数据库方言的作用和好处以及各种数据库的方言连接
Hibernate底层依然使用SQL语句来执行数据库操作,虽然所有关系型数据库都支持使用标准SQL语句,但所有数据库都对标准SQL进行了一些扩展,所以在语法细节上存在一些差异,因此Hibernate需 ...
- C#中StreamReader读取中文文本出现乱码的解决方法
在编写文本文件读写程序的过程中,有如下代码 StreamReader sr = new StreamReader(FileName); 结果发现打开中文文本文件出现乱码. 究其原因,原来自从Windo ...
- 介绍几个关于C/C++程序调试的函数
最近调试程序学到的几个挺有用的函数,分享一下,希望对用C/C++的朋友有所帮助! 1. 调用栈系列下面是函数原型: 1 2 3 4 #include "execinfo .h" i ...
- Day4上午
expect100+50+50, In fact 100+10+0. 代码能力还有待提高,部分分应该能拿的.结果...力不从心啊. T1 贪心做的不知对不对. 看来思路是对的,不知道能不能对. 暴力做 ...
- 使用Aspose.Cell控件实现Excel高难度报表的生成
1.使用Aspose.Cell控件实现Excel高难度报表的生成(一) http://www.cnblogs.com/wuhuacong/archive/2011/02/23/1962147.html ...