日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Mybatis-Plus 学习笔记】2、日志配置及常用 CRUD

發(fā)布時間:2023/12/8 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Mybatis-Plus 学习笔记】2、日志配置及常用 CRUD 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

上一篇文章中我們講了 Mybatis-Plus 的定義以及相關(guān)特點,并從零開始編寫了一個 SpringBoot + Mybatis-Plus 的實例。今天我們就來看看,如何利用 MP 來實現(xiàn)對數(shù)據(jù)庫的增刪改查。

日志配置

使用 MP 時,默認是不打印任何 SQL 語句的。而為了方便日常開發(fā)工作的調(diào)試,我們需要聯(lián)合控制臺和各種數(shù)據(jù)可視化工具進行語句的拼接檢查,因此我們利用 MP 自帶的日志功能,在控制臺輸出我們的 SQL 語句,從而方便我們調(diào)試。

在配置文件 application.yml (IDEA 默認生成的配置文件為 application.properties)中,加入一下配置,這樣 MP 就會在控制臺中打印完整帶參數(shù)的 SQL 語句,方便我們查看。

mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Mapper CRUD 使用方法

首先我們來看 Mapper 層 CRUD 涉及的一些方法,Mapper 層主要繼承自 BaseMapper 接口,里邊實現(xiàn)了各種用于操作數(shù)據(jù)庫的增刪改查的方法,以下我們就來看看日常我們常用的一些方法。

package com.cunyu.employee.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.cunyu.employee.entity.Employee;/*** Created with IntelliJ IDEA.** @author : cunyu* @version : 1.0* @project : Employee* @package : com.cunyu.employee.mapper* @className : EmployeeMapper* @createTime : 2021/8/7 17:45* @description : 員工 Mapper 類*/ public interface EmployeeMapper extends BaseMapper<Employee> { }

insert 操作

首先是插入數(shù)據(jù),insert 方法中,傳入我們所要插入數(shù)據(jù)庫的實體對象作為參數(shù)即可。

  • 方法聲明
/*** 插入一條記錄** @param entity 實體對象*/ int insert(T entity);
  • 插入實例
package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Testvoid contextLoads() {}@Autowiredprivate EmployeeMapper employeeMapper;@Testvoid testInsert() {Employee employee = new Employee();employee.setId(4L);employee.setName("趙六");employee.setSex("男");employee.setEmail("zhaoliu@cunyu1943.com");Assert.assertEquals(1, employeeMapper.insert(employee));System.out.println("插入成功");}}
  • 測試結(jié)果

  • 數(shù)據(jù)插入后的數(shù)據(jù)庫

select 操作

相比于插入數(shù)據(jù)操作,查詢數(shù)據(jù)的方法就要更多,而且還能實現(xiàn)批量查詢和條件查詢。

  • 根據(jù)主鍵查詢
  • 將所要查詢數(shù)據(jù)的主鍵作為參數(shù)傳入我們的 selectById 方法中,即可實現(xiàn)。

    • 方法聲明
    /*** 根據(jù) ID 查詢** @param id 主鍵ID*/ T selectById(Serializable id);
    • 查詢實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Testvoid contextLoads() {}@Autowiredprivate EmployeeMapper employeeMapper;@Testvoid testSelectById() {Employee employee = employeeMapper.selectById(3);System.out.println(employee);} }
    • 測試結(jié)果

  • 根據(jù)主鍵批量查詢
  • 上一個方法每次只能查詢一條記錄,如果我們想要查詢多條數(shù)據(jù)記錄,那么就可以將要查詢數(shù)據(jù)的主鍵列表傳入 selectBatchIds 方法即可。

    • 方法聲明
    /*** 查詢(根據(jù)ID 批量查詢)** @param idList 主鍵ID列表(不能為 null 以及 empty)*/ List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
    • 批量查詢實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Testvoid contextLoads() {}@Autowiredprivate EmployeeMapper employeeMapper;@Testvoid testSelectBatchIds() {List<Integer> ids = new ArrayList<>();ids.add(1);ids.add(4);List<Employee> employeeList = employeeMapper.selectBatchIds(ids);System.out.println(employeeList);} }
    • 測試結(jié)果

  • 根據(jù)多條件查詢
  • 除開支持主鍵查詢外,MP 還支持條件查詢,只要將我們的條件傳入 Map 列表中,然后將其作為 selectByMap 方法的參數(shù)即可,其中傳入 Map 的 key 對應我們數(shù)據(jù)庫中的字段,而 value 則對應字段的值。

    • 方法聲明
    /*** 查詢(根據(jù) columnMap 條件)** @param columnMap 表字段 map 對象*/ List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
    • 條件查詢實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Testvoid contextLoads() {}@Autowiredprivate EmployeeMapper employeeMapper;@Testvoid testSelectByMap() {Map<String, Object> map = new HashMap<>();map.put("sex", "男");map.put("name", "張三");System.out.println(employeeMapper.selectByMap(map));} }
    • 測試結(jié)果

    update 操作

    更新操作,主要是根據(jù)我們數(shù)據(jù)庫的主鍵進行查詢,將對應主鍵的實體對象傳入 updateById 方法即可。

    • 方法聲明
    /*** 根據(jù) ID 修改** @param entity 實體對象*/ int updateById(@Param(Constants.ENTITY) T entity);
    • 更新實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeMapper employeeMapper;@Testvoid testUpdate() {Employee employee = new Employee();employee.setEmail("zhao6@cunyu1943.com");employee.setName("趙 6");employee.setSex("女");employee.setId(4L);Assert.assertEquals(1, employeeMapper.updateById(employee));System.out.println("更新成功");}}
    • 測試結(jié)果

    • 數(shù)據(jù)更新后的數(shù)據(jù)庫

    delete 操作

    刪除操作,既可以根據(jù)主鍵刪除一條記錄,也能根據(jù)主鍵列表實現(xiàn)批量刪除,還能根據(jù)條件來進行刪除。

  • 根據(jù)主鍵刪除一條數(shù)據(jù)
  • 將所要刪除記錄的主鍵作為參數(shù)傳入 deleteById 方法即可。

    • 方法聲明
    /*** 根據(jù) ID 刪除** @param id 主鍵ID*/ int deleteById(Serializable id);
    • 刪除實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeMapper employeeMapper;@Testvoid testDeleteById() {Assert.assertEquals(1, employeeMapper.deleteById(2L));System.out.println("刪除成功");} }
    • 測試結(jié)果

    • 刪除數(shù)據(jù)后的數(shù)據(jù)庫

  • 根據(jù)條件刪除
  • 根據(jù)條件刪除同樣是講條件傳入 Map 中,然后將 Map 作為參數(shù)傳入 deleteByMap 方法,其中 key 對應數(shù)據(jù)庫中的字段,value 對應字段的值。

    • 方法聲明
    /*** 根據(jù) columnMap 條件,刪除記錄** @param columnMap 表字段 map 對象*/ int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
    • 刪除實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeMapper employeeMapper;@Testvoid testDeleteByMap() {Map<String, Object> map = new HashMap<>();map.put("name", "趙 6");Assert.assertEquals(1, employeeMapper.deleteByMap(map));System.out.println("刪除成功");} }
    • 測試結(jié)果

    • 刪除數(shù)據(jù)后的數(shù)據(jù)庫

  • 根據(jù)主鍵批量刪除
  • 將要刪除記錄的主鍵傳入集合中,然后將集合作為 deleteBatchIds 方法的參數(shù)即可。

    • 方法聲明
    /*** 刪除(根據(jù)ID 批量刪除)** @param idList 主鍵ID列表(不能為 null 以及 empty)*/ int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
    • 批量刪除實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeMapper employeeMapper;@Testvoid testDeleteBatchIds() {List<Integer> ids = new ArrayList<>();ids.add(1);ids.add(3);Assert.assertEquals(2, employeeMapper.deleteBatchIds(ids));System.out.println("刪除成功");} }
    • 測試結(jié)果

    • 刪除數(shù)據(jù)后的數(shù)據(jù)庫

    Service CRUD 接口

    Service 層繼承自 IService 接口,其中的方法和 Mapper 層中所提供的方法功能是一致的,除了方法名有所不同外,其他基本類似,但 Service 層中提供了更為豐富的方法,兩者的繼承結(jié)構(gòu)如下圖所示。

    package com.cunyu.employee.service;import com.baomidou.mybatisplus.extension.service.IService; import com.cunyu.employee.entity.Employee;/*** Created with IntelliJ IDEA.** @author : cunyu* @version : 1.0* @project : Employee* @package : com.cunyu.employee.service* @className : EmployeeService* @createTime : 2021/8/8 7:52* @description : 員工服務接口*/ @Service public interface EmployeeService extends IService<Employee> { } package com.cunyu.employee.service.Impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService;/*** Created with IntelliJ IDEA.** @author : cunyu* @version : 1.0* @project : Employee* @package : com.cunyu.employee.service.Impl* @className : EmployeeServiceImpl* @createTime : 2021/8/8 7:53* @description : 員工服務類實現(xiàn)*/@Service public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService { }

    Save

  • 插入一條記錄
  • 功能同 Mapper 層中的 insert 方法,只不過方法名不同。

    • 方法聲明
    // 插入一條記錄(選擇字段,策略插入) boolean save(T entity);
    • 插入實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testSave() {Employee employee = new Employee();employee.setId(5L);employee.setName("周七");employee.setEmail("zhouqi@cunyu1943.com");employee.setSex("女");Assert.assertTrue(employeeService.save(employee));System.out.println("插入成功");} }
    • 測試結(jié)果

    • 插入數(shù)據(jù)后的數(shù)據(jù)庫

  • 批量插入
  • 這里就和 Mapper 層中所有區(qū)別,Mapper 層中只支持單次插入,而 Service 層中支持批量插入,而傳入的參數(shù)就是我們所要傳入實體的集合,而且還可以分批次插入和統(tǒng)一插入。

    2.1 統(tǒng)一插入

    • 方法聲明
    // 插入(批量) boolean saveBatch(Collection<T> entityList);
    • 插入實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList; import java.util.List;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testSaveBatch() {Employee employee1 = new Employee();employee1.setId(6L);employee1.setEmail("zhangliang@cunyu1943.com");employee1.setSex("男");employee1.setName("張良");Employee employee2 = new Employee();employee2.setId(7L);employee2.setEmail("zhouyu@cunyu1943.com");employee2.setName("周瑜");employee2.setSex("男");List<Employee> employeeList = new ArrayList<>();employeeList.add(employee1);employeeList.add(employee2);Assert.assertTrue(employeeService.saveBatch(employeeList));System.out.println("批量插入成功");} }
    • 測試結(jié)果

    • 統(tǒng)一插入后的數(shù)據(jù)庫

    2.2 分批次插入

    • 方法聲明
    // 插入(批量) boolean saveBatch(Collection<T> entityList, int batchSize);
    • 分批次插入實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList; import java.util.List;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testSaveBatch() {Employee employee1 = new Employee();employee1.setId(8L);employee1.setEmail("jialuo@cunyu1943.com");employee1.setSex("女");employee1.setName("迦羅");Employee employee2 = new Employee();employee2.setId(9L);employee2.setEmail("zhugeliang@cunyu1943.com");employee2.setName("諸葛亮");employee2.setSex("男");List<Employee> employeeList = new ArrayList<>();employeeList.add(employee1);employeeList.add(employee2);Assert.assertTrue(employeeService.saveBatch(employeeList,2));System.out.println("批量插入成功");} }
    • 測試結(jié)果

    • 分批次插入后的數(shù)據(jù)庫

    SaveOrUpdate

  • 單條修改插入
    • 方法聲明
    // TableId 注解存在更新記錄,否插入一條記錄 boolean saveOrUpdate(T entity);
    • 單條修改插入實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList; import java.util.List;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testSaveOrUpdate() {Employee employee = new Employee();employee.setId(5L);employee.setName("周武");employee.setEmail("zhouwu@cunyu1943.com");employee.setSex("男");Assert.assertTrue(employeeService.saveOrUpdate(employee));System.out.println("更新成功");} }
    • 測試結(jié)果

    • 修改插入后的數(shù)據(jù)庫

  • 批量修改插入
  • 2.1 統(tǒng)一插入

    • 方法聲明
    // 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList);
    • 統(tǒng)一插入實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList; import java.util.List;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testSaveOrUpdateBatch() {Employee employee1 = new Employee();employee1.setId(10L);employee1.setEmail("zhongwuyan@cunyu1943.com");employee1.setSex("女");employee1.setName("鐘無艷");Employee employee2 = new Employee();employee2.setId(11L);employee2.setEmail("direnjie@cunyu1943.com");employee2.setName("狄仁杰");employee2.setSex("男");List<Employee> employeeList = new ArrayList<>();employeeList.add(employee1);employeeList.add(employee2);Assert.assertTrue(employeeService.saveOrUpdateBatch(employeeList));System.out.println("批量修改插入成功");} }
    • 測試結(jié)果

    • 統(tǒng)一插入數(shù)據(jù)后的數(shù)據(jù)庫

    2.2 分批次插入

    • 方法聲明
    // 批量修改插入 boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
    • 方法實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList; import java.util.List;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testSaveOrUpdateBatch() {Employee employee1 = new Employee();employee1.setId(12L);employee1.setEmail("yuji@cunyu1943.com");employee1.setSex("女");employee1.setName("虞姬");Employee employee2 = new Employee();employee2.setId(13L);employee2.setEmail("sulie@cunyu1943.com");employee2.setName("蘇烈");employee2.setSex("男");List<Employee> employeeList = new ArrayList<>();employeeList.add(employee1);employeeList.add(employee2);Assert.assertTrue(employeeService.saveOrUpdateBatch(employeeList, 2));System.out.println("批量修改插入成功");} }
    • 測試結(jié)果

    • 分批次插入數(shù)據(jù)后的數(shù)據(jù)庫

    Remove

  • 根據(jù) ID 刪除
    • 方法實例
    // 根據(jù) ID 刪除 boolean removeById(Serializable id);
    • 刪除實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testRemoveById() {Assert.assertTrue(employeeService.removeById(5));System.out.println("刪除成功");} }
    • 測試結(jié)果

  • 根據(jù)條件刪除
    • 方法聲明
    // 根據(jù) columnMap 條件,刪除記錄 boolean removeByMap(Map<String, Object> columnMap);
    • 按條件刪除實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.HashMap; import java.util.Map;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testRemoveByMap() {Map<String, Object> map = new HashMap<>();map.put("sex", "女");Assert.assertTrue(employeeService.removeByMap(map));System.out.println("刪除成功");} }
    • 測試結(jié)果

    • 按條件刪除后的數(shù)據(jù)庫

  • 根據(jù) ID 批量刪除
    • 方法聲明
    // 刪除(根據(jù)ID 批量刪除) boolean removeByIds(Collection<? extends Serializable> idList);
    • 批量刪除實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList; import java.util.List;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testRemoveByIds() {List<Integer> ids = new ArrayList<>();ids.add(1);ids.add(4);Assert.assertTrue(employeeService.removeByIds(ids));System.out.println("批量刪除成功");} }
    • 測試結(jié)果

    • 批量刪除后的數(shù)據(jù)庫

    Update

  • 根據(jù) ID 選擇修改
    • 方法聲明
    // 根據(jù) ID 選擇修改 boolean updateById(T entity);
    • 根據(jù) ID 修改實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testUpdateById() {Employee employee = new Employee();employee.setId(3L);employee.setName("程咬金");employee.setSex("男");employee.setEmail("chengyaojin@cunyu1943.com");Assert.assertTrue(employeeService.updateById(employee));System.out.println("更新成功");} }
    • 測試結(jié)果

    • 更新后的數(shù)據(jù)庫

  • 根據(jù) ID 批量更新
  • 2.1 統(tǒng)一更新

    • 方法聲明
    // 根據(jù)ID 批量更新 boolean updateBatchById(Collection<T> entityList);
    • 批量更新實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList; import java.util.List;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testUpdateBatchById() {Employee employee1 = new Employee();employee1.setId(6L);employee1.setName("妲己");employee1.setSex("女");employee1.setEmail("daji@cunyu1943.com");Employee employee2 = new Employee();employee2.setId(13L);employee2.setName("小喬");employee2.setSex("女");employee2.setEmail("xiaoqiao@cunyu1943.com");List<Employee> employeeList = new ArrayList<>();employeeList.add(employee1);employeeList.add(employee2);Assert.assertTrue(employeeService.updateBatchById(employeeList));System.out.println("更新成功");} }
    • 測試結(jié)果

    • 批量更新后的數(shù)據(jù)庫

    2.2 分批次更新

    • 方法聲明
    // 根據(jù)ID 批量更新 boolean updateBatchById(Collection<T> entityList, int batchSize);
    • 分批次更新實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList; import java.util.List;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testUpdateBatchById() {Employee employee1 = new Employee();employee1.setId(7L);employee1.setName("武則天");employee1.setSex("女");employee1.setEmail("wuzetian@cunyu1943.com");Employee employee2 = new Employee();employee2.setId(3L);employee2.setName("李元芳");employee2.setSex("男");employee2.setEmail("liyuanfang@cunyu1943.com");List<Employee> employeeList = new ArrayList<>();employeeList.add(employee1);employeeList.add(employee2);Assert.assertTrue(employeeService.updateBatchById(employeeList, 2));System.out.println("更新成功");} }
    • 測試結(jié)果

    • 分批次更新后的數(shù)據(jù)庫

    Get

  • 根據(jù) ID 查詢
  • 將所要查詢記錄的 id 作為參數(shù),然后將查詢到的實體返回。

    • 方法聲明
    // 根據(jù) ID 查詢 T getById(Serializable id);
    • 查詢實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testGetById() {Employee employee = employeeService.getById(9);System.out.println(employee);} }
    • 測試結(jié)果

    List

  • 查詢所有
  • 查詢所有記錄,然后返回到一個集合中。

    • 方法聲明
    // 查詢所有 List<T> list();
    • 查詢實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList; import java.util.List;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testList() {List<Employee> employeeLists = new ArrayList<>();employeeLists = employeeService.list();Assert.assertEquals(6, employeeLists.size());System.out.println("查詢成功");} }
    • 測試結(jié)果

  • 根據(jù) ID 批量查詢
  • 講所要查詢的記錄 id 傳入集合,然后座位方法參數(shù),最后返回查詢到的結(jié)果到一個集合中。

    • 方法聲明
    // 查詢(根據(jù)ID 批量查詢) Collection<T> listByIds(Collection<? extends Serializable> idList);
    • 批量查詢實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.ArrayList; import java.util.List;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testListByIds() {List<Long> ids = new ArrayList<>();ids.add(6L);ids.add(7L);Assert.assertEquals(2, employeeService.listByIds(ids).size());System.out.println("查詢成功");} }
    • 測試結(jié)果

  • 根據(jù)條件查詢
  • 條件傳入 Map 集合,key 對應字段,value 對應值,然后返回集合。

    • 方法聲明
    // 查詢(根據(jù) columnMap 條件) Collection<T> listByMap(Map<String, Object> columnMap);
    • 根據(jù)條件查詢實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import java.util.HashMap; import java.util.Map;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testListByMap() {Map<String, Object> map = new HashMap<>();map.put("sex", "女");Assert.assertEquals(3, employeeService.listByMap(map).size());System.out.println("查詢成功");} }
    • 測試結(jié)果

  • 查詢所有列表
    • 方法聲明
    // 查詢所有列表 List<Map<String, Object>> listMaps();
    • 查詢實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testListMaps() {Assert.assertEquals(6, employeeService.listMaps());System.out.println("查詢成功");} }
    • 測試結(jié)果

  • 查詢所有記錄
  • 用于查詢所有數(shù)據(jù)記錄,并將其返回到一個集合中。

    • 方法聲明
    // 查詢?nèi)坑涗?/span> List<Object> listObjs();
    • 查詢實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testListObjs() {Assert.assertEquals(6, employeeService.listObjs().size());System.out.println("查詢成功");} }
    • 測試結(jié)果

    Count

  • 查詢記錄總數(shù)
  • 用于統(tǒng)計數(shù)據(jù)控中的記錄總條數(shù),方法返回記錄條數(shù)。

    • 方法聲明
    // 查詢總記錄數(shù) int count();
    • 查詢記錄總數(shù)實例
    package com.cunyu.employee;import com.cunyu.employee.entity.Employee; import com.cunyu.employee.mapper.EmployeeMapper; import com.cunyu.employee.service.EmployeeService; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class EmployeeApplicationTests {@Autowiredprivate EmployeeService employeeService;@Testvoid testCount() {Assert.assertEquals(6, employeeService.count());System.out.println("查詢成功");} }
    • 測試結(jié)果

    總結(jié)

    好了,以上就是關(guān)于 Mybatis-Plus 的日志配置以及如何進行 CRUD 的相關(guān)內(nèi)容了,這里 CRUD 主要又分為 Mapper 層和 Service 層,我們可以根據(jù)自己的需要進行選擇。當然,在我們?nèi)粘J褂弥?#xff0c;常常都是兩個接口一起使用,關(guān)于更多 MP 的使用技巧,我們下期文章再見!

    總結(jié)

    以上是生活随笔為你收集整理的【Mybatis-Plus 学习笔记】2、日志配置及常用 CRUD的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。