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

princess_rosie princess_rosie 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
表结构如下

表名 主键 名字 年龄 手机号

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

请高手支招!

具体代码给详细点并且写上详细注释, 谢谢您!
...全文
评论
收藏 已收藏
分享
返回顶部
princess_rosie
Web 开发 81,108 成员 341,721 内容