Java HQL在所有属性中搜索
我正在使用HQL 我想找到拥有任何包含“mc”属性的客户 还有比这个更短的查询吗Java HQL在所有属性中搜索,java,hql,Java,Hql,我正在使用HQL 我想找到拥有任何包含“mc”属性的客户 还有比这个更短的查询吗 @Query("select c from Customer c where c.name like %:mc%" + " or c.mail like %:mc%" + " or c.adress like %:mc%" + " or c.tel like %:mc%" + " or c.cin like %:mc%") 我建议您使用Sprin
@Query("select c from Customer c where c.name like %:mc%"
+ " or c.mail like %:mc%"
+ " or c.adress like %:mc%"
+ " or c.tel like %:mc%"
+ " or c.cin like %:mc%")
我建议您使用SpringJPA来实现这个结果。我相信它会更干净
@Repository
@Transactional
public interface CustomerRepository extends JpaRepository<Customer, Long> {
List<Customer> findByName(String name);
List<Customer> findByMail(String mail);
List<Customer> findbyTel(String tel);
List<Customer> findByCin(String cin);
}
@Service
public CustomerService {
@Autowired
private CustomerRepository customerRepo;
public List<Customer> searchCustomers(String keyword){
List<Customer> customers = new ArrayList<>();
customers.addAll(customerRepo.findByName(keyword));
customers.addAll(customerRepo.findByMail(keyword));
customers.addAll(customerRepo.findbyTel(keyword));
customers.addAll(customerRepo.findByCin(keyword));
return customers;
}
}
@存储库
@交易的
公共接口CustomerRepository扩展了JpaRepository{
列出findByName(字符串名称);
列表findByMail(字符串邮件);
列表findbyTel(字符串电话);
列表findByCin(字符串cin);
}
@服务
公共客户服务{
@自动连线
私人客户存储位置客户存储;
公共列表搜索客户(字符串关键字){
列出客户=新建ArrayList();
customers.addAll(customerRepo.findByName(关键字));
customers.addAll(customerRepo.findByMail(关键字));
customers.addAll(customerRepo.findbyTel(关键字));
customers.addAll(customerRepo.findByCin(关键字));
返回客户;
}
}
如果这些字段中的任何一个是唯一的,那么您应该将列表返回类型更改为simply Customer,并将相应的add方法从addAll(集合)更改为add(对象) 感谢您的回复,但在我的例子中,我使用HQL并希望找到一个更短的查询