网站域名查询系统,英文网站怎么做,网页游戏排行选择,郑州网站开发外包开发中#xff0c;sql拼接很常见#xff0c;所以说一下动态sql#xff1a; 1if2chose,when,otherwise3where,set4foreach用法解析(现有一张users表 内有id username age 三个字段)#xff1a; !--查询所有用户#xff0c;传递参数type#xff0c;如果值为0#xff0…开发中sql拼接很常见所以说一下动态sql 1if2chose,when,otherwise3where,set4foreach用法解析(现有一张users表 内有id username age 三个字段) !--查询所有用户传递参数type如果值为0按照年龄升序排序如果为1则按照年龄降序排序否则按照ID排序--!--choose when otherwise的用法 大致相当于case when default--select idgetUserListByType resultTypeUserselect * from userschoosewhen testtype0order by age asc/whenwhen testtype1order by age desc/whenotherwiseorder by id asc/otherwise/choose/select!--根据多个id查询用户 if where foreach的用法--select idgetUserListByIds resultMapUserselect * from userswhere-- if判断 如果传进去的参数ids不为空if testids!null and idsand id in-- foreach循环ids 以逗号为分隔符 以ids这个字符串中的(为开始 )为结果foreach collectionids itemid open( close) separator,#{id}/foreach/if/where/select!--修改用户信息如果某字段为null则不修改这个字段 set的用法--select idupdateUserupdate userssetif testusername!null and username!username #{username}/ifif testage!nullage #{age}/if/set/select 我们还可以把重复的sql抽取出来作为公用的sql片段 定义sql片段 !-- sql片段 建议对单表进行sql片段的抽取方便重用抽取时不包含where--sql idfindUserSqlif testuserCustomer!nullif testuserCustomer.age!nulland user.age#{userCustomer.age}/ifif testuserCustomer.username!null and userCustomer.username!and user.username like $%{userCustomer.username}%/if/if/sql 使用sql片段 !-- 动态sql --select idfindUserCount parameterTypecom.zy.domain.User resultTypeintselect count(*) from users-- where 可自动去除条件中的第一个andwhereinclude refidfindUserSql/include/where/select 转载于:https://www.cnblogs.com/blazeZzz/p/9296718.html