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()才行!

posted on   滚动的蛋  阅读(409)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示