ICode9

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

面试之mybatis

2021-05-25 17:04:36  阅读:122  来源: 互联网

标签:缓存 查询 面试 二级缓存 tid mybatis id select


作为一个程序员的老鸟,在没有mybatis实战经验,被问到mybatis的标签时,竟然有些答不上来,下面是稍微问的多的标签

复杂查询(一对多)

  • 按照结果嵌套
<?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">
<mapper namespace="com.luke.dao.TeacherMapper">

    <resultMap id="teacherStudent" type="teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"/>
        </collection>
        
    </resultMap>
    
    <select id="getTeacherById" resultMap="teacherStudent">
        select s.id sid, s.name sname, t.name tname, t.id tid from student s,teacher t where s.tid = t.id and tid = #{id};
    </select>

</mapper>

  • 按照查询嵌套
<resultMap id="teacherStudent2" type="teacher">
	<result property="id" column="id"/>
	<collection property="students" javaType="ArrayList" ofType="student" column="id" select="getStudengByTeacherId"/>
</resultMap>

<select id="getTeacherById2" resultMap="teacherStudent2">
	select * from teacher where id = #{tid}
</select>

<select id="getStudengByTeacherId" resultType="student">
	select * from student where tid = #{tid}
</select>

复杂查询(多对一)

  • 按照查询嵌套处理
<?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">
<mapper namespace="com.luke.dao.StudentMapper">

    <resultMap id="studentTeacher" type="student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid" javaType="teacher" select="getTeacher"/>
    </resultMap>
    
    <select id="getAllStudent" resultMap="studentTeacher">
        select * from student;
    </select>
    
    <select id="getTeacher" resultType="teacher">
        select * from teacher where id = #{tid};
    </select>
</mapper>

  • 按照结果嵌套处理
<resultMap id="studentTeacher2" type="student">
	<result property="id" column="sid"/>
	<result property="name" column="sname"/>
	<association property="teacher" javaType="teacher">
		<result property="name" column="tname"/>
	</association>

</resultMap>

<select id="getAllStudent2" resultMap="studentTeacher2">
	select s.id sid, s.name sname, t.name tname from student s,teacher t where s.tid = t.id;
</select>

Foreach
多行插入

<select id="queryBlogForeach" parameterType="map" resultType="blog">
  select * from blog
   <where>
       <!--
       collection:指定输入对象中的集合属性
       item:每次遍历生成的对象
       open:开始遍历时的拼接字符串
       close:结束时拼接的字符串
       separator:遍历对象之间需要拼接的字符串
       select * from blog where 1=1 and (id=1 or id=2 or id=3)
     -->
       <foreach collection="ids"  item="id" open="and (" close=")" separator="or">
          id=#{id}
       </foreach>
   </where>
</select>

一级缓存

一级缓存也叫本地缓存:

与数据库同一次会话期间查询到的数据会放在本地缓存中。

以后如果需要获取相同的数据,直接从缓存中拿,没必须再去查询数据库;

也就是同一个session中查询同样的查询语句,第二次及之后的将读取缓存中的数据

SqlSession session = MybatisUtils.getSession();

缓存失效的四种情况:

  • sqlSession不同
  • sqlSession相同,查询条件不同
  • sqlSession相同,两次查询之间执行了增删改操作!
  • sqlSession相同,手动清除一级缓存

二级缓存

二级缓存也叫全局缓存,一级缓存作用域太低了,所以诞生了二级缓存

基于namespace级别的缓存,一个名称空间,对应一个二级缓存;

工作机制

一个会话查询一条数据,这个数据就会被放在当前会话的一级缓存中;

如果当前会话关闭了,这个会话对应的一级缓存就没了;但是我们想要的是,会话关闭了,一级缓存中的数据被保存到二级缓存中;

新的会话查询信息,就可以从二级缓存中获取内容;

不同的mapper查出的数据会放在自己对应的缓存(map)中;
参考文章
https://juejin.cn/post/6910903604698152973

标签:缓存,查询,面试,二级缓存,tid,mybatis,id,select
来源: https://blog.csdn.net/u013401975/article/details/117250404

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

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

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

ICode9版权所有