面试之mybatis

作为一个程序员的老鸟,在没有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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值