一、配置
/**
* @author beth
* @data 2019-10-14 20:01
*/
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
二、分页查询
/**
* 分页查询
*/
@Test
public void selectPage() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.ge("age",26);
Page<UserInfo> page = new Page<UserInfo>(1,2);
IPage<UserInfo> iPage = userInfoMapper.selectPage(page,queryWrapper);
System.out.println("总页数"+iPage.getPages());
System.out.println("总记录数"+iPage.getTotal());
List<UserInfo> userInfoList = iPage.getRecords();
userInfoList.forEach(System.out::println);
}
三、分页查询,返回map
/**
* 分页查询,返回map
*/
@Test
public void selectMapPage() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.ge("age",26);
Page<UserInfo> page = new Page<UserInfo>(1,2);
//Page<UserInfo> page = new Page<UserInfo>(1,2,false);
IPage<Map<String,Object>> iPage = userInfoMapper.selectMapsPage(page,queryWrapper);
System.out.println("总页数"+iPage.getPages());
System.out.println("总记录数"+iPage.getTotal());
List<Map<String, Object>> userInfoList = iPage.getRecords();
userInfoList.forEach(System.out::println);
}
四、自定义sql分页查询
1、
@Repository
public interface UserInfoMapper extends BaseMapper<UserInfo> {
IPage<UserInfo> selectUserPage(Page<UserInfo> page, @Param(Constants.WRAPPER) Wrapper<UserInfo> wrapper);
}
2、
<select id="selectUserPage" resultMap="com.xiao.permission_system.entity.UserInfo">
select * from user ${ew.customSqlSegment}
</select>
3、
/**
* 自定义sql分页查询
*/
@Test
public void selectUserPage() {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.ge("age",26);
Page<UserInfo> page = new Page<UserInfo>(1,2);
IPage<UserInfo> iPage = userInfoMapper.selectUserPage(page,queryWrapper);
System.out.println("总页数"+iPage.getPages());
System.out.println("总记录数"+iPage.getTotal());
List<UserInfo> userInfoList = iPage.getRecords();
userInfoList.forEach(System.out::println);
}
来源:oschina
链接:https://my.oschina.net/u/2427561/blog/3118521