SQL动态拼接 如何转换为Hibernate复杂子查询?
表结构如下
表名 主键 名字 年龄 手机号
用户表 User id name age mobile
客户表 Cust id name age mobile
主键 user外键 cust外键
服务表 Service id userId custId //两个外键均为多对一关系
根据前台要查询的四个值做SQL拼接类似于这样子
select * from service s
left join user user on s.userId = user.Id
left join Cust cust on s.custId = cust.Id
where 1=1
if (userName != null){
user.name like '%userName%'
}
if (userAge != null){
and user.age = userAge
}
if (userMobile != null){
and user.mobile like '%userMobile%'
}
if (custName != null){
and cust.name like '%custName%'
}
if (cutsAge != null){
and cust.age = custAge
}
if (cutsMobile != null){
and cust.mobile like '%cutsMobile%'
}
我想说的是Hibernate做这种根据不定传参做动态查询的很难写
请高手支招!
具体代码给详细点并且写上详细注释, 谢谢您!