開發工具: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日志級別:
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