- 概述
细节描述不详细,这里只作为自己学习的总结。MyBatis 是当前主流的 ORM(Object Relational Mapping) 框架,当然同类型的还有Hibernate框架。
Hibernate 是一个“全自动化”的 ORM 框架,而 MyBatis 则是一个“半自动化”的 ORM 框架。“全自动化”:意为开发者只需要调用相关接口就可以完成操作,整个流程框架都已经封装好了,开发者无需关注。具体来讲 Hibernate 实现了 POJO 和数据库表之间的映射,同时可以自动生成 SQL 语句并完成执行。
“半自动化”指的是框架只提供了一部分功能,剩下的工作仍需要开发者手动完成,MyBatis 框架没有实现 POJO 与数据库表的映射,它只实现了 POJO 与 SQL 之间的映射关系,同时需要开发者手动定义 SQL 语句,以及数据与 POJO 的装配关系。
虽然功能上没有 Hibernate 更加方便,但是这种“半自动化”的方式提高了框架的灵活性,MyBatis 对所有的 JDBC 代码实现了封装,包括参数设置、SQL 执行、结果集解析等,通过 XML 配置的方式完成 POJO 与数据的映射。
- 涉及的文件
- 实体类:POJO
- mapper接口:接口类,对应pojo查询的接口。
- mapper.xml 接口配置文件,用于写具体的sql语句,和mapper接口一一对应。
- 全局配置文件:config.xml,用于配置数据连接参数,缓存,打印sql,懒加载等,其次mapper.xml要添加到全局配置文件中。
- 延迟加载
当实体类中存在级联操作时,有时只需要加载主体,不需要加载实体类中的对象时,可以使用延迟加载,当使用到时再另加载。
在config.xml中开启延迟加载
<configuration>
<!--设置 settings -->
<settings>
<!--打印 SQL -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--打开延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="true" />
</settings>
</configuration>
mapper.xml中的配置
<configuration>
<!--设置 settings -->
<settings>
<!--打印 SQL -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--打开延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="true" />
</settings>
</configuration>
-
缓存
缓存分为一级缓存和二级缓存,一级缓存为SqlSession缓存,同一个SqlSession,当第一次去数据库查询后,会保留查询数据,第二次再查询时,直接从缓存中读取。但是不同的sqlSession不会存在,其次当做update insert delete 操作后,为保持数据的一致性,缓存也会被清楚。
二级缓存为mapper级。不同的sqlsession操作都会保存缓存。这个可以使用mybatis自带的二级缓存实现,也可以用ehcache 实现。第一使用自带的二级缓存,实体类实现Serializable 接口:
config.xml 中配置开启二级缓存
<configuration>
<!-- 设置 settings -->
<settings>
<!-- 打印 SQL -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
mapper.xml 中配置
<mapper namespace="com.southwind.repository.ClassesRepository">
<cache></cache>//添加缓存
<select id="findById" parameterType="java.lang.Long" resultType="com.southwind.entity.Classes">
select * from classes where id = #{id}
</select>
</mapper>
第二使用 ehcache实现,实体类不用实现Serializable 接口:
maven添加依赖:
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.4.3</version>
</dependency>
</dependencies>
在resource中创建ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore/>
<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
config.xml中开启缓存
<configuration>
<!-- 设置settings -->
<settings>
<!-- 打印SQL -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
mapper.xml中配置缓存
<mapper namespace="com.southwind.repository.ClassesReposirory">
<!-- 开启二级缓存 -->
<cache type="org.mybatis.caches.ehcache.EhcacheCache" >
<!-- 缓存创建以后,最后一次访问缓存的时间至失效的时间间隔 -->
<property name="timeToIdleSeconds" value="3600"/>
<!-- 缓存自创建时间起至失效的时间间隔-->
<property name="timeToLiveSeconds" value="3600"/>
<!-- 缓存回收策略,LRU 移除近期最少使用的对象 -->
<property name="memoryStoreEvictionPolicy" value="LRU"/>
</cache>
<select id="findById" parameterType="java.lang.Long" resultType="com.southwind.entity.Classes">
select * from classes where id = #{id}
</select>
</mapper>
- 动态SQL
使用动态sql可以根据条件,动态生成sql语句。加强了mybatis开发的灵活性。
- if标签
<select id="findByUser" parameterType="com.southwind.entity.User" resultType="com.southwind.entity.User">
select * from t_user where
<if test="id!=null">
id = #{id}
</if>
<if test="username!=null">
and username = #{username}
</if>
<if test="password!=null">
and password = #{password}
</if>
<if test="age!=null">
and age = #{age}
</if>
</select>
- where标签:可以解决动态sql中 and 的问题
<select id="findByUser" parameterType="com.southwind.entity.User" resultType="com.southwind.entity.User">
select * from t_user
<where>
<if test="id!=null">
id = #{id}
</if>
<if test="username!=null">
and username = #{username}
</if>
<if test="password!=null">
and password = #{password}
</if>
<if test="age!=null">
and age = #{age}
</if>
</where>
</select>
- trim标签:解决and ,","号问题。trim 标签中的 prefix 和 suffix 属性会被用于生成实际的 SQL 语句,会和标签内部的语句拼接。如果语句的前面或后面遇到 prefixOverrides 或 suffixOverrides 属性中指定的值,MyBatis 会自动将它们删除。在指定多个值的时候,别忘了每个值后面都要有一个空格,保证不会和后面的 SQL 连接在一起。
prefix:表示在后面sql的前面,有predixOverride指定的值时,自动去掉predixOverride的值。
<select id="findByUser" parameterType="com.southwind.entity.User" resultType="com.southwind.entity.User">
select * from t_user
<trim prefix="where" prefixOverrides="and">
<if test="id!=null">
id = #{id}
</if>
<if test="username!=null">
and username = #{username}
</if>
<if test="password!=null">
and password = #{password}
</if>
<if test="age!=null">
and age = #{age}
</if>
</trim>
</select>
- choose when 标签
<select id="findByUser" parameterType="com.southwind.entity.User" resultType="com.southwind.entity.User">
select * from t_user
<where>
<choose>
<when test="id!=null">
id = #{id}
</when>
<when test="username!=null">
and username = #{username}
</when>
<when test="password!=null">
and password = #{password}
</when>
<when test="age!=null">
and age = #{age}
</when>
</choose>
</where>
</select>
- set 标签:用于update语句
<update id="update" parameterType="com.southwind.entity.User">
update t_user
<set>
<if test="username!=null">
username = #{username},
</if>
<if test="password!=null">
password = #{password},
</if>
<if test="age!=null">
age = #{age}
</if>
</set>
where id = #{id}
</update>
- foreach标签:
public class User{
private Integer id;
private String username;
private String password;
private Integer age;
private List<Integer> ids;
}
<select id="findByUser" parameterType="com.southwind.entity.User" resultType="com.southwind.entity.User">
select * from t_user
<where>
<foreach collection="ids" open="id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>