04_MyBatis动态SQL

  -------------------------------
  - if
  - choose (when, otherwise)
  - trim (where, set)
  - foreach
  -------------------------------

1.1 if

在这里插入图片描述但是会出现问题:

 <select id="queryBlogif" parameterType="map" resultType="com.xxxx.pojo.Blog">
        select * from blog where
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </select>
Preparing: select * from blog where and title = ? and author = ? 

用Where解决问题

1.2 Where

在这里插入图片描述

1.3 choose

在这里插入图片描述

1.4 Set

<update id="updateBlog" parameterType="map">
        update blog
        <set>
            <if test="title != null">
                title = #{title},
            </if>
            <if test="author != null">
                author = #{author}
            </if>
        </set>
        where id = #{id};
    </update>

1.5 sql片段

有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。
提取SQL片段:

 <sql id="if">
        <if test="title != null">
            title = #{title},
        </if>
        <if test="author != null">
            author = #{author}
        </if>
    </sql>

引用SQL片段:

 <update id="updateBlog" parameterType="map">
        update blog
        <set>
            <include refid="if"></include>
        </set>
        where id = #{id};
    </update>

1.6 Foreach

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxxx.dao.BlogMapper">
    <!--
    collection:指定输入对象中的集合属性
    item:每次遍历生成的对象
    open:开始遍历时的拼接字符串
    close:结束时拼接的字符串
    separator:遍历对象之间需要拼接的字符串
    select * from blog where 1=1 and (id=1 or id=2 or id=3)
-->
   <select id="queryBlogForeach" parameterType="map" resultType="com.xxxx.pojo.Blog">
       select * from blog
       <where>
           <foreach collection="ids"  item="id" open="and (" close=")" separator="or">
           id=#{id}
           </foreach>
       </where>
   </select>
</mapper>

public class DaoTest {
    //注意导包:org.apache.log4j.Logger
    static Logger logger = Logger.getLogger(DaoTest.class);
    @Test
    public  void test(){
       try(SqlSession session = MyBatisUtils.getSqlSesion();){
           BlogMapper mapper = session.getMapper(BlogMapper.class);
           Map<String, List> map = new HashMap<>();
           List<Integer> ids = new ArrayList<>();
           ids.add(1);
           ids.add(2);
           ids.add(3);
           map.put("ids" , ids);
           List<Blog> blogs = mapper.queryBlogForeach(map);
           for (Blog blog : blogs) {
               System.out.println(blog);
           }
       }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值