MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
xml映射器
顶级元素.3..
常用顶级类
<select> 映射查询语句 </select><selectid="selectPerson" parameterType="int"parameterMap="deprecated" resultType="hashmap"resultMap="personResultMap" flushCache="false"useCache="true" timeout="10" fetchSize="256"statementType="PREPARED" resultSetType="FORWARD_ONLY"><insert> 映射插入语句 </insert><insertid="insertAuthor" parameterType="domain.blog.Author"flushCache="true" statementType="PREPARED"keyProperty="" keyColumn="" useGeneratedKeys=""timeout="20"><update> 映射更新语句 </update><updateid="updateAuthor" parameterType="domain.blog.Author"flushCache="true" tatementType="PREPARED" timeout="20"><delete> 映射删除语句 </delete><deleteid="deleteAuthor" parameterType="domain.blog.Author"flushCache="true" statementType="PREPARED" timeout="20">id; // 命名空间唯一标识符parameterType; // 传入的参数全限定名,可选mybatis会通过TypeHandLen判断类型resultType; // 期望从语句中返回类全限定名或别名resultMap; // 对外部resultmap的别名引用,解决复杂的映射问题flushCache; // 清空本地缓存或二级缓存,默认falseuseCache; // 本条语句会被二级缓存,默认truetimeout; // 抛异常前等待数据库秒数fetchSize; // 增大驱动读取数据库的数量,默认每次读取10个statementType; // 标记操作SQL的对象statement; // 直接操作不进行预编译: $-Statementprepared; // 预处理,进行预编译: #-preparedStatement 默认callable; // 执行存储过程: callableStatementresultSetType;databaseId;resultOrdered;resultSets;
结果映射类
// 单类型<resultMap><id property="id" column="user_id"><result property="userName" column="user_name"></resultMap>// 一对一<resultMap><id properety="id" column="user_id"><result property="userName" column="user_name"><assoction property="order" javaType="Order"> // property是名字,Javatype是类型<id property="orderId" column="id"><result property="orderName" column="name"></assoction></resultMap>// 一对多<resultMap><id properety="id" column="user_id"><result property="userName" column="user_name"><collection property="order" javaType="java.util.ArrayList" ofType="Order"><id property="orderId" column="id"><result property="orderName" column="name"></collection></resultMap>
结果映射懒加载
懒加载将大量同时运行的语句分散开来,在需要时再进行查询 能提高数据库的性能 assoction和collection具有延迟加载(懒加载)功能 在调用主表时,连接表或子表不会进行查询, 在需要用到某个主表数据时,在会 进行链接表或主表查询 减少了连接表或字表的查询次数,提升数据库的性能,减少了查询所需的时间
// 一对一// 一对一主表<resultMap><id properety="id" column="user_id"><result property="userName" column="user_name"><assoction property="order" javaType="Order"column=""></resultMap>// 一对一连接表<resulttMap id="authorResult" type="Author"><id property="orderId" column="id"><result property="orderName" column="name"></resultMap>
动态SQL
if 判断元素
select * from users<if test="id!=null">where id=#{id}</if>
choose、when、otherwise 筛选元素
select * from userswhere gender =<choose><when test="gender == '男'"> 1 </when><when test="gender == '女'"> 2 </when><otherwhen> 0 </otherwhen></choose>
where、set、trim条件查询元素
// 如果where元素有内容情况下才会插入where// 如果子句中开头有and | or where元素也会自动剔除select * from users<where><if test="id!=null"> id=#{id} </if><if test="name!=null">and name=#{name}</if></where>// set与where类似// 如果字句尾部有逗号会自动剔除update users<set><if test="name!=null"> name=#{name}, </if><if test="gender!=null"> gender=#{gender} </if></set>// trim可以自定义元素前缀,并且设置自定义剔除字句的前后语句<trim prefix="WHERE" prefixOverrides="AND|OR" suffixOverrides="AND|OR"></trim>
foreach批量插入元素
// 批量查询// list = {1,2,3,4,5}select * from users where id in<foreach item="item" index="index" collection="list"open="where id in (" separator="," close=")">#{item}</foreach>// 批量插入// users = {user, user, user}insert into users (name, genger) values<foreach item="user" index="index" collection="users" separator=",">(user.name, user.genger)</foreach>
sql片段
// 声明片段// 引用片段
缓存机制
- 映射语句中的select语句结果将会被缓存
- 使用增删改(
insert、delete、update)后,清除缓存 - 缓存会使用最近最少使用算法(LRU, Least Recently Used)算法来清除不需要的缓存。
- 缓存不会定时进行刷新(也就是说,没有刷新间隔)。
- 缓存会保存列表或对象(无论查询方法返回哪种)的 1024 个引用。
缓存会被视为读/写缓存,这意味着获取到的对象并不是共享的,可以安全地被调用者修改,而不干扰其他调用者或线程所做的潜在修改。
二级缓存配置
eviction属性:缓存回收策略
- LRU(Least Recently Used)最近使用算法,移除最长时间没有使用的数据
- FIFO(First in Out)先进先出算法,优先回收最长使用的数据
- SOFT软引用,移除基于垃圾回收器状态和软引用规则的对象
- WEAK弱引用,更积极地移除基于垃圾回收器状态和弱引用规则的对象
- flushInterval:刷新间隔,单位毫秒
- 默认不设置,没有刷新时间
- size:引用数目,正整数
- 表示缓存最大可存储的对象,太大容易导致内存溢出
- readOnly:只读缓存true/false
- true:只读缓存;会给所有调用者返回相同实例
- false:读写缓存;会返回缓存对象的拷贝(通过序列化),速度慢但安全
缓存查询机制
第一次查询时的顺序二级缓存查询数据一级缓存查询数据数据库中查询数据数据写入一级缓存关闭前数据写入二级缓存
