Mybatis之动态SQL

Mybatis动态SQL

  1. 什么是动态SQL:动态SQL就是根据不同的条件生成不同的SQL语句
  2. 所谓的动态SQL,本质上还是SQL语句,只是我们可以在SQL层面,去执行一个逻辑代码

创建基础项目

实体类

package com.Mybatis.pojo;
import java.util.Date;
//需要get和set
public class Blog {
    private String id;
    private String title;
    private String author;
    private Date createTime;//和数据库字段不一样,
    //在Mybatis_config.xml开启驼峰命名映射只有下划线可以
    private int views;
    }

新东西:在Mybatis_config.xml添加驼峰命名自动映射

<settings>
<!--标准的日志工厂,注意大小写不能错,name和value里面不能有空格,否则报错-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
<!--是否开启驼峰命名自动映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

动态SQL:IF,where

这个where标签会知道如果它包含的标签中有返回值的话,它就插入一个where。此外,如果标签返回的内容是以 AND 或 OR 开头的,则它会剔除掉。

接口

//查询数据
List<Blog> getBlog(Map map);

SQL语句

<select id="getBlog" resultType="Blog">
    select * from jdbc.blog
    <where>
        <if test="title !=null">
            title = #{title}
        </if>
        <if test="author !=null">
            and author = #{author}
        </if>
    </where>
</select>

测试

@org.junit.Test
public void get(){
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    Blog blog = new Blog();
    blog.setTitle("Java");//获取数据库title字段中带有Java的数据
    blog.setAuthor("狂神说");
    List<Blog> blog1 = mapper.getBlog(blog);
    for (Blog blog2 : blog1) {
        System.out.println(blog2);
    }
    sqlSession.close();
}

动态SQL:choose、when、otherwise

有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句

接口

List<Blog> getBlog2(Blog blog);

SQL语句

<select id="getBlog2" parameterType="Blog" resultType="Blog">
    select *from jdbc.blog
    <where>
        <choose>
            <when test="title !=null">
                title = #{title}
            </when>
            <when test="author!=null">
                and author = #{author}
            </when>
            <otherwise>
                and views = #{views}
            </otherwise>
        </choose>
    </where>
</select>

注:这里的三个条件在查询的时候只要满足一个条件的时候就可以查询处理,如果有三个指定条件,那么默认查询SQL中第一个title字段

测试

@org.junit.Test
public void get2(){
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    Blog blog = new Blog();
    blog.setAuthor("狂神说");
    blog.setTitle("Java");//默认查询SQL中title字段
    blog.setViews(1000);
    List<Blog> blog1 = mapper.getBlog2(blog);
    for (Blog blog2 : blog1) {
        System.out.println(blog2);
    }
    sqlSession.close();
}

动态SQL:set标签

注:set元素就可以实现此功能。set元素遇到逗号,它会自动将对应的逗号去掉。

接口

//更新数据
int updateBlog(Blog blog);

SQL语句

<update id="updateBlog" parameterType="Blog">
    update jdbc.blog
    <set>
        <if test="title!=null">
            title = #{title},//后面必须加逗号
        </if>
        <if test="author!=null">
            author =#{author}
        </if>
        where views = #{views}
    </set>
</update>

测试

@org.junit.Test
public void get3(){
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    Blog blog = new Blog();
    blog.setTitle("SpringBoot");
    blog.setAuthor("狂神");
    blog.setViews(1000);//把数据库字段views中等于1000的数据更改
    mapper.updateBlog(blog);
    sqlSession.close();
}

动态SQL:SQL片段

有的时候,我们可能会将一些功能的部分抽取出来,方便复用!
使用SQL标签抽取公共部分可,在需要使用的地方使用Include标签引用即可
注:最好基于简单的查询来定义SQL片段
不要存在where标签

接口

List<Blog> getBlog(Blog blog);

SQL语句

<sql id="if">
    <if test="title !=null">
        title = #{title}
    </if>
    <if test="author !=null">
        and author = #{author}
    </if>
</sql>
<select id="getBlog" parameterType="Blog" resultType="Blog">
    select * from jdbc.blog
    <where>
    //引入SQL
        <include refid="if"></include>
    </where>
</select>

测试

@org.junit.Test
public void get(){
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    Blog blog = new Blog();
    blog.setAuthor("狂神说");
    List<Blog> blog1 = mapper.getBlog(blog);
    for (Blog blog2 : blog1) {
        System.out.println(blog2);
    }
    sqlSession.close();
}

动态SQL:foreach

接口

List<Blog> ForE(Map map);

SQL语句

<select id="ForE" parameterType="map" resultType="Blog">
    select *from jdbc.blog
    <where>
    //select *from jdbc.blog where 1=1 and(id=1 or id=2 or id=3)
        //collection属性类型item要查询的字段open要拼接的开头两个字符之间必须有空格 close要拼接的尾
        <foreach collection="ids" item="id" open="and ("  separator="or" close=")">
            id = #{id}
        </foreach>
    </where>
</select>

测试

@org.junit.Test
public void get4(){
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    HashMap hashMap = new HashMap();
    ArrayList<Integer> ids = new ArrayList<Integer>();
    ids.add(1);
    ids.add(2);
    hashMap.put("ids",ids);
    List<Blog> blogs = mapper.ForE(hashMap);
    for (Blog blog : blogs) {
        System.out.println(blog);
    }
    sqlSession.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值