方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 根据 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);

测试代码

selectById

1
2
3
4
5
@Test
void selectById() {
BlogType blogType = blogTypeMapper.selectById(1);
System.out.println(blogType.getId() + "," + blogType.getName() + "," + blogType.getSortNum());
}

selectOne

 如果查到的数据不是 1 条时会报错

1
2
3
4
5
6
7
@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());
}

selectBatchIds

1
2
3
4
5
6
7
8
9
10
11
12
@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());
}
}

selectList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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());
}
}

selectByMap

1
2
3
4
5
6
7
8
9
@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());
}
}

selectMaps

查询到的数据以键值对的方式存储在map中

1
2
3
4
5
6
7
8
9
@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"));
}
}

selectObjs

只返回一个字段

1
2
3
4
5
6
7
8
9
10
@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);
}

selectPage

先配置分页插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.ledao.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* @author LeDao
* @company
* @create 2022-12-15 4:03
*/
@Configuration
@EnableTransactionManagement
public class MybatisPlusConfig {

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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);
}
}

selectMapsPage

1
2
3
4
5
6
7
8
9
10
11
12
@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);
}
}

selectCount

1
2
3
4
5
6
7
@Test
void selectCount() {
QueryWrapper<BlogType> blogTypeQueryWrapper = new QueryWrapper<>();
blogTypeQueryWrapper.gt("id", 7);
int count = blogTypeMapper.selectCount(blogTypeQueryWrapper);
System.out.println(count);
}