SpringBoot中Mybatis-plus的通用接口BaseMapper方法演示(下)

上编:SpringBoot中Mybatis-plus的通用接口BaseMapper方法演示(上)_biubiubiu0706的博客-CSDN博客

下面是通用接口BaseMapper里的10个查询方法

/**
* 根据 ID 查询
*
* @param id 主键ID
*/
T selectById(Serializable id);

/**
* 查询(根据ID 批量查询)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
List<T> selectBatchIds(Collection<? extends Serializable> idList);

/**
* 查询(根据 columnMap 条件)
*
* @param columnMap 表字段 map 对象
*/
List<T> selectByMap( Map<String, Object> columnMap);

/**
* 根据 entity 条件,查询一条记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
T selectOne(Wrapper<T> queryWrapper);

/**
* 根据 Wrapper 条件,查询总记录数
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
Integer selectCount(Wrapper<T> queryWrapper);

/**
* 根据 entity 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<T> selectList( Wrapper<T> queryWrapper);

/**
* 根据 Wrapper 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<Map<String, Object>> selectMaps(Wrapper<T> queryWrapper);

/**
* 根据 Wrapper 条件,查询全部记录
* <p>注意: 只返回第一个字段的值</p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<Object> selectObjs(Wrapper<T> queryWrapper);

/**
* 根据 entity 条件,查询全部记录(并翻页)
*
* @param page         分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
<E extends IPage<T>> E selectPage(E page, Wrapper<T> queryWrapper);

/**
* 根据 Wrapper 条件,查询全部记录(并翻页)
*
* @param page         分页查询条件
* @param queryWrapper 实体对象封装操作类
*/
<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, Wrapper<T> queryWrapper);

第一个

selectById

用法:查询指定id 对象信息

第二个

selectBatchIds

用法:批量查询指定的id对象信息

第三个

selectByMap

用法:查询满足条件实体信息,条件封装到map集合中

 第四个

selectCount

用法:查询满足条件实体信息记录总条数    参入一个条件构造器对象

第五个

selectList

用法:查询满足条件实体信息记录, 返回List<T>

第六种

selectMaps

用法:查询满足条件实体信息记录, 返回List<Map<String, Object>>

 该方法注意下 返回的List里面的map的key 既不能说全是实体类属性, 也不能说全是数据表字段名

这里得注意下  实际开发时候得注意

 

 关于selectList和selectMap什么时候使用合适????

如果查询的结果  可以封装成对象 那么用selectList  如果查询出来的不适合封装对象  或者不想写封装的对象用selectMap  比如  查询每个部门的人数  select dept_id,count(*) count from t_employee group by dept_id    适用于selectMap

当然selectList也是可以的

比如

第七种

selectPage

用法:分页查询满足条件实体信息记录

用plus做分页需要先配置下

合理化配置,主要是检查分页时候传进来的数据  比如你传个-1分页   很明显不符合逻辑 或者总共5页面  你查询 第10页        做了合理化配置后,不符合逻辑的统一返回最后一页数据

@Configuration
public class MybatisPlusConfig{

    //分页
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true); //合理化
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }
}

select * from employee where...limit (currentPage-1)*pageSize,pageSize

第八种

selectMapsPage

用法:跟selectPage一样,区别在与selectMapsPage返回分页数据集合泛型是Map, selectPage是实体对象。

 

第九种

selectOne

用法:查询满足条件实体对象,如果有多条数据返回,抛异常。

第10种

selectObjs

用法:查询满足条件实体对象,返回指定列的集合,如果没有指定列,默认返回第一列

不指定列就返回第一列

 

猜你喜欢

转载自blog.csdn.net/tiantiantbtb/article/details/129471671
今日推荐