Mybaits 查询 choose when 的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @Select ( "<script>" + "SELECT * " + "FROM bgs_housing A" + " <where> " + " A.building !=0 and built_area >=#{needVo.min_acreage} and built_area <= #{needVo.max_acreage} and built_area !=0" + " and A.id NOT IN(SELECT housing FROM bgs_follow WHERE need = #{needVo.id} AND bgs_follow.state <5)" + " AND A.type = #{needVo.max_acreage}" + " <foreach collection=\"params.keys\" item=\"key\" > " + " <choose>\r\n" + " <when test=\"key =='name'\">\r\n" + " and A.name like concat('%',#{params[${key}]},'%') " + " </when>\r\n" + " <otherwise>\r\n" + " <if test=\"params[key]!= null\">and A.${key} =#{params[${key}]}</if> " + " </otherwise>\r\n" + " </choose>" + "</foreach> " + " <if test=\"needVo.min_price!= 0\">and A.rent_price >=#{needVo.min_price}</if> " + " <if test=\"needVo.max_price!= 0\">and A.rent_price <=#{needVo.max_price}</if> " + " </where> " + " limit #{page.offset},#{page.size}" + "</script>" ) List<Map<String, Object>> matchHousing( @Param ( "needVo" )Need needVo, @Param ( "params" )Map<String, Object> params, @Param ( "page" ) PageUtil page) throws Exception; |
注意:
单字符判断相等时错误:
<if test="takeWay == '1' and workday != null ">
改为<if test='takeWay == "1" and workday != null '>
或改为<if test="takeWay == '1'.toString() and workday != null ">即可。
原因是:
mybatis是用OGNL表达式来解析的,在OGNL的表达式中,’1’会被解析成字符,java是强类型的,char 和 一个string 会导致不等,所以if标签中的sql不会被解析。
总结下使用方法:单个的字符要写到双引号里面或者使用.toString()才行!
标签:
Mybaits
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了