Mybatis-(if,choose,foreache,sql)

目录

一.环境搭建

前言什么是动态SQL

动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。

二.动态查询--if语句

五.动态语句---sql标签

六.动态查询---foreach标签


一.环境搭建

创表-->插入数据-->配置mapper--->配置mybatis-config.xml

1.创建blog表并插入相关数据如下图:

Blog表
idtitlenameview
1小王子冰岛4
2线性代数

美国

5
3数据结构中国6
4概率论中国7

2.创建实体类blog,配置Blogmapper

BlogMapper(接口)

public interface BlogMapper{
    public List<Blog> queryBlogIf(Map map);
    public List<Blog> queryBlogChoose(Map map);
    public List<Blog> queryBlogForeach(Map map);
    public int updateBlog(Map map);
}

Blog.java

@Data
public class Blog {
    private String id;
    private String title;
    private String name;
    private int view;
}

 3.注册BlogMapper

    <mappers>
<mapper resource="Dao/BlogMapper.xml"/>
    </mappers>

前言什么是动态SQL

动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。


二.动态查询--if语句

  1. 创建方法接口
  2. 实现接口
    <select id="queryBlogIf" parameterType="map"  resultType="Blog">
        select *from mybatis.blog
    <where>
       <if test="title!=null">
        and title=#{title}
       </if>
        <if test="id!=null">
            and  id=#{id}
        </if>
       <if test="view">
            and view=#{view}
      </if>
    </where>
    </select>

sql源语句select *from blog where id=xx and title=xx and view=xx and id=xx

where标签     

  如果条件全部不满足,就会按select *from blog 查询结果展示

如果第一个条件满足,那么第一个条件的and就会被删除


三.动态查询--choose语句---->相当于java中switch语句,第一个命中了,后面的就不会实现,

  1. 创建方法接口
  2. 实现接口

 id-->方法名字;parametertype--->参数类型; resulttypoe--->返回类型

<choose>相当于Switch

<when>相当于case

</when>

</choose>

choose

    <select id="queryBlogChoose" parameterType="map" resultType="Blog">
select *from mybatis.blog
<where>
    <choose>
       <when test="id!=null">
         and id=#{id}
       </when>
       <when test="title!=null">
           and title=#{title}
       </when>
       <when test="view!=null">
           and view=#{view}
       </when>
      <when test="name!=null">
          and name=#{name}
      </when>
    </choose>
</where>
    </select>

 3.测试

    public void queryBlogChoose(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        map.put("id",1);
        map.put("title","小王子");
        List<Blog> blogs = mapper.queryBlogChoose(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

当你第一个给的是id,他只会读取到id,即使你后面给了title,它同样不会运行


五.动态语句---sql标签

sql的标签的作用:sql片段抽取公共部分语句重复使用

 用法:<sql id="自定义名字" >  想要重复利用的代码 </sql>

在需要使用的位置加上<include refid=“对应上面的id”/>即可

注意:最好基于单表来定义sql片段,sql中不要存在where标签-->

六.动态查询---foreach标签

foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量

废话不多说开干!

1.创建接口方法

2.在BlogMapper.XML中实现

我们现在传递一个万能的map,这个map里面可以存在集合

  <select id="queryBlogForeach" parameterType="map" resultType="Blog">
        select *from mybatis.blog
        <where>
            <foreach collection="ids" item="id" open="(" close=")" separator="or" >
             id=#{id}

            </foreach>
        </where>
    </select>

我只想查where 1=1( and(id=1,or id=2,or id=3)的数据。

collection="自定义一个id",什么不重要,;从集合里面遍历出来的每一项->item是元素名字 

open是开始的位置and( 如果是第一个元素,and会省略

close是关闭的符号)

separator=“or” 分隔符

3.测试

1.新建一个集合ArrayList 名字和上面的id对应

2.给ids集合加值

ids.add(1);ids.add(20=);

3.新建一个hash表

4.map.put("ids",ids);

    @Test
    public void queryBlogForeache(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(4);
        ids.add(1);
        map.put("ids",ids);
        List<Blog> blogs = mapper.queryBlogForeach(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值