MyBatis学习笔记5

news/2024/7/24 4:54:41 标签: mybatis, 学习, 笔记

动态SQL之IF语句

1.接口

List<Blog> queryBlogIF(Map map);

2.xml文件

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

3.测试

@Test
    public void queryBlogID(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        map.put("title","微服务");
        map.put("author","狂神说");
        List<Blog> blogs = mapper.queryBlogIF(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

choose使用

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

        </where>
    </select>

测试

public void queryBlogChoose(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap map = new HashMap();
        //map.put("title","微服务");
        //map.put("author","狂神说");
        map.put("views","9999");
        List<Blog> blogs = mapper.queryBlogChoose(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

set设置

<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>

测试

public void updateBlog(){
        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 blogs = mapper.updateBlog(map);
        System.out.println(blogs);
        sqlSession.close();
    }

sql复用

1.使用SQL标签抽取公共部分

<sql id="if-title-author">
        <if test="title!=null">
            title=#{title},
        </if>
        <if test="author!=null">
            author=#{author}
        </if>
    </sql>

2.在需要使用的地方,使用Include标签引用

<update id="updateBlog" parameterType="map">
        update mybatis.blog
        <set>
            <include refid="if-title-author"/>
        </set>

        where id=#{id}
    </update>

注:1.最好基于单表定义SQL片段

2.不存在where 标签

foreach遍历

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

测试

@Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        ArrayList<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(2);
        ids.add(3);

        HashMap map = new HashMap();
        map.put("ids",ids);
        List<Blog> blogs = mapper.queryBlogforeach(map);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

缓存

MyBatis 内置了一个强大的事务性查询缓存机制,它可以非常方便地配置和定制。 为了使它更加强大而且易于配置,我们对 MyBatis 3 中的缓存实现进行了许多改进。

默认情况下,只启用了本地的会话缓存,它仅仅对一个会话中的数据进行缓存。 要启用全局的二级缓存,只需要在你的 SQL 映射文件中添加一行:

一级缓存默认开启,在一个sqlsession中

二级缓存需手动开启是全局缓存

1.配置文件

<setting name="cacheEnabled" value="true"/>

2.Mapper

 <cache eviction="FIFO"
           flushInterval="60000"
           size="512"
           readOnly="true"/>

3.测试

@Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user1 = mapper.getUser(1);
        System.out.println(user1);
        sqlSession.close();

        System.out.println("+++++++++++++++++++");
        sqlSession.close();
        SqlSession sqlSession2 = MybatisUtils.getSqlSession();
        UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
        User user2 = mapper2.getUser(1);
        System.out.println(user2);
        System.out.println(user1==user2);
        sqlSession2.close();

    }

缓存总结:1.只要开启了二级缓存,在同一个Mapper中有效

  1. 所有数据会先放在一级缓存中
  2. 只有会话提交,或者关闭的时候,才会提交到二级缓存中
  3. 缓存是为了提高查询效率

http://www.niftyadmin.cn/n/4926874.html

相关文章

C#版本LINQ增强开源库

LINQ对应的中文名称是&#xff1a;语言集成查询&#xff0c;是对一系列直接将查询功能集成到C#语言技术的统称&#xff0c;我们最常用的场景是&#xff0c;用于数据库的查询功能。 在.Net中&#xff0c;本身对Objects也支持LINQ的扩展&#xff0c;但是对于一些特定的功能&…

910数据结构(2014年真题)

算法设计题 问题1 已知一个带头结点的单链表head&#xff0c;假设结点中的元素为整数&#xff0c;试编写算法&#xff1a;按递增次序输出单链表中各个结点的数据元素&#xff0c;并释放结点所占的存储空间。要求&#xff1a;(1)用文字给出你的算法思想&#xff1b;(2)不允许使…

备战秋招013(20230809)

文章目录 前言一、今天学习了什么&#xff1f;二、动态规划1.完全背包问题2.题目 三、牛客网华为机试1、简介2、ACM2、题目 三、SQL1、增删改部分 总结 前言 提示&#xff1a;这里为每天自己的学习内容心情总结&#xff1b; Learn By Doing&#xff0c;Now or Never&#xff…

Pytorch量化之Post Train Static Quantization(训练后静态量化)

使用Pytorch训练出的模型权重为fp32&#xff0c;部署时&#xff0c;为了加快速度&#xff0c;一般会将模型量化至int8。与fp32相比&#xff0c;int8模型的大小为原来的1/4, 速度为2~4倍。 Pytorch支持三种量化方式&#xff1a; 动态量化&#xff08;Dynamic Quantization&…

go的make使用

在 Go 语言中&#xff0c;make 是一个用于创建切片、映射&#xff08;map&#xff09;和通道&#xff08;channel&#xff09;的内建函数。它提供了一种初始化和分配内存的方式&#xff0c;用于创建具有特定长度和容量的数据结构。下面将详细介绍 make 函数的使用方法和各种情况…

nginx负载均衡(nginx结束)

本节主要内容 1、四层&#xff0c;七层代理的配置方法 2、负载均衡的算法 nginx负载均衡&#xff1a;反向代理来实现 反向代理有两种转发方式&#xff1a;1、四层代理 2、七层代理 Nginx的七层代理和四层代理 七层是最常见的反向代理方式&#xff0c;只能配置在nginx配置文…

Vue 本地应用 记事本 v-on v-model v-for使用

新增功能 vue当中如何生成列表结构&#xff1f;使用的指令是v-for&#xff0c;同时要有一个可以生成列表的数据&#xff0c;常用的是数组。记事本里面的内容并不复杂&#xff0c;所以这里使用字符串数组就行了。 获取用户输入的内容使用绑定v-model&#xff0c;双向数据绑定&a…

前沿分享-无创检测血糖RF波

非侵入性血糖仪&#xff0c;利用射频 (RF) 波连续测量血液中的葡萄糖水平。利用射频波技术连续实时监测血液中的葡萄糖水平&#xff0c;使用的辐射要比手机少得多。 大概原理是血液中的葡萄糖是具有介电特性&#xff0c;一般来说就是介电常数。 电磁波波幅的衰减反映了介质对电…