Mapper
查询
简单查询
Wrapper的实现类主要有QueryWrapper、UpdaterMapper、LambdaQueryWrapper、LambdaUpdateWrapper都提供了对应的条件方法来定制查询对象,详情查看[Wrapper](# Wrapper)
方法名 |
描述 |
T selectById(Serializable id) |
根据单个ID查询单个实体 |
List<T> selectBatchIds(Collection<? extends Serializable> |
根据多个ID批量查询多个实体 |
List<T> selectByMap(Map<String, Object> columnMap) |
根据Map等值查询多个实体,Map的key是列名,value是条件 |
T selectOne(Wrapper<T> queryWrapper) |
根据Wrapper条件查询出一个实体 |
List<T> selectList(Wrapper<T> queryWrapper) |
根据Wrapper条件查询出多个实体 |
List<Object> selectObjs(Wrapper<T> queryWrapper) |
根据Wrapper条件查询出实体的一列属 |
List<Map<String, Object>> selectMaps(Wrapper<T> queryWrapper); |
根据Wrapper条件查询出实体,并且封装成map |
Integer selectCount(Wrapper<T> queryWrapper); |
根据Wrapper条件查询出符合条件的记录数 |
分页查询
- 创建一个配置类,用于配置分页插件
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
- 使用分页查询
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
Page<Student> pageConfigEntity = new Page<>(1, 2, true);
Page<Student> page = userMapper.selectPage(pageConfigEntity, queryWrapper);
System.out.println("总页数" + page.getPages());
System.out.println("总记录数" + page.getTotal());
List<Student> studentList = page.getRecords();
studentList.forEach(System.out::println);
Page<Map<String, Object>> pageConfigMap = new Page<>(1, 2, true);
Page<Map<String, Object>> mapPage = userMapper.selectMapsPage(pageConfigMap, queryWrapper);
System.out.println("总页数" + mapPage.getPages());
System.out.println("总记录数" + mapPage.getTotal());
List<Map<String, Object>> records = mapPage.getRecords();
records.forEach(System.out::println);
自定义SQL
- 在实体类中自定义方法,至少使用一个参数为
@Param(Constants.WRAPPER) Wrapper<Student> wrapper
- SQL语句可使用XML方式或注解方式,在where子句中添加
${ew.customSqlSegment}
即可
- xml方式需要在application文件中配置xml文件路径,
mybatis-plus.mapper-locations
,路径使用/
分割
public interface UserMapper extends BaseMapper<Student> {
@Select("select * from student ${ew.customSqlSegment}")
List<student> selectAll(@Param(Constants.WRAPPER) Wrapper<Student> wrapper);
}
- 编写程序使用
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Test
public void testSelect() {
List<Student> studentList = userMapper.selectAll(queryWrapper);
Assertions.assertEquals(5, studentList.size());
}
}
插入
方法名 |
描述 |
int insert(T entity) |
根据实体插入一条记录,自动回写ID值 |
主键生成策略
全局策略
默认全局策略是基于雪花算法的自增ID,可通过mybatis-plus.global-config.db-config.id-type
属性填充,可选策略与局部可选策略一致
局部策略
使用@TableId
注解,修改注解的type
属性为IdType
枚举类的属性
枚举类型 |
描述 |
IdType.NONE |
默认策略,跟随全局的策略 |
IdType.AUTO |
数据库自增 |
IdType.ASSIGN_ID |
分布式全局唯一分配ID |
IdType.ASSIGN_UUID |
分布式全局唯一分配UUID |
IdType.INPUT |
自行输入 |
修改
方法名 |
描述 |
int updateById(T entity); |
根据实体ID去更新其他属性,如果其他属性为null则不会出现在set中 |
int update(T updateEntity, Wrapper<T> whereWrapper) |
根据UpdateWrapper查询,根据实体更新,即实体出现在set子句中,UpdateWrapper出现在where子句中,其实[UpdateWrapper](# UpdateWrapper常用方法)中也有set方法可设置set子句的内容,所以可将实体设置为null |
删除
方法名 |
描述 |
int deleteById(Serializable id) |
根据单个ID批量删除单个实体 |
int deleteBatchIds(Collection<? extends Serializable> idList) |
根据多个ID批量删除多个实体 |
int deleteByMap(Map<String, Object> columnMap); |
根据Map等值查询删除多个实体,Map的key是列名,value是条件 |
int delete(Wrapper<T> wrapper) |
根据Wrapper条件删除实体 |
Comments NOTHING