生活随笔
收集整理的這篇文章主要介紹了
【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;
public interface EmployeeMapper extends BaseMapper<Employee> {
}
insert 操作
首先是插入數(shù)據(jù),insert 方法中,傳入我們所要插入數(shù)據(jù)庫的實體對象作為參數(shù)即可。
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("插入成功");}}
- 數(shù)據(jù)插入后的數(shù)據(jù)庫
select 操作
相比于插入數(shù)據(jù)操作,查詢數(shù)據(jù)的方法就要更多,而且還能實現(xiàn)批量查詢和條件查詢。
根據(jù)主鍵查詢
將所要查詢數(shù)據(jù)的主鍵作為參數(shù)傳入我們的 selectById 方法中,即可實現(xiàn)。
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
);}
}
根據(jù)主鍵批量查詢
上一個方法每次只能查詢一條記錄,如果我們想要查詢多條數(shù)據(jù)記錄,那么就可以將要查詢數(shù)據(jù)的主鍵列表傳入 selectBatchIds 方法即可。
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
);}
}
根據(jù)多條件查詢
除開支持主鍵查詢外,MP 還支持條件查詢,只要將我們的條件傳入 Map 列表中,然后將其作為 selectByMap 方法的參數(shù)即可,其中傳入 Map 的 key 對應我們數(shù)據(jù)庫中的字段,而 value 則對應字段的值。
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
));}
}
update 操作
更新操作,主要是根據(jù)我們數(shù)據(jù)庫的主鍵進行查詢,將對應主鍵的實體對象傳入 updateById 方法即可。
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("更新成功");}}
- 數(shù)據(jù)更新后的數(shù)據(jù)庫
delete 操作
刪除操作,既可以根據(jù)主鍵刪除一條記錄,也能根據(jù)主鍵列表實現(xiàn)批量刪除,還能根據(jù)條件來進行刪除。
根據(jù)主鍵刪除一條數(shù)據(jù)
將所要刪除記錄的主鍵作為參數(shù)傳入 deleteById 方法即可。
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("刪除成功");}
}
- 刪除數(shù)據(jù)后的數(shù)據(jù)庫
根據(jù)條件刪除
根據(jù)條件刪除同樣是講條件傳入 Map 中,然后將 Map 作為參數(shù)傳入 deleteByMap 方法,其中 key 對應數(shù)據(jù)庫中的字段,value 對應字段的值。
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("刪除成功");}
}
- 刪除數(shù)據(jù)后的數(shù)據(jù)庫
根據(jù)主鍵批量刪除
將要刪除記錄的主鍵傳入集合中,然后將集合作為 deleteBatchIds 方法的參數(shù)即可。
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("刪除成功");}
}
- 刪除數(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;
@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;@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("插入成功");}
}
- 插入數(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("批量插入成功");}
}
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("批量插入成功");}
}
SaveOrUpdate
單條修改插入
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("更新成功");}
}
批量修改插入
2.1 統(tǒng)一插入
boolean saveOrUpdateBatch(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 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("批量修改插入成功");}
}
- 統(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("批量修改插入成功");}
}
- 分批次插入數(shù)據(jù)后的數(shù)據(jù)庫
Remove
根據(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("刪除成功");}
}
根據(jù)條件刪除
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("刪除成功");}
}
根據(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("批量刪除成功");}
}
Update
根據(jù) ID 選擇修改
boolean updateById(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 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("更新成功");}
}
根據(jù) ID 批量更新
2.1 統(tǒng)一更新
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("更新成功");}
}
2.2 分批次更新
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("更新成功");}
}
Get
根據(jù) ID 查詢
將所要查詢記錄的 id 作為參數(shù),然后將查詢到的實體返回。
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
);}
}
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("查詢成功");}
}
根據(jù) ID 批量查詢
講所要查詢的記錄 id 傳入集合,然后座位方法參數(shù),最后返回查詢到的結(jié)果到一個集合中。
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("查詢成功");}
}
根據(jù)條件查詢
條件傳入 Map 集合,key 對應字段,value 對應值,然后返回集合。
Collection<T> listByMap(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 testListByMap() {Map<String, Object> map
= new HashMap<>();map
.put("sex", "女");Assert.assertEquals(3, employeeService
.listByMap(map
).size());System.out
.println("查詢成功");}
}
查詢所有列表
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("查詢成功");}
}
查詢所有記錄
用于查詢所有數(shù)據(jù)記錄,并將其返回到一個集合中。