SpringBoot之JPA模糊查询,同时使用pageable参数

JPA模糊查询,同时使用pageable参数

使用JPA在做模糊查询(LIKE)时,有如下几种方式可以完成,本文介绍的是在同时使用Pageable参数的情况下,如何做模糊查询:

本例中查询需求为:分页获得某企业下,题库名字和题库描述符合搜索内容的题库列表

一、使用JPA语法
public interface QuestionBankDao extends JpaRepository<QuestionBank, Integer> {
	Page<QuestionBank> findByEnterpriseIdAndDisabledFalseAndNameLikeAndDescriptionLike(Pageable pageable, int enterpriseId, String name, String description);
}

使用时,需要在Controller层或者Service层将%通配符加入到搜索字符串中,eg:

searchContent = '%' + searchContent + '%';
questionBankDao.findByEnterpriseIdAndDisabledFalseAndNameLikeAndDescriptionLike(pageable,1,searchContent,searchContent);

可以看到,这样子写带来的不便:

  • DAO的对应方法语句会变得很长
  • 要求在Controller层或者Service层将%通配符加入到搜索字符串中,我个人觉得这样的设计不够优美
二、使用@Query配合原生SQL
public interface QuestionBankDao extends JpaRepository<QuestionBank, Integer> {
    @Query(value = "select b from question_bank b where b.enterprise_id = :enterpriseId " +
            "and (b.name like CONCAT('%',:searchContent,'%') or b.description like CONCAT('%',:searchContent,'%')) " +
            "and b.disabled = false order by ?#{#pageable}", nativeQuery = true)
    Page<QuestionBank> searchByEnterpriseId(Pageable pageable, @Param("enterpriseId") int enterpriseId, @Param("searchContent") String searchContent);
}

使用的时候,直接将searchContent传入DAO就可以了,不需要在Controller或者Service层拼接通配符。

但是这样做对我而言有个比较棘手的问题:
当需要使用Pageable利用字段排序时,如果在Java的代码中使用的实体类是用驼峰命名(createdTime),而在MySQL中用的是下划线命名(created_time),即在实体类中用了@Column进行映射。此时Pageable中的参数必须传入MySQL中的下划线命名,而不是实体类的驼峰命名。

三、使用@Query配合JPQL(类似hiberneate中的HQL)
public interface QuestionBankDao extends JpaRepository<QuestionBank, Integer> {
    @Query(value = "select b from QuestionBank b where b.enterpriseId = :enterpriseId " +
            "and (b.name like %:searchContent% or b.description like %:searchContent%) " +
            "and b.disabled = false")
    Page<QuestionBank> searchByEnterpriseId(Pageable pageable, @Param("enterpriseId") int enterpriseId, @Param("searchContent") String searchContent);

}

同二,使用的时候,直接将searchContent传入DAO就可以了,不需要在Controller或者Service层拼接通配符。

方法三是我最后采用的方法,能够解决方法一种不够优美?的小问题,也能解决方法二中名字映射的问题。因为它用的是JPQL,直接使用的就是Java中实体对象的名字,MySQL中的字段名是什么JPQL根本不关心


加油冲鸭!

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值