zoukankan      html  css  js  c++  java
  • Mybatis系列三-动态sql和缓存快速了解

    mybatis03-动态SQL

    动态sql的出现大大解决了复杂sql的编写和灵活得去运用sql去提高效率。动态sql就是在拼接sql语句,然后组合使用。

    搭建环境

    先把整个环境搭建

    创建数据库

    image-20200519134730685

    搭建idea环境,编写对应的pojo

    image-20200519135142124

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

    dao层的实体类和对应的xml文件

    这里写多了一个工具类,是为了生成一个随机的id

    image-20200519144024756

    //这是BlogMapper接口
    package com.yhy.dao;
    
    import com.yhy.pojo.Blog;
    
    public interface BlogMapper {
    //    编写添加方法
        //往数据库添加信息
        int addBlog(Blog blog);
    }
    
    

    BlogMapper.xml

    <?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">
    <!--namespace=绑定一个对应的Dao/Mapper接口 原本是写接口,但是现在不用怎么做-->
    
    <mapper namespace="com.yhy.dao.BlogMapper">
    
        <insert id="addBlog" parameterType="blog">
            insert into mybatis.blog(id, title, author, create_time, views) values(
            #{id},#{title},#{author},#{createTime},#{views});
        </insert>
    
    </mapper>
    
    

    测试

    import com.yhy.dao.BlogMapper;
    import com.yhy.pojo.Blog;
    import com.yhy.utils.IDutils;
    import com.yhy.utils.MybatisUtils;
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    
    import java.util.Date;
    
    /**
     * @Author: yhy
     * @Date: 2020/5/19
     * @Time: 14:17
     */
    public class Mytest {
        @Test
        public void test(){
            SqlSession sqlSession = MybatisUtils.getSqlSession();
            BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    
            Blog blog = new Blog();
            blog.setAuthor("郝老师");
            blog.setId(IDutils.getId());
            blog.setTitle("web快速入门");
            blog.setViews(1000);
            blog.setCreateTime(new Date());
            mapper.addBlog(blog);
    
            blog.setId(IDutils.getId());
            blog.setTitle("Java快速入门");
            blog.setCreateTime(new Date());
            mapper.addBlog(blog);
    
    
            blog.setId(IDutils.getId());
            blog.setTitle("python快速入门");
            blog.setViews(1000);
            blog.setCreateTime(new Date());
            mapper.addBlog(blog);
    
    
            sqlSession.close();
    
        }
    
    
    }
    
    

    最后结果-成功插入数据库

    image-20200519144454891

    image-20200519144823879

    动态sql实践

    其实官网上也讲的很清楚。官网介绍

    测试if语句

    1、首先接口上的方法定义

    //测试使用动态sql
    List<Blog> queryBlogIf(Map map);
    

    2、配置文件上写动态sql

    <select id="queryBlogIf" parameterType="map" resultType="blog">
        select * from mybatis.blog where 1=1
        //上面这一行是匹配if没有传入,这用全部查询
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
    </select>
    

    3、最后是测试,查看结果

    示例一

    @Test
    public void queryBlog(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    //这里是直接传入一个空的map,测试全部查询是什么情况。
        HashMap map =new HashMap<>();
    
        List<Blog> blogs = mapper.queryBlogIf(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
    
        }
        sqlSession.close();
    
    
    }
    

    image-20200519151335886

    示例二

    @Test
    public void queryBlog(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    
        HashMap map =new HashMap<>();
        
    //测试动态查询。给出一个title
        String tit = "Java快速入门";
        map.put("title",tit);
    
        List<Blog> blogs = mapper.queryBlogIf(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
    
        }
        sqlSession.close();
    
    
    }
    

    结果,正确查出答案。

    image-20200519203455739

    其他语句

    image-20200519203835178

    where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。

    如果不用where标签,当第一个if不起作用的时候原来的语句就是这样SELECT * FROM BLOG where and..就会报错。

    <select id="findActiveBlogLike"
         resultType="Blog">
      SELECT * FROM BLOG
      <where>
        <if test="state != null">
             state = #{state}
        </if>
        <if test="title != null">
            AND title like #{title}
        </if>
        <if test="author != null and author.name != null">
            AND author_name like #{author.name}
        </if>
      </where>
    </select>
    

    示例-update中的set

    使用set标签用来更新数据,同时set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)。就是说在写sql的时候,他会自动省略掉逗号等,其实我没有加逗号也是可以运行的

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

    image-20200519210427762

    image-20200519210407152

    mybatis04-缓存

    前言

    缓存是什么?

    有分硬件的缓存和软件的缓存。

    硬件的是cpu缓存,在cpu和内存之间的临时存储器

    软件的常见的有数据库缓存、内存缓存、网络缓存

    存在内存中的数据,减少与数据库的直接交互,减少开销,提高效率

    缓存的淘汰机制

    一级缓存

    sqlsession

    一级缓存中默认开启,一次session有效

    在查询同一个数据的时候,就会启动缓存,但有时涉及增删改,一级缓存就会失效。

    二级缓存

    二级缓存也叫全局缓存

    在使用的时候需要加一个cache标签

    步骤

    1. 开启全局缓存,在setting中设置-cacheenabled
    2. 在所需的mapper.xml中配置,也可配置缓存的一些参数,自定义参数--比如多久刷新,存的数据大小
    3. 在会话关闭的时候,一级缓存的数据就会给二级缓存,这样就是使用二级缓存的好处。

    只要开启了二级缓存,在同一个mapper下都会有效。

    拓展-ehcache

    使用广泛的java分布式缓存-自定义缓存,了解一下就可以,可以在maven仓库上找

    使用先导包,开启,在mapper中指定。

    <cache type=....ehcache>//补全版本使用自己导包
    

    现在主流的都是使用redis了。

    总结

    1. 用户取数据的时候,先是二级缓存,再到一级缓存,再到数据库。

    2. 一级缓存的会话关闭的时候,一级缓存会存到二级缓存,不过都会有时间限制

  • 相关阅读:
    HAOI2008题解
    codeforces round375(div.2)题解
    codeforces round373(div.2) 题解
    TJOI2015题解
    CF976D. Degree Set
    dtoj#4243. 熊猫(i)
    dtoj#4242. 大爷(w)&&CF1061E
    CF786C Till I Collapse
    dtoj#4239. 删边(cip)
    dtoj#2504. ZCC loves cube(cube)
  • 原文地址:https://www.cnblogs.com/yhycoder/p/12929496.html
Copyright © 2011-2022 走看看