Mybaits--动态拼接SQL语句之常用标签(包含一对多双向配置所用标签)

来源:互联网 发布:网络不文明行为 编辑:程序博客网 时间:2024/05/29 15:39

先看一下有哪些常用标签

这里写图片描述

where标签 及使用前后效果对比

使用where标签前    <select id="queryMessageList" parameterType="com.imooc.bean.Message"        resultMap="MessageResult">        select ID,COMMAND,DESCRIPTION,CONTENT from MESSAGE where 1=1        <!-- &&=&amp;&amp;=and " "=&quot;&quot; #{} = ? -->        <if test="command !=null &amp;&amp;!&quot;&quot;.equals(command.trim())">            and COMMAND=#{command}        </if>        <if test="description != null and !&quot;&quot;.equals(description.trim())">            and DESCRIPTION like '%' #{description} '%'        </if>    </select>使用where标签后(仅仅少了where1=1这个小技巧)<select id="queryMessageList" parameterType="com.imooc.bean.Message"        resultMap="MessageResult">        select ID,COMMAND,DESCRIPTION,CONTENT from MESSAGE        <where>            <!-- &&=&amp;&amp;=and " "=&quot;&quot; #{} = ? -->            <if test="command !=null &amp;&amp;!&quot;&quot;.equals(command.trim())">                and COMMAND=#{command}            </if>            <if                test="description != null and !&quot;&quot;.equals(description.trim())">                and DESCRIPTION like '%' #{description} '%'            </if>        </where>    </select>

sql 标签 及引用

作用:

1.相当于定义一个常量属性,用的时候就进行引用,提升代码的复用性。

2.数据库字段的扩展不会对原有的sql语句产生影响。

<sql id="columns">ID,COMMAND,DESCRIPTION,CONTENT</sql><select id="queryMessageList" parameterType="com.imooc.bean.Message"        resultMap="MessageResult">        select <include refid="columns"></include> from MESSAGE        <where>            <!-- &&=&amp;&amp;=and " "=&quot;&quot; #{} = ? -->            <if test="command !=null &amp;&amp;!&quot;&quot;.equals(command.trim())">                and COMMAND=#{command}            </if>            <if                test="description != null and !&quot;&quot;.equals(description.trim())">                and DESCRIPTION like '%' #{description} '%'            </if>        </where>    </select>

set 标签

作用:在update语句中解决动态修改多个值SQL语句的拼接问题

  <update id="">        update MESSAGE        <set>            <if test="command !=null &amp;&amp;!&quot;&quot;.equals(command.trim())">                COMMAND=#{command},            </if>            <if test="description != null and !&quot;&quot;.equals(description.trim())">                DESCRIPTION = #{description},            </if>        </set>    </update>

trim 标签

作用:根据项目需求,灵活解决动态SQL语句的拼接问题

  相当于where标签自动去除SQL语句where前边的and/or  suffix="后缀,下标"  代表如果标签中有输入值则输出suffix="后缀,下标"定义的内容  <trim prefix="where" suffix="后缀,下标" prefixOverrides="and/or">  </trim> 相当于set标签自动去除SQL语句后边的','         <trim prefix="set" suffixOverrides=","> </trim>

choose 标签

作用:相当于

if(){}

elseif(){}

else{},根据逻辑条件动态拼接SQL语句

<select id="queryMessageList" parameterType="com.imooc.bean.Message"        resultMap="MessageResult">        select        <include refid="columns"></include>        from MESSAGE        <where>            <choose>                <when                    test="description != null and !&quot;&quot;.equals(description.trim())">                    and DESCRIPTION like '%' #{description} '%'                </when>                <when                    test="command !=null &amp;&amp;!&quot;&quot;.equals(command.trim())">                    and COMMAND=#{command}                </when>                <otherwise>and COMMAND=#{command}</otherwise>            </choose>        </where>    </select>    <sql id="columns">ID,COMMAND,DESCRIPTION,CONTENT</sql>

association 标签 (多对一关联映射)

作用:做关联查询的时候把父表的数据,映射到子表实体类中引用父类的对应【父类对象属性】中,通过此属性可以取出父表中的取值

 <mapper namespace="CommandContent">  <resultMap type="com.imooc.bean.CommandContent" id="Content">    <id column="ID" jdbcType="INTEGER" property="id"/>    <result column="CONTENT" jdbcType="VARCHAR" property="content"/>    <result column="COMMAND_ID" jdbcType="VARCHAR" property="commandId"/>    <association property="字表引用父表的属性名" resultMap="(父表配置中)namespace.resultMap-id"></association>  </resultMap></mapper>

collection 标签 (一对多关联映射)

作用:做关联查询的时候把子表的数据,映射到父表实体类中引用子类的对应【子类集合属性】中,通过此属性可以取出子表中的取值

    <resultMap type="com.imooc.bean.Command" id="Command">        <!-- column对应的为sql语句中字段名,如果给字段取别名,column必须配置为别名才能被识别 -->        <id column="C_ID" jdbcType="INTEGER" property="id" />        <result column="NAME" jdbcType="VARCHAR" property="name" />        <result column="DESCRIPTION" jdbcType="VARCHAR" property="description" />        <!-- 【一方】配置文件中【引用】 【一方】实体类定义的【多方】的集合 -->        <collection property="contentList" resultMap="CommandContent.Content" />    </resultMap></mapper>

forech 标签 (遍历集合)

作用:多用于查询语句的in语句中,用来遍历集合,达到select * from tablename where column in (1,2,3,4…,n)语句的效果;

    <!--包含递归算法的in语句和遍历集合-->    <select id="selectByNameAndCategoryIds" resultMap="BaseResultMap" parameterType="map">        SELECT        <include refid="Base_Column_List"></include>        from mmall_product        where status = 1        <if test="productName!=null">            AND name LIKE #{productName}        </if>        <if test="categoryIdList !=null">            and category_id in             <foreach collection="categoryIdList" item="item" open="(" close=")" index="indext" separator=",">                #{item}            </foreach>        </if>    </select></mapper>
原创粉丝点击