mybatis 动态sql(查询学习)
Mybatis 这个框架 与书写 SQL 语句 有很大的关系,所以 在 没有 SQL 很好的 书写能力下,会遇到好多问题,本人就是例子,先将学习(查询操作中遇到的问题进行总结);
一,<bind>标签的使用,(绑定上下文 )多用与 sql 语句中 sql 语句like 模糊查询使用;
使用案列代码:
<select id="getObjectByVo" resultMap="studentMap" parameterType="queryVo"> select * from student <where> <if test="entity!=null"> <if test="entity.stu_name!=null "> <bind name="stu_name" value="'%'+entity.getStu_name()+'%'"/> stu_name like #{stu_name} </if> </if> </where> <if test="startSize!=null and pageSize!=null"> limit #{startSize} ,#{pageSize} </if> </select>
二.CONAT 的使用:(字符串连接函数)
stu_name like CONCAT(CONCAT('%',#{entity.stu_name},'%'"))
替换 上面bind 的
<bind name="stu_name" value="'%'+entity.getStu_name()+'%'"/> stu_name like #{stu_name}
添加 <if>判断条件 以防 出错!
一起交流学习,一起进步;------------------------Ankermaker;
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步