2、SpringBoot+MybatisPlus整合-------BaseCRUD
开发工具:STS
代码下载链接:
版本:
Springboot:1.5.14.RELEASE
使用2.0以上的Springboot,会报出一些异常。欢迎知道异常原因的大牛解惑。
MybatisPlus:2.3
前言:
MP扩展了Mybatis对实体的一些简单的CRUD操作,我们不需要再去实现,只要在mapper接口中继承BaseMapper就可以了。
开始测试:
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日志级别:
1 logging: 2 level: 3 com: 4 xm: 5 mapper: trace
1.Select
方法名 | 解释 |
selectById() | 根据主键ID查询,参数类型为:Serializable |
selectOne() | 根据实体里面有值的属性进行查询,参数类型为实体类型,查询结果只有一个 |
selectByMap() | 根据map封装的字段属性进行查询,参数类型为map,结果集为List |
selectBatchIds() | 根据多个id生成的集合进行查询,参数类型为Connection,结果集为List |
代码:
1 package com.xm; 2 3 import java.util.Arrays; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.boot.test.context.SpringBootTest; 12 import org.springframework.test.context.junit4.SpringRunner; 13 14 import com.xm.mapper.StudentMapper; 15 import com.xm.pojo.Student; 16 17 @RunWith(SpringRunner.class) 18 @SpringBootTest 19 public class StudentText { 20 21 @Autowired 22 private StudentMapper studentMapper; 23 24 @Test 25 /** 26 * 查询测试 27 */ 28 public void selectTest() { 29 /** 30 * selectById() 31 * @param Serializable id:主键ID 32 * sql语句:SELECT id,`name` FROM student WHERE id=? 33 * return: Student 34 */ 35 /*Student student = studentMapper.selectById(2); 36 System.out.println(student);*/ 37 38 /** 39 * selectOne() 40 * @param Student entity: 学生实体 41 * sql语句:SELECT id,`name` FROM student WHERE `name`=? 42 * return: Student 43 */ 44 /*Student student = new Student(); 45 student.setName("郭小明"); 46 Student student2 = studentMapper.selectOne(student); 47 System.out.println(student2);*/ 48 49 /** 50 * selectByMap() 51 * @param Map<String, Object> columnMap: 字段名为key,值为value的map 52 * sql语句:SELECT id,`name` FROM student WHERE name = ? AND id = ? 53 * return: List 54 */ 55 /*Map<String,Object> map = new HashMap<>(); 56 map.put("name", "郭小明"); 57 map.put("id", "2"); 58 List<Student> students = studentMapper.selectByMap(map); 59 System.out.println(students);*/ 60 61 /** 62 * selectBatchIds() 63 * @param Collection<? extends Serializable> idList:id组成的集合 64 * sql语句:SELECT id,`name` FROM student WHERE id IN ( ? , ? , ? , ? ) 65 * return: List 66 */ 67 List<Student> stutents = studentMapper.selectBatchIds(Arrays.asList(1,2,3,4)); 68 System.out.println(stutents); 69 } 70 71 }
2.update
方法名 | 解释 |
updateById() | 根据实体id更新实体中有值的字段,无值的属性不做修改,除id外必须含有一个属性不为null,否则执行错误 |
updateAllColumnById() | 根据实体id更新实体中所有字段,属性为null的直接更新为null |
代码:
1 package com.xm; 2 3 import java.util.Arrays; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.boot.test.context.SpringBootTest; 12 import org.springframework.test.context.junit4.SpringRunner; 13 14 import com.xm.mapper.StudentMapper; 15 import com.xm.pojo.Student; 16 17 @RunWith(SpringRunner.class) 18 @SpringBootTest 19 public class StudentText { 20 21 @Autowired 22 private StudentMapper studentMapper; 23 24 @Test 25 /** 26 * 更新测试 27 */ 28 public void updateTest() { 29 30 /** 31 *updateById() 32 * @param Student entity:有id的学生实体 33 * sql语句:UPDATE student SET `name`=? WHERE id=? 34 * return: Integer 35 */ 36 /*Student student = new Student(); 37 student.setId(23); 38 student.setName("寄生虫"); 39 Integer num = studentMapper.updateById(student); 40 System.out.println("更新行数:"+num);*/ 41 42 /** 43 *updateAllColumnById() 44 * @param Student entity:有id的学生实体 45 * sql语句:UPDATE student SET `name`=? WHERE id=? 46 * return: Integer 47 */ 48 Student student = new Student(); 49 student.setId(22); 50 //student.setName("寄生虫"); 51 Integer num = studentMapper.updateAllColumnById(student); 52 System.out.println("更新行数:"+num); 53 } 54 55 56 57 }
3.delete
方法 | 解释 |
deleteById() | 根据id进行删除 |
deleteBatchId() | 根据id集合进行删除 |
deleteByMap() | 根据属性名和值封装的map进行删除 |
代码:
1 package com.xm; 2 3 import java.util.Arrays; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.boot.test.context.SpringBootTest; 12 import org.springframework.test.context.junit4.SpringRunner; 13 14 import com.xm.mapper.StudentMapper; 15 import com.xm.pojo.Student; 16 17 @RunWith(SpringRunner.class) 18 @SpringBootTest 19 public class StudentText { 20 21 @Autowired 22 private StudentMapper studentMapper; 23 24 @Test 25 /** 26 * 删除测试 27 */ 28 public void deleteTest() { 29 /** 30 *deleteById() 31 * @param Serializable id:学生id 32 * sql语句:DELETE FROM student WHERE id=? 33 * return: Integer 34 */ 35 /*Integer num = studentMapper.deleteById(22); 36 System.out.println("更新行数:"+num);*/ 37 38 /** 39 *deleteBatchId() 40 * @param Collection<? extends Serializable> idList:id组成的集合 41 * sql语句:DELETE FROM student WHERE id IN ( ? , ? , ? ) 42 * return: Integer 43 */ 44 /*Integer num = studentMapper.deleteBatchIds(Arrays.asList(18,19,20)); 45 System.out.println(num);*/ 46 47 /** 48 *deleteByMap() 49 * @param Map<String, Object> columnMap: 字段名为key,值为value的map 50 * sql语句:DELETE FROM student WHERE name = ? AND id = ? 51 * return: Integer 52 */ 53 Map<String,Object> map = new HashMap<>(); 54 map.put("name", "张大萨"); 55 map.put("id", "21"); 56 Integer num = studentMapper.deleteByMap(map); 57 System.out.println(num); 58 } 59 60 61 62 }
4.insert
方法 | 解析 |
insert() | 插入一个实体,必须包含至少一个属性不为null,否则添加失败 |
insertAllColumn() | 插入一个实体,属性可以全部为空 |
注意:添加方法里要注意主键自动生成策略是否和数据库的一致。
原因分析:主键生成策略可能不一致
解决策略:1.实体主键上添加注解@TableId(type=IdType.AUTO)
2.全局配置:
id-type: #主键类型 0:”数据库ID自增”,
1:”用户输入ID”,
2:”全局唯一ID (数字类型唯一ID)”,
3:”全局唯一ID UUID”;
代码:
1 package com.xm; 2 3 import java.util.Arrays; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.boot.test.context.SpringBootTest; 12 import org.springframework.test.context.junit4.SpringRunner; 13 14 import com.baomidou.mybatisplus.annotations.TableId; 15 import com.baomidou.mybatisplus.enums.IdType; 16 import com.xm.mapper.StudentMapper; 17 import com.xm.pojo.Student; 18 19 @RunWith(SpringRunner.class) 20 @SpringBootTest 21 public class StudentText { 22 23 @Autowired 24 private StudentMapper studentMapper; 25 26 @Test 27 /** 28 * 添加测试 29 */ 30 public void insertTest() { 31 32 /** 33 * 测试失败: 34 * 原因分析:主键生成策略可能不一致 35 * 解决策略:1.实体主键上添加注解@TableId(type=IdType.AUTO) 36 * 2.全局配置: 37 * id-type: #主键类型 0:"数据库ID自增", 38 * 1:"用户输入ID", 39 * 2:"全局唯一ID (数字类型唯一ID)", 40 * 3:"全局唯一ID UUID"; 41 */ 42 43 /** 44 *insert() 45 * @param Student entity:学生实体 46 * sql语句:INSERT INTO student ( `name` ) VALUES ( ? ) 47 * return: Integer 48 */ 49 /*Student student = new Student(); 50 student.setName("烤鸡翅"); 51 Integer num = studentMapper.insert(student); 52 System.out.println("更新行数:"+num);*/ 53 54 /** 55 *insertAllColumn() 56 * @param Student entity:学生实体 57 * sql语句:INSERT INTO student ( `name` ) VALUES ( ? ) 58 * return: Integer 59 */ 60 Student student = new Student(); 61 //student.setName("烤鸡翅"); 62 Integer num = studentMapper.insertAllColumn(student); 63 System.out.println("更新行数:"+num); 64 65 } 66 67 68 }
2018-07-19