美文网首页
mybatis动态SQL

mybatis动态SQL

作者: 须小弥 | 来源:发表于2018-06-26 18:27 被阅读0次

动态 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之后,<set></set>提供的功能会把最后的“,”给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会被<where></where>给去掉trim。OR也会被去掉(如果有OR的话)。

3.组装insert

上面的<set>,<where>都是已经实现好的功能(等价于自定义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 。比如先前的<where>,如果自定义实现组装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

相关文章

  • MyBatis动态SQL

    MyBatis 动态SQL 内容 Mybatis动态SQL在XML中支持的几种标签: if chose trim、...

  • MyBatis核心知识点

    (1)Mybatis动态sql是做什么的?都有哪些动态sql?能简述一下动态sql的执行原理不? Mybatis动...

  • MyBatis 动态SQL(*.xml)

    原文参考MyBatis 动态SQL MyBatis的动态SQL大大减少了拼接SQL语句时候的各种格式问题,这里摘录...

  • Mybatis动态SQL

    MyBatis Mybatis笔记连载上篇连接MyBatis缓存Mybatis笔记连载下篇连接 动态SQL 动态S...

  • MyBatis的动态SQL与日志Log4J、SQL语句构造器

    一、MyBatis动态SQL 动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似...

  • MyBatis学习:动态sql

    1.动态sql 动态sql是mybatis中的一个核心,什么是动态sql?动态sql即对sql语句进行灵活操作,通...

  • mybatis的xml文件的标签详解

    Mybatis #{}和${}和区别 mybatis获取方法参数 动态SQL

  • 第八章 动态SQL

    动态SQL中的元素介绍 动态SQL有什么作用 MyBatis提供了对SQL语句动态组装的功能 动态SQL中的元素 ...

  • JavaEE基础知识学习----MyBatis(四)动态SQL

    MyBatis的动态SQL MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似...

  • IT 每日一结

    mybatis动态sql 动态sql绝对是mybatis排列前几的闪光点之一。传统代码中的sql语句需要经过多个字...

网友评论

      本文标题:mybatis动态SQL

      本文链接:https://www.haomeiwen.com/subject/aevmyftx.html