我使用的是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%")
答案 0 :(得分:0)
我建议您使用Spring JPA来实现此结果。我相信它会更清洁。
@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;
}
}
如果这些字段中的任何一个是唯一的,那么您应该将List返回类型更改为Customer,并将相应的add方法从addAll(Collection)更改为add(Object);