ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Mybatis-基础补充

2021-11-26 16:59:36  阅读:171  来源: 互联网

标签:mapper tel 补充 标签 基础 二级缓存 Mybatis where id


配置文件加载

在这里插入图片描述

mapper配置文件详解

对于mapper配置文件中的statement,元素介绍。

DQL

  1. id:一个mapper.xml下的statement唯一标识。不同mapper.xml下id可以重复。
  2. parameterType:入参的类型。
  3. resultType:返回结果集封装的类型。
  4. resultMap:mapper.xml中定义的resultMap引用。
  5. userCache:对于查询结果是否保存二级缓存。
  6. flushCache:执行sql后会清空一级缓存和当前namespace的二级缓存。
  7. databaseId:标注数据库厂商

DML

区别与DQL的如下

  1. flushCache:默认true
  2. useGeneratedKeys:开启使用,则Mybatis会使用jdbc底层的getGeneratedKeys方法,取出自增主键的值,应用与insert和update。
  3. keyProperty:配合useGeneratedKeys使用,用于指定出传入参数对象的属性名,应用与insert和update。
  4. keyClumn:设置useGeneratedKeys生效的值对应到数据库表中的列名,应用与insert和update。

useGeneratedKeys:就是在插入数据时,用数据库的自增id作为主键。如果这个属性为true,则主键可以不用传,mybatis会在底层使用getGeneratedKeys方法帮我们查出id,放入id属性中,回填到实体类。

默认情况下,数据库中建表第一列时主键。但有时候情况例外,那就需要通过keyProperty指定实体类的id,keyClumn指定数据库中表的主键id。

mapper.xml中的缓存

一级缓存

默认情况下Mybatis只会开启基于SqlSesion的一级缓存。二级缓存默认不开启。

二级缓存

基于SqlSessionFactroy级别的缓存。一个namespce对应一块二级缓存。如果需要为namespce开启二级缓存,需要在对应的mapper.xml中声明一个<cache>标签。

开启二级缓存,我们的pojo要实现Serializable接口。为了将缓存数据取出执行反序列化操作。因为二级缓存数据可能存储内存业可能存储在磁盘中。如果我们要取这个缓存的话,就需要反序列化了。所以mybatis中的pojo都去实现Serializable接口。

mapper.xml编写动态sql

if标签

我们有时候会这样些。

where 1=1
<if test="xxx">
and xx=#{xx}
</if>

这样写是为了避免where语句中如果只有一个条件,多一个and的问题。

我们也可以这样写:
定义<where>标签,这个标签会帮我们构造where,而且会帮我们除去第一个不必要的and。不过and标签都要写在<if>标签的前面。

如果习惯都写到后面,那么最后一个条件就会在后面多一个and,如何处理?

<where>
<if test="xxx">
xx=#{xx} and
</if>
</where>
  1. 不这么写。。。。。
  2. 使用<trim>标签
    (1) prefix :在整个标签前面附加指定内容
    (2) prefixOverrides :去掉标签体内第一个指定的关键字
    (3) suffix :在整个标签最后追加指定内容
    (4) suffixOverrides :去掉标签体内最后一个指定的关键字
<trim prefix="where" suffix="" suffixOverrides="and">
<if test="xxx">
xx=#{xx} and
</if>
</where>

choose,when,otherwise标签

如果一条查询中有多个条件,每次只让其中一个条件生效。这种情况就需要使用这三个标签了。

表达的含义是:if() else if() else()

 <choose>
        <when test="id != null and id != ''">
            where id = #{id}
        </when>
        <when test="name != null and name != ''">
            where name like concat('%', #{name}, '%')
        </when>
        <otherwise>
            where tel = #{tel}
        </otherwise>
    </choose>

set标签

在update语句中,使用set标签,可以帮我们去掉最后一个set后面的逗号。

<set>
        <if test="name != null and name != ''">
            name = #{name},
        </if>
        <if test="tel != null and tel != ''">
            tel = #{tel},
        </if>
    </set>
    where id =#{id}

这个里面的最后一个tel=#{tel}后面的逗号,set标签会帮我们去掉。

foreach

主要用于使用in的场景。where xxx in();

where id in
 <foreach collection="ids" item="id" open="(" close=")" separator=",">
        #{id}
    </foreach>

批量更新

 Department department = new Department();
    department.setName("测试部");
    //实体类转Map工具类
    BeanMap beanMap = BeanMap.create(department);

    Map<String, Object> departmentMap = new HashMap<>(2);
    departmentMap.put("id", "123456");
    departmentMap.put("beanMap", beanMap);
    sqlSession.update("dynamic.updateByMap", departmentMap);
<update id="updateByMap" parameterType="map">
    update test
    <foreach collection="beanMap" index="key" item="value" open="set " separator=",">
        <if test="value != null">
            ${key} = #{value}
        </if>
    </foreach>
    where id = #{id}
</update>

foreach 在循环 Map 时,键值对的 key 是 index ,value 是 item 。

sql标签

有一些通用的sql语句,我们可以通过<sql>标签来进行抽取。

<sql id="columns">
    id, name, sex
</sql>

后面的sql可以通过引用sql,获取对应的列。

<select id="testSql" resultType="map">
    select <include refid="columns"/> from test2
</select>

SQL 语句片段也是可以接收 <include> 标签传递的参数值的!<include> 标签并不是完全自闭合的,它里面有 <property> 标签可以写。

<sql id="columns">
    name, tel
    <if test="${testValue} != null and ${testValue} == true">
        , id
    </if>
</sql>
<select id="testSql" resultType="map">
    select 
    <include refid="columns">
      <property name="testValue" value="true"/>
    </include>
     from test2
</select>

标签:mapper,tel,补充,标签,基础,二级缓存,Mybatis,where,id
来源: https://blog.csdn.net/xiaopang2020/article/details/121561227

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有