// 根据 ID 查询
T selectById(Serializable id);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询全部记录
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 查询(根据 columnMap 条件)
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// 根据 Wrapper 条件,查询全部记录
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 entity 条件,查询全部记录(并翻页)
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录(并翻页)
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
@Test
void selectById() {
BlogType blogType = blogTypeMapper.selectById(1);
System.out.println(blogType.getId() + "," + blogType.getName() + "," + blogType.getSortNum());
}
如果查到的数据不是1条时会报错
@Test
void selectOne() {
QueryWrapper<BlogType> blogTypeQueryWrapper = new QueryWrapper<>();
blogTypeQueryWrapper.eq("name", "MyBatis");
BlogType blogType = blogTypeMapper.selectOne(blogTypeQueryWrapper);
System.out.println(blogType.getId() + "," + blogType.getName() + "," + blogType.getSortNum());
}
@Test
void selectBatchIds() {
List<Integer> idList = new ArrayList<>();
idList.add(1);
idList.add(5);
idList.add(6);
idList.add(7);
List<BlogType> blogTypeList = blogTypeMapper.selectBatchIds(idList);
for (BlogType blogType : blogTypeList) {
System.out.println(blogType.getId() + "," + blogType.getName() + "," + blogType.getSortNum());
}
}
@Test
void selectList() {
//参数为空时查询所有
List<BlogType> blogTypeList = blogTypeMapper.selectList(null);
for (BlogType blogType : blogTypeList) {
System.out.println(blogType.getId() + "," + blogType.getName() + "," + blogType.getSortNum());
}
System.out.println("------------------------");
//根据条件查询
QueryWrapper<BlogType> blogTypeQueryWrapper = new QueryWrapper<>();
blogTypeQueryWrapper.like("name", "a");
List<BlogType> blogTypeList1 = blogTypeMapper.selectList(blogTypeQueryWrapper);
for (BlogType blogType : blogTypeList1) {
System.out.println(blogType.getId() + "," + blogType.getName() + "," + blogType.getSortNum());
}
}
@Test
void selectByMap() {
Map<String, Object> map = new HashMap<>(16);
map.put("sortNum", 81);
List<BlogType> blogTypeList = blogTypeMapper.selectByMap(map);
for (BlogType blogType : blogTypeList) {
System.out.println(blogType.getId() + "," + blogType.getName() + "," + blogType.getSortNum());
}
}
查询到的数据以键值对的方式存储在map中
@Test
void selectMaps() {
QueryWrapper<BlogType> blogTypeQueryWrapper = new QueryWrapper<>();
blogTypeQueryWrapper.gt("id", 5).like("name", "a");
List<Map<String, Object>> mapList = blogTypeMapper.selectMaps(blogTypeQueryWrapper);
for (Map<String, Object> map : mapList) {
System.out.println(map.get("id")+","+map.get("name")+","+map.get("sortNum"));
}
}
只返回一个字段
@Test
void selectObjs() {
QueryWrapper<BlogType> blogTypeQueryWrapper = new QueryWrapper<>();
//选择要查询的字段,不选择就返回第一个字段(一般是id)
blogTypeQueryWrapper.select("name");
//查询条件
blogTypeQueryWrapper.gt("id", 5);
List<Object> objectList = blogTypeMapper.selectObjs(blogTypeQueryWrapper);
System.out.println(objectList);
}
先配置分页插件
package com.ledao;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author LeDao
* @company
* @create 2021-07-23 14:15
*/
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
测试
@Test
void selectPage() {
//设置查询条件
QueryWrapper<BlogType> blogTypeQueryWrapper = new QueryWrapper<>();
blogTypeQueryWrapper.gt("id", 7);
//设置当前页以及每页记录数
Page<BlogType> page = new Page<>(2, 2);
//获取记录
Page<BlogType> blogTypePage = blogTypeMapper.selectPage(page, blogTypeQueryWrapper);
List<BlogType> blogTypeList = blogTypePage.getRecords();
for (BlogType blogType : blogTypeList) {
System.out.println(blogType);
}
}
@Test
void selectMapsPage() {
//设置查询条件
QueryWrapper<BlogType> blogTypeQueryWrapper = new QueryWrapper<>();
blogTypeQueryWrapper.gt("id", 7);
//设置当前页以及每页记录数
Page page = new Page<>(2, 2);
IPage<Map<String, Object>> mapIPage = blogTypeMapper.selectMapsPage(page, blogTypeQueryWrapper);
for (Map<String, Object> record : mapIPage.getRecords()) {
System.out.println(record);
}
}
@Test
void selectCount() {
QueryWrapper<BlogType> blogTypeQueryWrapper = new QueryWrapper<>();
blogTypeQueryWrapper.gt("id", 7);
int count = blogTypeMapper.selectCount(blogTypeQueryWrapper);
System.out.println(count);
}