SQL动态拼接 如何转换为Hibernate复杂子查询?

princess_rosie 2010-12-05 08:55:12
表结构如下

表名 主键 名字 年龄 手机号

用户表 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做这种根据不定传参做动态查询的很难写

请高手支招!

具体代码给详细点并且写上详细注释, 谢谢您!
...全文
141 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧