Mybatis 动态 Sql 语句的使用

一、环境的准备

(1)搭建数据库

create table blog1(
    id varchar(50) not null ,
    title varchar(100) not null ,
    author varchar(30) not null ,
    create_time datetime not null ,
    views int(30) not null
)

insert into mybatis.blog1 values ('1','我的第四次学习','鸭鸭','2022-04-23 10:12:58',11234);
insert into mybatis.blog1 values ('2','我的第四次学习','鸭鸭','2022-04-23 10:12:58',11234);
insert into mybatis.blog1 values ('3','我的第四次学习','鸭鸭','2022-04-23 10:12:58',11234);
insert into mybatis.blog1 values ('4','我的第四次学习','鸭鸭','2022-04-23 10:12:58',11234);

(2)创建对应实体类

(3)创建对应方法接口

(4)测试类

二、if 语句

<select id="blogIF" parameterType="map" resultType="com.lisi.pojo.Blog">
        select * from mybatis.blog1

        <where>
            <if test="title != null">-->
                and title = #{title}
            </if>

            <if test="author != null">
                and author = #{author}
            </if>
        </where>
    </select>

测试类;

 @Test
    public void test01(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        HashMap map = new HashMap();
//        map.put("title","我的第1次学习");
        map.put("author","狗狗");

        List<Blog> blogList = mapper.blogIF(map);
        for (Blog blog : blogList){
            System.out.println(blog);
        }

        sqlSession.close();
    }

当 title 和 author  都为空时则 查询全表,如果填入则在Sql语句  select * from mybatis.blog1 where 后面加上title 和 author 的条件;

二、choose 语句

        当我们不想用到全部的查询条件,只想选择其中一个,选择choose标签可以解决此类问题;类似于Java中的Switch 语句。

<select id="blogChoose" resultType="com.lisi.pojo.Blog" parameterType="map" >
        select * from mybatis.blog1
        <where>
        <choose>
            <when test="title!=null">
                title = #{title};
            </when>
            <when test="author!=null">
                author = #{author};
            </when>
            <otherwise>
                and views = #{views};
            </otherwise>

        </choose>

        </where>
    </select>

测试类;

@Test
    public void test02(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        HashMap map = new HashMap();
//        map.put("title","我的第2次学习");
        map.put("views","99999");
        List<Blog> blogList = mapper.blogChoose(map);
        for (Blog blog : blogList){
            System.out.println(blog);
        }

        sqlSession.close();

    }

当有一个满足条件后面的将不再执行;

三、set 语句

<update id="updateBlog" parameterType="map" >
        update mybatis.blog1
        <set>
            <if test="title !=null">
               title = #{title},
            </if>
            <if test="author !=null">
               author = #{author}
            </if>

        </set>
            where id = #{id}
    </update>

注意;set 是用 “,” 隔开的

测试类;

@Test
    public void test03(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        HashMap map = new HashMap();
        map.put("title","第二次学习");
        map.put("author","老虎");
        map.put("id","1");
        int num = mapper.updateBlog(map);

        sqlSession.close();
    }

四、Foreach 语句

遍历集合;

<!--
      collection:指定输入对象中的集合属性
      item:每次遍历生成的对象
      open:开始遍历时的拼接字符串
      close:结束时拼接的字符串
      separator:遍历对象之间需要拼接的字符串
      select * from blog where 1=1 and (id=1 or id=2 or id=3)
    -->
    <select id="blogForeach" parameterType="map" resultType="com.lisi.pojo.Blog">
       select * from mybatis.blog1
        <where>
            <foreach collection="ids" item="id" open="and (" close=")" separator="or">
                id = #{id}
            </foreach>

        </where>

    </select>

测试类;

@Test
    public void test04(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        HashMap map = new HashMap();
        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);
        ids.add(3);

        map.put("ids",ids);



        List<Blog> blogList= mapper.blogForeach(map);
        for (Blog blog:blogList){
            System.out.println(blog);
        }

        sqlSession.close();
    }

五、Sql 片段

        有时部分代码段频繁出现,为了提高代码的复用性,简化代码;我们将这些代码抽取出来,然后使用;

例如;

sql代码块部分:

<sql id="If-blog">

<if test="title != null">
and title = #{title}
</if>-->

<if test="author != null">
and author = #{author}
</if>

</sql>
    

<select id="blogIF" parameterType="map" resultType="com.lisi.pojo.Blog">
     select * from mybatis.blog1
     <where>
     <include refid="If-blog"/> //通过include标签,使用id引用
     </where>
        
</select>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值