mybatis动态sql
mybatis动态sql
001 where 和 if
1 2 3 4 5 6 7 8 9 10 11 | <select id= "findAllByCondition" resultType= "com.po.pf.domain.User" parameterType= "com.po.pf.domain.User" > select * from user <where> < if test= "username != null" > and username = #{username} </ if > < if test= "sex != null" > and sex = #{sex} </ if > </where> </select> |
或者
1 2 3 4 5 6 7 8 9 | <select id= "findAllByCondition" resultType= "com.po.pf.domain.User" parameterType= "com.po.pf.domain.User" > select * from user where 1 = 1 < if test= "username != null" > and username = #{username} </ if > < if test= "sex != null" > and sex = #{sex} </ if > </select> |
002-foreach
1 2 3 4 5 6 7 8 9 10 11 12 | <select id= "findUserInIds" resultType= "com.po.pf.domain.User" parameterType= "list" > select * from user <where> < if test= "list != null and list.size()>0" > <foreach collection= "list" open= "and id in (" close= ")" item= "uid" separator= "," > #{uid} </foreach> </ if > </where> </select> List<User> findUserInIds(List<Integer> list); |
003-sql标签
<!-- 了解的内容:抽取重复的sql语句-->
1 2 3 4 5 6 7 8 | <sql id= "defaultUser" > select * from user </sql> <select id= "findById1" parameterType= "int" resultType= "com.po.pf.domain.User" > <include refid= "defaultUser" ></include> where id = #{uid} </select> |
004- trim
Trim 可以在条件判断完的SQL语句前后 添加或者去掉指定的字符
prefix: 添加前缀
prefixOverrides: 去掉前缀
suffix: 添加后缀
suffixOverrides: 去掉后缀
1 2 3 4 5 6 7 8 9 10 11 | <select id= "findAllByCondition" resultType= "com.po.pf.domain.User" parameterType= "com.po.pf.domain.User" > select * from user <trim prefix= "where" suffixOverrides= "and" > < if test= "username != null" > username = #{username} and </ if > < if test= "sex != null" > sex = #{sex} and </ if > </trim> </select> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY