MyBatis知识点总结

1.别名 //主配置文件配置
给类起别名
<typeAliases> <typeAlias alias = "User" type = "org.lanqiao.bean.User" /> <typeAlias alias = "Teacher" type = "org.lanqiao.bean.Teacher" /> </typeAliases>
给包起别名
<typeAliases> <package name = "org.lanqiao.bean" /> </typeAliases>

2.mybatis全局设置 //主配置文件配置
<settings>
开启驼峰命名转换
<setting name = "mapUnderscoreToCamelCase" value = "false" />
开启缓存 <setting name = "cacheEnabled" value = "true" />
开启懒加载 <setting name = "lazyLoadingEnabled" value = "true" /> </settings>

3.返回操作的ID //映射文件配置
<insert id = "addUser" useGeneratedKeys = "true" keyProperty = "id" > insert into user(name,sex,email,phone) values (#{name},#{sex},#{email},#{phone}) </insert>

4.mybatis映射(一对一)
<!-- 根据班级 id 查询班级信息(带老师信息),一对一 -->
< select id ="getClass" parameterType ="int" resultMap ="ClassResultMap" >
select * from classes c,teacher t where c.teacher_id=t.id and c.id=#{id}
</ select >

< resultMap id ="ClassResultMap" type ="org.lanqiao.bean.Classes" >
< id property ="id" column ="id" />
< result property ="name" column ="name" />
< association property ="teacher" javaType ="org.lanqiao.bean.Teacher" >
< id property ="id" column ="id" />
< result property ="name" column ="name" />
</ association >
</ resultMap >

多对一(在多对一关系中,关系由多的一方维护)
<!-- 根据班级 id 查询班级信息(带老师信息和学生信息)多对一 -->
< select id ="getClass3" parameterType ="int" resultMap ="ClassResultMap3" >
select * from classes c,teacher t,student s where c.teacher_id=t.t_id
and c.id=s.class_id and c.id=#{id}
</ select >

< resultMap id ="ClassResultMap3" type ="org.lanqiao.bean.Classes" >
< id property ="id" column ="id" />
< result property ="name" column ="name" />
< association property ="teacher" javaType ="org.lanqiao.bean.Teacher" >
< id property ="id" column ="t_id" />
< result property ="name" column ="t_name" />
</ association >

< collection property ="students" ofType ="org.lanqiao.bean.Student" >
< id property ="id" column ="s_id" />
< result property ="name" column ="s_name" />
</ collection >
</ resultMap >

5.mybatis动态sql (使用OGNL表达式)
if
<select id = "findUserByConditions" resultType = "User" > select * from user where 1=1 <if test = "name != null" > AND namelike #{name} </if> </select>
choose, when, otherwise
<select id = "findUserByConditions" resultType = "User" > select * from User where 1=1 <choose> <when test = "name!= null" > AND name like #{ name } </when> <when test = "email!= null" > AND email like #{ email } </when> <otherwise> </otherwise> </choose> </select>

where, set
< select id ="findUserByConditions2"
parameterType ="User" resultType ="User" >
select * from user
< where >
< if test ="name != null" >
AND name like #{name}
</ if >
< if test ="email != null" >
AND email like #{email}
</ if >
</ where >
</ select >

<update id = "updateUserByConditions" > update user <set> <if test = "name!= null" > name =#{ name }, </if> <if test = "sex!= null" > sex =#{ sex }, </if> <if test = "email != null" > email =#{ email }, </if> <if test = "phone!= null" > phone =#{ phone } </if> </set> where id=#{id} </update>
foreach
属性解释:
 item:表示集合中每一个元素进行迭代时的别名。
 index:指 定一个名字,用于表示在迭代过程中,每次迭代到的位置。
 open:表示该语句以什么开始。
 separator:表示在每次进行迭代之间以什么符号作为分隔 符。
 close:表示以什么结束。
collection:表示要迭代的数据,三种取值:
  1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
    2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
    3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可
< select id ="getUserIn" resultType ="User" >
select * from user where id in
< foreach item ="item" index ="index" collection ="list"
open ="(" separator ="," close =")" >
#{item}
</ foreach >
</ select >
mybatis转义字符
  &lt;       < 
  &gt;       >
  &lt;&gt;    <>
  &amp;      & 
  &apos;      '
  &quot;      "
mybatis缓存
 为了减轻数据压力,提高 数据库 的性能,我们往往会需要使用缓存。MyBatis为我们提供了一级缓存和二级缓存。
    (1)一级缓存是SqlSession级别的缓存,在操作数据库的时候需要创建一个SqlSession,其中有一个HashMap,用于存储缓存数据。不同的SqlSession之间,其缓存数据的HashMap是不同的;
    (2)二级缓存是Mapper级别的缓存,多个SqlSession去操作同一个Mapper中的SQL语句,则这些SqlSession可以共享二级缓存,即二级缓存是跨SqlSession的。


 MyBatis中的二级缓存默认是关闭的,如果我们想要使用二级缓存,则需要对其进行配置:
    (1)在SqlMapConfig.xml文件中配置二级缓存的总开关,代码如下:
[html]   view plain   copy
  1. <settings>  
  2.     <!-- 开启二级缓存(默认是开的,这里写出来是为了方便代码维护) -->  
  3.     <setting name="cacheEnabled" value="true" />  
  4. </settings>  
    (2)在XXXMapper.xml文件中开启二级缓存,代码如下:
[html]   view plain   copy
  1. <!-- 开启本mapper所在namespace的二级缓存 -->  
  2. <cache />  
    (3)如果想要对某个POJO中的数据进行二级缓存,则需要将其序列化:让这个POJO类实 现Serializable接口(这样做的原因是因为二级缓存的数据存储介质是多种多样的,不一定只在内存中,也可能在硬盘中,甚至是远程传输,因此我们将其序列化,以便将来对其进行反序列化)。

Cache Hit Ratio:缓存的命中率

<!-- 开启二级缓存 --> < cache />

cache标签里的其他属性:
< cache eviction ="FIFO" <!--回收策略为先进先出-- > flushInterval="60000" <!--自动刷新时间60s--> size="512" <!--最多缓存512个引用对象--> readOnly="true"/> <!--只读-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值