MyBatis
入门小结
为了使用 MyBatis
,我们需要需要完成以下步骤:
SqlSessionFactory sqlSession sqlSession
MyBatis
中有两个主要的配置文件:
全局配置文件:用来配置全局属性,包含全局设置、数据库、事务、插件、映射文件的位置
JPA
有很大的不同】
<select>
:查询SQL
id resultType databaseId
这里面的SQL,字段与物理表中的字段保持一致,参数名与属性保持一致【JSP取对象】
增删改的返回值会帮你自动封装成Integer,Long或者Boolean,只需要在Mapper接口中声明即可【增删改中也没有resultType】
主键的获取:
<insert useGeneratedKeys="true" keyProperty="id">
useGeneratedKeys keyProperty
使用 <selectKey>
编写查找主键的值
keyProperty
: order
:有 before
与 after
【在该SQL之前插入还是之后】 resultType
:指定ID的类型 参数:
#
是从Map中取值,默认key是param1,param2...【使用注解 @Param
来指定Key】
$
与 #
$ #
放回List类型:直接填写简单类型
<select resultType="EmployeeEntity">
放回Map类型:
<select resultType="map">
【查到了一个数据】 <select resultType="EmployeeEntity">
+ @MapKey("propertyName")
【查到了多个数据】
resultMap
用于自定义结果集映射封装。【建议使用 resultMap
将所有的列都写上!】
1. `<association>`:
<resultMap id="" type="EmployeeEntity">
<id property="id" column="id"/>
<result property="" column=""></result>
<!--
分布查询:延迟加载需要额外配置:
lazyLoadingEnable: 是否激活延迟加载【所有的关联查询都将延迟加载】
aggressiveLazyLoading: 如果开启的话只要有一个懒加载属性被加载,则该对象中的所有懒加载属性都将被加载,否则就按需加载
<association property="dept" select="DeptMapper.getDeptById" column="dept_id" fetchType="lazy"/>
其中`column={keyName=ColumnName}`【keyName】是对方SQL中的#{xxx}
-->
<association property="" javaType="">
<id property="" column=""></id>
<result property="" column=""></result>
</association>
<collection property="" ofType="">
<id></id>
<result property="" column=""></result>
</collection>
</resultMap>
<if test="条件">
:test的判断表达式是使用OGNL表达式【】 <if test="aclId != null and aclId !='' and aclId.trim() != ''">
</if>
<where>
:自动加上 where
+ 只能够去掉 开头
的 AND
<where>
<if test="name != null and name !=''">
AND name like #{name}
</if>
<if test="name != null">
AND age = #{age}
</if>
</where>
<trim>
: 字符串截取
<trim>
<trim prefix="" prefixOverrides="" suffix="" suffixOverrides="">
<if test="name != null and name !=''">
AND name like #{name}
</if>
<if test="name != null">
AND age = #{age}
</if>
</trim>
<choose>
: <choose>
<when test="aclId">
name = #{name}
</when>
<when test="aclId">
last_name = #{lastName}
</when>
<otherwise>
sex = #{sex}
</otherwise>
</choose>
<set>
:自动添加 set
字句 + 去掉最后的逗号 <update id="updateById">
update t_employee
<set>
<if test="name != null">
name = #{name},
</if>
<if test="name != null">
age = #{age},
</if>
</set>
</update>
<foreach>
:
<select>
select * from t_employee e where e.id in
<foreach collection="employeeList" item="one" open="(" separator="," close=")" index="i">
#{id}
</foreach>
</select>
<bind>
与 <sql>
<bind name="_valName" value="Math.PI">
<sql>
+ <include>
:include还可以自定义一些属性,可以在 <sql>
中使用 ${}
进行访问 Mybatis 将缓存分为两级缓存:
* 一级缓存:sqlSession级别缓存
* 查出数据之后,将会自动放入一级缓存中
* 二级缓存:Mapper级别缓存【本质是基于namespace的】,每一个Mapper都维护自己缓存
* 二级缓存默认**不开启**的,需要手动配置,配置之后将会查询结果将会放入二级缓存【**不同的namespace中**】中
一次查询:先查询二级缓存,二级缓存中没有再到一级缓存中,如果一级缓存中没有则到数据库中查找
开启二级缓存需要完成下列的三步:
1. 全局配置文件:配置`cacheEnabled`,该属性设置二级缓存【只能设置二级缓存】
2. `Mapper.xml`:添加标签`<cache>`
* `eviction`:缓存回收策略
* `flushInterval`:多久清空一次
* `readOnly`:是否只读。
* 如果设置只读:则Mybatis会直接将缓存的应用给用户
* 如果设置非只读:则先克隆缓存,然后再交给用户克隆之后的数据
* `size`:缓存中可以放多少个元素
* `type`:指定自定义缓存的全类名【实现Mybatis的`Cache`接口】
3. 缓存的数据实现序列化接口
查询结果先放入一级缓存中, 会话关闭 之后,将会话中的缓存放入二级缓存中
一级缓存:
* 增删改:一级缓存失效
* 手动清除一级缓存:`opession.clearCache()`会清除二级缓存
二级缓存:
* `select`标签的`useCache`:关闭二级缓存
* 增删改:会清空缓存
* 增删改标签的`flushCache`:设置`true`将会清空一级缓存,也会清除二级缓存【`select`标签的默认是false】
* `openSession.clear()`不会清除二级缓存
`localCacheScope`:本地缓存作用域设置STETAMENT会禁用一级缓存。
使用第三方缓存可使用如下的一种:
<cache type="缓存类型全类名"> <cache-ref="命名空间">