Mybatis之动态SQL

动态SQL就是可以根据不同的条件生成不同的SQL语句
在这里插入图片描述
在这里插入图片描述

package com.lei.pojo;

import java.util.Date;

public class Blog {
    private String id;
    private String title;
    private String author;
    private Date createTime;
    private int views;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public int getViews() {
        return views;
    }

    public void setViews(int views) {
        this.views = views;
    }

    @Override
    public String toString() {
        return "blog{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", createTime=" + createTime +
                ", views=" + views +
                '}';
    }
}

package com.lei.utils;

import java.util.UUID;

public class IdUtils {
    public static String getId() {
        return UUID.randomUUID().toString().replaceAll("-","");
    }


}
package com.lei.dao;

import com.lei.pojo.Blog;

import java.util.List;
import java.util.Map;

public interface BlogMapper {
    int addBlog(Blog blog);
    List<Blog> findBlogIF(Map map);
}

 @Test
    public void addBlog() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Blog blog = new Blog();
        blog.setId(IdUtils.getId());
        blog.setTitle("Mybatis1");
        blog.setAuthor("王五");
        blog.setCreateTime(new Date());
        blog.setViews (9999);
        mapper.addBlog(blog);
        Blog blog1 = new Blog();
        blog1.setId(IdUtils.getId());
        blog1.setTitle( "mybatis2");
        mapper .addBlog(blog1);
        Blog blog2 = new Blog();
        blog2.setId(IdUtils.getId());
        blog2.setTitle( "mybatis3");
        mapper.addBlog(blog2);
        Blog blog3 = new Blog();
        blog3.setId (IdUtils.getId());
        blog3.setTitle("mybatis4");
        mapper.addBlog(blog3);

        sqlSession.close();

    }

if

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lei.dao.BlogMapper">
    <insert id="addBlog" parameterType="com.lei.pojo.Blog">
        insert into blog values(#{id},#{title},#{author},#{createTime},#{views})
    </insert>

    <select id="findBlogIF" parameterType="Map" resultType="com.lei.pojo.Blog">
        select * from blog where 1=1
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
        and author = #{author}
        </if>
    </select>

</mapper>

在这里插入图片描述

where

但上面1=1这样的SQL是不规范的,要想进行多个条件的筛选然后拼装SQL,都需要在前面加一个and,但现在我们有了where标签,可以帮我们自动去掉字句开头的and。

    <select id="findBlogIF" parameterType="Map" resultType="com.lei.pojo.Blog">
        select * from blog
        <where>
            <if test="title != null">
                and title = #{title}
            </if>
            <if test="author != null">
                and author = #{author}
            </if>
        </where>

    </select>

在这里插入图片描述
他会自动把第一个and去掉。

where,choose,when

  <select id="findBlogChoose" parameterType="Map" resultType="com.lei.pojo.Blog">
         select * from 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>
 @Test
    public void findBlogIF() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();
        map.put("title","mybatis3");
        map.put("author","王五");
        map.put("views",9999);
        List<Blog> blogIF = mapper.findBlogIF(map);
        for (Blog blog:blogIF) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

在这里插入图片描述
如果都不满足就会走otherwise这个。

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>
package com.lei.dao;

import com.lei.pojo.Blog;

import java.util.List;
import java.util.Map;

public interface BlogMapper {
    int addBlog(Blog blog);
    List<Blog> findBlogIF(Map map);
    List<Blog> findBlogChoose(Map map);
    int updateBlog(Map map);
}
   @Test
    public void updateBlog() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Map map = new HashMap();
        map.put("title","mybatis311");
        map.put("author","王五11");
        map.put("id", "a6f0037b230044fbabacc788387cf2e8");
        mapper.updateBlog(map);
        sqlSession.close();
    }

在这里插入图片描述

foreach

在这里插入图片描述

package com.lei.dao;

import com.lei.pojo.Blog;

import java.util.List;
import java.util.Map;

public interface BlogMapper {
    int addBlog(Blog blog);
    List<Blog> findBlogIF(Map map);
    List<Blog> findBlogChoose(Map map);
    int updateBlog(Map map);
    //查询1,2,3号博客
    List<Blog> findBlogForeach(Map map);
}
<!--    select * from blog where 1=1 and (id=1 or id = 2 or id=3)-->
<!--    我们可以传递一个map,这个map里面可以存在一个集合-->
    <select id="findBlogForeach" parameterType="Map" resultType="com.lei.pojo.Blog">
        select * from blog
        <where>
            <foreach collection="ids" item="id" open="and (" close=")" separator="or">
                id=#{id}
            </foreach>
        </where>
    </select>
    @Test
    public void findBlogForeach() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        ArrayList<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(3);
        map.put("ids",ids);
        List<Blog> blogForeach = mapper.findBlogForeach(map);
        for (Blog blog:blogForeach) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lhj_loveFang_1105

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值