like子句JPQL中的参数

问题描述 投票:0回答:9

我正在尝试编写一个带有 like 子句的 JPQL 查询:

LIKE '%:code%'

我想要 code=4 并找到

455
554
646
...

我过不了

:code = '%value%'

namedQuery.setParameter("%" + this.value + "%");

因为在另一个地方我需要

:value
不被
%
字符包裹。有帮助吗?

java jpa eclipselink jpql sql-like
9个回答
176
投票

如果你这样做

LIKE :code

然后做

namedQuery.setParameter("code", "%" + this.value + "%");

然后 value 仍然没有 '%' 符号。如果您需要在同一查询的其他地方使用它,只需使用除“代码”之外的另一个参数名称。


62
投票

我不对所有查询使用命名参数。例如,在 JpaRepository 中使用命名参数是不常见的。

解决方法我使用 JPQL CONCAT 函数(此代码模拟开始于):

@Repository
public interface BranchRepository extends JpaRepository<Branch, String> {
    private static final String QUERY = "select b from Branch b"
       + " left join b.filial f"
       + " where f.id = ?1 and b.id like CONCAT(?2, '%')";
    @Query(QUERY)
    List<Branch> findByFilialAndBranchLike(String filialId, String branchCode);
}

我在优秀的文档中发现了这种技术:http://openjpa.apache.org/builds/1.0.1/apache-openjpa-1.0.1/docs/manual/jpa_overview_query.html


7
投票

您可以使用 JPA LOCATE 函数

LOCATE(searchString, candidateString [, startIndex]): 返回 candidateString 中 searchString 的第一个索引。位置是基于 1 的。 如果未找到字符串,则返回 0.

仅供参考:my top google hit 上的文档颠倒了参数。

SELECT 
  e
FROM 
  entity e
WHERE
  (0 < LOCATE(:searchStr, e.property))

4
投票

我不知道我是迟到了还是超出了范围,但我认为我可以这样做:

String orgName = "anyParamValue";

Query q = em.createQuery("Select O from Organization O where O.orgName LIKE '%:orgName%'");

q.setParameter("orgName", orgName);

4
投票

JPA criteria API 中有很好的 like() 方法。尝试使用它,希望它会有所帮助。

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery criteriaQuery = cb.createQuery(Employees.class);
Root<Employees> rootOfQuery = criteriaQuery.from(Employees.class);
criteriaQuery.select(rootOfQuery).where(cb.like(rootOfQuery.get("firstName"), "H%"));

3
投票
  1. 使用下面的 JPQL 查询。
select i from Instructor i where i.address LIKE CONCAT('%',:address ,'%')");
  1. 使用以下标准代码:
@Test
public void findAllHavingAddressLike() {
    CriteriaBuilder cb = criteriaUtils.criteriaBuilder();
    CriteriaQuery<Instructor> cq = cb.createQuery(Instructor.class);
    Root<Instructor> root = cq.from(Instructor.class);
    printResultList(cq.select(root).where(
        cb.like(root.get(Instructor_.address), "%#1074%")));
}

3
投票

使用

JpaRepository
CrudRepository
作为存储库界面:

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    @Query("SELECT t from Customer t where LOWER(t.name) LIKE %:name%")
    public List<Customer> findByName(@Param("name") String name);

}


@Service(value="customerService")
public class CustomerServiceImpl implements CustomerService {

    private CustomerRepository customerRepository;
    
    //...

    @Override
    public List<Customer> pattern(String text) throws Exception {
        return customerRepository.findByName(text.toLowerCase());
    }
}

2
投票

只需省略''

LIKE %:code%

0
投票

使用JPQL查询。

@Query("select e from Entity e where e.id = ?1 and e.code like CONCAT('%', CONCAT(?2, '%'))")
List<Entity> findByIdAndCodeLike(Long id, String code);
© www.soinside.com 2019 - 2024. All rights reserved.