mybatis动态sql

1、循环

public List<Entity> queryById(List<String> userids);
  <select id="queryById" resultMap="BaseReslutMap" >
      select * FROM entity
      where id in 
      <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
              #{userid}
      </foreach>
  </select>

在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:

如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.(官方说法:注意 你可以将一个 List 实例或者数组作为参数对象传给 MyBatis,当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”。)所以,不管是多参数还是单参数的list,array类型,都可以封装为map进行传递。如果传递的是一个List,则mybatis会封装为一个list为key,list值为object的map,如果是array,则封装成一个array为key,array的值为object的map,如果自己封装呢,则colloection里放的是自己封装的map里的key值

2、 concat模糊查询

<select id="queryById" resultMap="BascResultMap" parameterType="entity">
  SELECT * from entity
  <where>
    <if test="name!=null">
      name like concat('%',concat(#{name},'%'))
    </if>
  </where>
</select>

3、 choose (when, otherwise)标签

<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">  
    SELECT *  
      FROM User u   
    <where>  
        <choose>  
            <when test="username !=null ">  
                u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')  
            </when >  
            <when test="sex != null and sex != '' ">  
                AND u.sex = #{sex, jdbcType=INTEGER}  
            </when >  
            <when test="birthday != null ">  
                AND u.birthday = #{birthday, jdbcType=DATE}  
            </when >  
            <otherwise>  
            </otherwise>  
        </choose>  
    </where>    
</select>  

4、 selectKey 标签

<insert id="insert" parameterType="com.test.entity.User" keyProperty="userId">  
    <selectKey keyProperty="userId" resultType="String" order="BEFORE">  
        select nextval('user')  from dual
    </selectKey>  
    INSERT INTO tb_user(user_id,  
                            user_name,  
                            STUDENT_SEX)  
    VALUES (#{userId},  
            #{userName})  
</insert> 

5、trim

where set 标签 的功能都可以用 trim 标签来实现,并且在底层就是通过
TrimSqlNode 现的。

prefix :当 trim 元素内包含内容时,会给内容增加 prefix 指定的前缀。
prefixOverrides :当 trim 元素内包含内容时,会把内容中匹配的前缀字符串去掉。
suffix :当 trim 元素内包含内容时,会给内容增加 suffix 指定的后缀。
suffixOverrides :当 trim 元素内包含内容时,会把内容中匹配的后缀字符串去掉。

<insert id ="insert"  parameterType = "com.test.entity.User" >
    insert tb_user
    <trim prefix="(" suffix = ")"  suffixOverrides = ",">
        user_id,
        user_name
    </trim>
    <trim prefix="values(" suffix = ")"  suffixOverrides = ",">
        #{userId,jdbc=VARCHAR},
        #{userName,jdbc=VARCHAR}
    </trim>
</insert>

7、bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中。

<if test=” userNarne != null and userNarne ! = ””>
and user name like concat ’, #{ userNarne },’
</if> 

使用 concat 函数连接字符串,在 SQL 中,这个函数支持多个参数,但在 Oracle 中只支持两个参数。由于不同数据库之间的语法差异 ,如果更换数据库,有些 SQL 语句可能就需要重写。针对这种情况,可 以使用 bind 标签来避免由于更换数据库带来的一些麻烦。将上面的方法改为 bind 方式后,代码如下。

<if test=” userNarne != null and userNarne !=””>
<bind narne= userNarneLike value ”’ ’+ userNarne ’”
and user name like #{userNarneLike} 
</if>
原文地址:https://www.cnblogs.com/yrjns/p/10877430.html