04-Mapper

nobility 发布于 2022-05-05 2579 次阅读


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条件查询出符合条件的记录数

分页查询

  1. 创建一个配置类,用于配置分页插件
@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();  //创建MybatisPlus拦截器
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));  //添加分页插件,并设置方言
        return interceptor;
    }
}
  1. 使用分页查询
QueryWrapper<User> queryWrapper = new QueryWrapper<>();  //查询构造器

/*实体类方式*/
Page<Student> pageConfigEntity = new Page<>(1, 2, true);  //参数是当前页数,页面大小,是否查询总记录数,默认是true所以是两条SQL
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);

/*map方式*/
Page<Map<String, Object>> pageConfigMap = new Page<>(1, 2, true);  //参数是当前页数,页面大小,是否查询总记录数,默认是true所以是两条SQL
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

  1. 在实体类中自定义方法,至少使用一个参数为@Param(Constants.WRAPPER) Wrapper<Student> wrapper
  2. SQL语句可使用XML方式或注解方式,在where子句中添加${ew.customSqlSegment}即可
  3. 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);
}
  1. 编写程序使用
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条件删除实体
此作者没有提供个人介绍
最后更新于 2022-05-05