mybatis动态SQL

? 动态SQL在xml中支持的标签

  • if
  • choose
  • where
  • trim
  • foreach
  • bind

if

if在where中使用

<select id="findById" resultType="SysUser" parameterType="map">
    select * from sys_user;
    <where>
        <if test="userName !=null and userName!=''">
            and userName=#{userName}
        </if>
        <if test="userInfo !=null and userInfo!=''">
            and userInfo=#{userInfo}
        </if>
    </where>
</select>

test属性值是一个符合OGNL要求的判断表达式,结果为true或false。在数值中,所有的非0都是true,只有0为false。

  • property!=null:适用于任何类型的字段,用于判断是否为空

  • property!='':判断字符串

  • and 和 or:and -> &&,可以使用小括号分组

    模糊查询:

name like concat('%',#{name},'%')
name like '%${name}%'

在update中使用if

<update id="update" parameterType="SysUser">
    update sys_user
    <set>
        <if test="userName !=null and userName!=''">
            and userName=#{userName},
        </if>
        <if test="userInfo !=null and userInfo!=''">
            and userInfo=#{userInfo},
        </if>
        <if test="headImg!=null">
            headImg=#{headImg, jdbcType=BLOB},
        </if>
        <if test="createTime!=null">
            createTime=#{createTime, jdbcType=timestamp},
        </if>
    </set>
    where id=#{id}
</update>

trim

使用trim实现where

<trim prefix="where" prefixOverrides="and |or ">
    <if test="xx">
    </if>
</trim>

foreach

foreach实现in

<foreach collection="list" open="(" close=")" separator="," item="id" index="i">
	#{id}
</foreach>

findAll(List)

  • collection的值:list|array|collection|_parameter

list,array,collection,_parameter都是map参数的key

/ DefaultSqlSession.wrapCollection方法

  private Object wrapCollection(final Object object) {
    if (object instanceof Collection) {
      StrictMap<Object> map = new StrictMap<>();
      map.put("collection", object);
      if (object instanceof List) {
        map.put("list", object);
      }
      return map;
    } else if (object != null && object.getClass().isArray()) {
      StrictMap<Object> map = new StrictMap<>();
      map.put("array", object);
      return map;
    }
    return object;
  }

foreach实现批量插入

<insert id="insertList" parameterType="list">
    insert into user(
    id,username,password
    )
    values
    <foreach collection="list" item="user" separator=",">
        (
        #{id},#{username},#{password}
        )
    </foreach>
</insert>

foreach实现动态更新

当参数为Map的时候,foreach的index不是索引,而是map的key,利用这个key实现动态更新

<update id="updateByMap" parameterType="map">
    update sys_user
    set 
    <foreach collection="_parameter" item="val" index="key" separator=",">
        #{key}=#{val}
    </foreach>
    where id=#{id}
</update>

updateByMap(new HashMap<String, Object>());

bind

bind标签中可以使用OGNL表达式

  • 模糊查询
<select id="findAll">
    select * from sys_user
    <where>
        <if test="username!=null and username!=''">
            and username like concat('%',#{username},'%')
        </if>
    </where>
</select>

使用了MySQL的函数(concat),不兼容

  • 替代方法,使用字符串拼接
<select id="findAll">
    select * from sys_user
    <where>
        <if test="username!=null and username!=''">
            <bind name="nameLike" value="'%'+username+'%'"/>
            and username like #{nameLike}
        </if>
    </where>
</select>

bind中的OGNL表达式可以防止SQL注入

mybatis适配不同的数据库,生成不同的SQL

  • mybat-config.xml
  <databaseIdProvider type="DB_VENDOR">
    <property name="SQL Server" value="sqlserver"/>
    <property name="DB2" value="db2"/>
    <property name="Oracle" value="oracle"/>
    <property name="MySql" value="mysql"/>
    <property name="PostgreSQL" value="postgresql"/>
    <property name="Derby" value="derby"/>
    <property name="HSQL" value="hsqldb"/>
    <property name="H2" value="h2"/>
  </databaseIdProvider>
  • mapper.xml
<select id="findAll" databaseId="mysql">
    select * from sys_user
    <where>
        <if test="username!=null and username!=''">
            and username like concat('%',#{username},'%')
        </if>
    </where>
</select>
<select id="findAll" databaseId="oracle">
    select * from sys_user
    <where>
        <if test="username!=null and username!=''">
            and username like '%'||#{username}||'%'
        </if>
    </where>
</select>
posted @ 2019-09-23 11:05  fight139  阅读(341)  评论(0编辑  收藏  举报