mybatis动态SQL

动态 SQL-参考链接
有兴趣深究的可以去查看mybatis-3-mapper.dtd这个文件。
http://mybatis.org/dtd/mybatis-3-config.dtd

1.组装update

    <update id="updateByPrimaryKeySelective"
            parameterType="simuhunluo.top.Node">
        update node
        <set>
            <if test="nodeName != null">
                node_name = #{nodeName,jdbcType=VARCHAR},
            </if>
            <if test="note != null">
                note = #{note,jdbcType=VARCHAR},
            </if>
        </set>
        where node_id = #{nodeId,jdbcType=INTEGER}
    </update>

使用set,if 。第二个if判断里面也是有逗号结尾,真正组装成sql之后,提供的功能会把最后的“,”给trim掉。

2.组装select

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  <where> 
    <if test="state != null">
         state = #{state}
    </if> 
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
  </where>
</select>

使用where,if 。如果第一个if没匹配到,匹配到第二个if,开头的AND会被给去掉trim。OR也会被去掉(如果有OR的话)。

3.组装insert

上面的,都是已经实现好的功能(等价于自定义trim),如果没有自己需要的,可以自定义trim。比如insert:

    <insert id="insertSelective"
            parameterType="simuhunluo.top.Node">
        insert into node  
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="nodeId != null">
                node_id,
            </if>
            <if test="nodeName != null">
                node_name,
            </if>
            <if test="note != null">
                note,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="nodeId != null">
                #{nodeId,jdbcType=INTEGER},
            </if>
            <if test="nodeName != null">
                #{nodeName,jdbcType=VARCHAR},
            </if>
            <if test="note != null">
                #{note,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

prifix前缀;
suffix后缀;
suffixOverrides后缀赘余符,第三个note如果匹配到,后面的逗号会被处理掉;
同样也有前缀赘余符,prefixOverrides 。比如先前的,如果自定义实现组装select的话:

<select id="findActiveBlogLike" resultType="Blog">
    SELECT * FROM BLOG 
    <trim prefix="WHERE" prefixOverrides="AND |OR ">
          <if test="state != null">
             state = #{state}
        </if> 
        <if test="title != null">
            AND title like #{title}
        </if>
        <if test="author != null and author.name != null">
            AND author_name like #{author.name}
        </if>
    </trim>
</select>

如果要like做%?%通配查询,可以用以下三种方式:

  1. 传参数直接用%%包裹传过来
  2. concat(推荐用这种方式)
    <select id="selectConditional" parameterType="*******Node" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from node
        <where>
            <if test="nodeId != null">
                and node_id = #{nodeId}
            </if>
            <if test="nodeName != null">
                and node_name like concat(concat('%',#{nodeName}),'%')
            </if>
        </where>
    </select>
实际上在输出sql的时候(我的搜索内容是“tn”),组装的sql如下:
2018-07-03 14:21:42  DEBUG ****.mapper.NodeMapper.selectConditional ==>  Preparing: select node_id, node_name, note from node WHERE node_name like concat(concat('%',?),'%') 
2018-07-03 14:21:42  DEBUG ****.mapper.NodeMapper.selectConditional ==> Parameters: tn(String)
  1. bind(这种方式存在一定的问题,比如下面代码中如果nodeName传过来的时候是null会报错,建议用方式2 concat)
    <select id="selectConditional" parameterType="*****.dao.model.Node" resultMap="BaseResultMap">
        <bind name="nodeName" value="'%'+nodeName+'%'"/>
        select
        <include refid="Base_Column_List"/>
        from node
        <where>
            <if test="nodeId != null">
                and node_id = #{nodeId}
            </if>
            <if test="nodeName != null">
                and node_name like #{nodeName}
            </if>
        </where>
    </select>
实际上在输出sql的时候(我的搜索内容是“tn”),组装的sql如下:
2018-07-03 14:24:53  DEBUG ****.mapper.NodeMapper.selectConditional ==>  Preparing: select node_id, node_name, note from node WHERE node_name like ? 
2018-07-03 14:24:53  DEBUG ****.mapper.NodeMapper.selectConditional ==> Parameters: %tn%(String)

4. delete

posted @ 2018-07-03 16:00  须小弥  阅读(529)  评论(0编辑  收藏  举报