我使用的是Micronaut Data 1.0.2版本。
给定以下JPA实体类:
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String code;
private String name;
private String fullName;
}
我可以使用以下PageableRepository
:方法创建全文搜索查询
@Repository
public interface ProductRepository extends PageableRepository<Product, Long> {
Slice<Product> findByCodeLikeOrNameLikeOrFullNameLike(
String code, String name, String fullName, Pageable pageable);
}
但是,我有一个问题,需要为name
属性再添加一个条件。我想要实现的相当于以下SQL:
select * from product
where code like '%search%'
or (name like '%search%' and name not like '%-%')
or full_name like '%search%'
我测试了以下方法:
Slice<Product> findByCodeLikeOrNameLikeAndNotLikeOrFullNameLike
Slice<Product> findByCodeLikeOrNameLikeAndNotContainsOrFullNameLike
Slice<Product> findByCodeLikeOrNameLikeAndNameNotLikeOrFullNameLike
Slice<Product> findByCodeLikeOrNameLikeAndNameNotContainsOrFullNameLike
知道怎么让它工作吗?
提前感谢。
如果您使用@Query
,那么它应该类似于:
@Query(value = "select p from Product p where (p.code like :code or p.fullName like :fullName or p.name like :name) and p.name not like :ignoreName")
Slice<Product> fullSearch(String code, String name, String ignoreName, String fullName, Pageable pageable);
这里你可以省略countQuery
,因为你使用的是Slice<Product>
如果要使用Page<Product>
,则需要有可用的元素总数。所以您需要提供计数查询。
类似:
countQuery = "select count(p.id) from Product p where (p.code like :code or p.fullName like :fullName or p.name like :name) and p.name not like :ignoreName")