开发工具: STS
代码下载链接: GitHub 管理代码 https://github.com/theIndoorTrain/Springboot_MybatisPlus/tree/71ec3e9332642937931092a21ddd58ec28434410
版本:
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
来源: https://www.cnblogs.com/TimerHotel/p/springboot_mp_02.html