目录
1.直接使用分页插件进行查询
1.1先引入插件
@Configuration
@EnableTransactionManagement
@MapperScan("com.llkj.dp.system.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor addPaginationInnerInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//向Mybatis过滤器链中添加分页拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
1.2进行编写
IPage<SysRole> iPage = new Page<>(page, limit);
QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
if (StrUtil.isEmpty(roleQueryVo.getRoleName())) {
IPage<SysRole> result = sysRoleService.page(iPage);
return Result.ok(result);
} else {
wrapper.like("role_name", roleQueryVo.getRoleName());
IPage<SysRole> result = sysRoleService.page(iPage, wrapper);
return Result.ok(result);
}
2.Mybatis写法
controller
Page<SysRole> pageParm = new Page<>(page, limit);
IPage<SysRole> pageModel = sysRoleService.selectPage(pageParm, roleQueryVo);
service
@Override
public IPage<SysRole> selectPage(Page<SysRole> pageParam, SysRoleQueryVo roleQueryVo) {
return sysRoleMapper.selectPage1(pageParam, roleQueryVo);
}
mapper
注意在mapper中方法不要写selectPage,否则会导致程序出错
xml配置
<resultMap id="RoleMap" type="com.llkj.dp.model.system.SysRole" autoMapping="true">
</resultMap>
<sql id="columns">
id,role_name,role_code,description,create_time,update_time,is_deleted
</sql>
<select id="selectPage1" resultMap="RoleMap">
select
<include refid="columns"/>
from sys_role
<where>
<if test="vo.roleName!=null and vo.roleName!=''">
and role_name like concat('%',#{vo.roleName},'%')
</if>
and is_deleted = 0
</where>
order by id desc
</select>