美文网首页
分页查询

分页查询

作者: 笨比乔治 | 来源:发表于2020-12-06 10:45 被阅读0次

1.动态查询

EntityWrapper wrapper = new EntityWrapper<Article>(); 
wrapper.eq("id", article.getId());
 //动态sql,例如<if test="null != field"> and field='xxx' </if> 
wrapper.eq(null != map.get(field), field, map.get(field));

2.利用反射获取pojo的全部属性
3.使用map

// POST /article/search/{page}/{size} 文章分页
    @RequestMapping(value = "search/{page}/{size}", method = RequestMethod.POST)
    //之前接受文章数据,使用pojo,但是现在根据条件查询
    //而所有的条件都需要进行判断,遍历pojo的所有属性需要使用反射的方式,成本较高,性能较低
    //直接使用集合的方式遍历,这里接受数据改为Map集合
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String, Object> map) {
        //根据条件分页查询
        Page<Article> pageData = articleService.findByPage(map, page, size);

        //封装分页返回对象
        PageResult<Article> pageResult = new PageResult<>(
                pageData.getTotal(), pageData.getRecords()
        );

        //返回数据
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
 
   }

增删改查--修改
可以传入一个对象,对象里面包含了有id值,简单一点就可以直接传入一个对象。

image.png
image.png
image.png

MyBatis-Plus 分页插件---四步走

第一步: 配置分页配置类

使用 Mybatis Plus 提供的Page对象
向Mybatis Plus中注入Pagination Interceptor插件
新建config包,创建MybatisPlus Config对象,添加下面的代码

@Configuration 
public class MybatisPlusConfig { 
@Bean 
public PaginationInterceptor paginationInterceptor() { 
return new PaginationInterceptor(); 
} 
}

第二步:编写Dao层代码

Mapper接口
public interface SsCompanyMapper extends BaseMapper<SsCompany> {//可以继承或者不继承BaseMapper
    /**
     * <p>
     * 查询 : 根据state状态查询用户列表,分页显示
     * 注意!!: 如果入参是有多个,需要加注解指定参数名才能在xml中取值
     * </p>
     *
     * @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位(你可以继承     * Page实现自己的分页对象)
     * @param state 状态,可以作为查询条件,可以不加
     * @return 分页对象
     */
    //IPage<User> selectPageVo(Page page, @Param("state") Integer state);
    IPage<SsCompany> selectPage(Page page);
}

Mapper.xml 等同于编写一个普通 list 查询,mybatis-plus 自动替你分页
<select id="selectPage" resultType="com.itheima.domain.SsCompany">
select * from ss_company
</select>
注意:加不加where取决于你是否携带查询条件

第三步:调用分页方法

接口
public interface SsCompanyService {
    IPage<SsCompany> selectPage(Page<SsCompany> page);
}
实现类
@Service
@Transactional
public class SsCompanyServiceImpl implements SsCompanyService {

    @Autowired
    private SsCompanyMapper ssCompanyMapper;

    @Override
    public IPage<SsCompany> selectPage(Page<SsCompany> page) {
        // 不进行 count sql 优化,解决 MP 无法自动优化 SQL 问题,这时候你需要自己查询 count 部分
        // page.setOptimizeCountSql(false);
        // 当 total 为小于 0 或者设置 setSearchCount(false) 分页插件不会进行 count 查询
        // 要点!! 分页返回的对象与传入的对象是同一个
        return ssCompanyMapper.selectPage(page);
    }
}

第四步:编写Controller

@RestController
@RequestMapping("/company")
public class SsCompanyController {

    @Autowired
    private SsCompanyService ssCompanyService;

    @RequestMapping("/list")
    public IPage<SsCompany> list() {
        /**
         * Page(current,size)
         * current:当前页,long类型
         * size:每页显示的数量,long类型
         * 可参考其构造方法
         */
        IPage<SsCompany> iPage = ssCompanyService.selectPage(new Page<>(2l,3));
        return iPage;
    }
}

相关文章

网友评论

      本文标题:分页查询

      本文链接:https://www.haomeiwen.com/subject/bqvwyktx.html