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

先看一下有哪些常用标签

这里写图片描述

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>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus 是一个 MyBatis 的增强工具,它提供了许多功能,其中之一就是简化 SQL 的编写。下面演示一些 MyBatis-Plus 简化 SQL 的例子: 1. 查询条件构造器 使用 QueryWrapper 可以方便地构造查询条件,例如: ``` QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("name", "Tom").ge("age", 18).orderByDesc("id"); List<User> userList = userMapper.selectList(wrapper); ``` 这个例子中,我们使用 QueryWrapper 来构造查询条件,其中 eq 表示等于,ge 表示大于等于,orderByDesc 表示降序排序。最后使用 selectList 方法查询符合条件的数据。 2. 分页查询 使用 Page 对象可以轻松实现分页查询,例如: ``` Page<User> page = new Page<>(1, 10); IPage<User> userPage = userMapper.selectPage(page, null); List<User> userList = userPage.getRecords(); ``` 这个例子中,我们创建了一个 Page 对象,指定查询第一页的 10 条数据。然后使用 selectPage 方法查询符合条件的数据,并将结果封装在一个 IPage 对象中。最后从 IPage 对象中获取查询结果。 3. Lambda 表达式 使用 Lambda 表达式可以更加简洁地构造查询条件,例如: ``` List<User> userList = userMapper.selectList(Wrappers.<User>lambdaQuery() .eq(User::getName, "Tom") .ge(User::getAge, 18) .orderByDesc(User::getId)); ``` 这个例子中,我们使用 Wrappers.lambdaQuery() 创建一个 LambdaQueryWrapper 对象,然后使用 eq 方法指定查询条件,ge 方法指定大于等于条件,orderByDesc 指定降序排序。最后使用 selectList 方法查询符合条件的数据。 以上是 MyBatis-Plus 简化 SQL 的几个例子,这些功能可以大大简化 SQL 的编写,提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值