本文主要是介绍在开发过程中,使用 mybatis 所遇到的坑,仅供自己记录查询以及各位猿友借鉴!
**场景 3:**mybatis-xml 动态查询条件中实现 if else 的效果
SQL 写法
<where> <if test="sInfoWindcode != null and sInfoWindcode != ''"> AND a.s_info_windcode = #{sInfoWindcode} </if> <if test="sInfoWindcode == null || sInfoWindcode == ''"> <![CDATA[ LIMIT 50 ]]> </if>> </where>
或者
<choose> <when test="processStatus != null && processStatus != '' && processStatus != '-110'"> and process_status = #{processStatus,jdbcType=VARCHAR} </when> <otherwise> and process_status != 1 </otherwise> </choose>
**场景 2:**mybatis-xml 查询条件字段的值是一个数组,也即前端的条件筛选的值可以多个
参数定义:
private String[] marketListBoardName;
SQL 写法:
<if test="marketListBoardName != null and marketListBoardName != ''"> AND ad.MARKET_LISTBOARDNAME IN <foreach collection="marketListBoardName" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </if>
**场景 1:**mybatis-xml 查询条件字段的值是数字字符串如 1 或者 y,如下写法的判断条件将不起作用
<if test="type == '1'"> AND a.S_DIV_PROGRESS != '3' </if> <if test="type == 'y'"> AND a.S_DIV_PROGRESS != '3' </if>
更改为:
<if test='type == "1"'> AND a.S_DIV_PROGRESS != '3' </if> <if test="type == '1'.toString()"> AND a.S_DIV_PROGRESS != '3' </if> <if test='type == "y"'> AND a.S_DIV_PROGRESS != '3' </if>
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于