学习目标:
总结第三阶段笔记
学习内容:
1.动态sql标签
1).<where>标签:去除where后多余的and/or
<where> <if test="id !=null">id =#{id} </if> <if test="name !=null">and name =#{name} </if> </where>
2).<set>标签:去除set后多余的逗号
<set> <if test="name !=null">name =#{name}, </if> <if test="age !=null">age =#{id} ,</if> </set>
3).<choose>:根据属性不为null的查询
<choose> <when test="name !=null">name= #{name} </when> <when test="age !=null">age= #{age} </when> <otherwise>sex=#{sex}</otherwise> </choose>
2.多表映射
单表映射用 resultType="User"
多表映射用 resultMap="empRM"
3.一对一封装与一对多
一对多用集合封装 List<E>,一对一直接对象封装
1).association 表示一对一封装
2).property 当前对象的属性名称
3).javaType 指定属性类型
<resultMap id="empRM" type="Emp" autoMapping="true">
<id column="emp_id" property="empId"/>
<association property="dept" javaType="Dept" autoMapping="true">
<id column="dept_id" property="deptId"/>
</association>
</resultMap>
1.collection: 封装的集合类型 2.property 当前对象的属性名称 3.ofType 指定集合内部(泛型)的对象类型 <resultMap id="deptRM" type="Dept" autoMapping="true"> <id column="dept_id" property="deptId"/> <collection property="emps" ofType="Emp" autoMapping="true"> <id column="emp_id" property="empId"></id> </collection> </resultMap>
4.Mybatis缓存
说明:由同一个SQLSessionFactory(类比连接池)生产的sqlSession内,实现数据的共享(数据库连接)
一级缓存:在同一个Session内,实现数据库的共享(单线程)
SpringBoot测试说明: * SpringBoot中用户使用userMapper接口时,用户每调用一次, * SpringBoot就会创建一个SqlSession. * * 如何解决多个SqlSession的问题? * 解决方案: * 利用@Transactional的事务注解,将多个SqlSession控制为一个
二级缓存:由同一个SqlSessionFactory生产多个SqlSession(多线程).可以在多线程的条件下,可以实现数据共享.<cache/>标识使用二级缓存
默认条件下一二级缓存是开启的. 但是二级缓存需要标记 cache