MyBatis 实践 -配置
MyBatis 实践
标签: Java与存储
Configuration
mybatis-configuration.xml是MyBatis的全局配置文件(文件名任意),其配置内容和顺序如下:
- properties : 属性(文件)加载/配置
- settings : 全局配置参数
- typeAliases : 定义类型别名
- typeHandlers : 类型处理器
- objectFactory : 对象工厂
- plugins : 插件
- environments : 环境集合属性对象
- environment
- transactionManager : 事务管理
- dataSource : 数据源
- environment
- databaseIdProvider:P数据库厂商标识
- mappers : 映射器
properties
方便对配置参数统一管理,供其他XML引用,我们可以将数据库的连接参数抽取出来:
- db.properties
## Data Source
mysql.driver.class=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://host:port/db?characterEncoding=utf-8
mysql.user=user
mysql.password=password
- mybatis-configuration.xml
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<!-- 配置JDBC事务管理-->
<transactionManager type="JDBC"/>
<!-- 配置数据源-->
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver.class}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.user}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
注: MyBatis按照如下顺序加载properties:
1) 在<properties>标签内定义的属性;
2) .properties文件中定义的属性;
3) 最后读取作为方法参数传递的属性.
settings
MyBatis全局配置参数,会影响MyBatis运行时行为(如:开启二级缓存/延迟加载).见MyBatis文档.
typeAliases
MyBatis默认支持的类型别名可参考MyBatis文档,我们也可以自定义别名,但并不推荐,使用PO对象的全限定名可以提高Statement的可读性.
typeHandlers
typeHandlers用于Java类型和JDBC类型转换,MyBatis提供了很多默认的类型处理器(详见MyBatis文档),而且也基本满足日常开发需求,因此一般就不再需要单独定义.
mappers
前面已经将SQL语句定义到了mapper文件中,那么<mappers/>属性就是告诉MyBatis到哪里去寻找mapper文件,MyBatis提供了如下几种配置方法:
| 配置 | 描述 |
|---|---|
<mapper resource=""/> |
使用类路径的资源(Resources/java目录下) |
<mapper url=""/> |
使用完全限定路径 |
<mapper class=""/> |
使用mapper接口类路径 |
<package name=""/> |
注册指定包下的所有mapper接口 |
注意:后两种方式要求mapper接口名和mapper映射文件名称相同,且放在同一个目录中(不推荐).
其他关于MyBatis的配置信息可参考MyBatis文档.
整合Spring
实现MyBatis与Spring整合之后,可以使用Spring来管理SqlSessionFactory和mapper接口,Spring自动使用SqlSessionFactory创建SqlSession,并将实现好DAO接口注册到Spring容器中, 供@Autowired使用.
1. 添加依赖
- 添加Spring支持
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
- 添加MyBatis-Spring包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
- 添加Hikaricp数据库连接池
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${hikaricp.version}</version>
</dependency>
- 不要忘了MySQL数据库驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
2. 配置文件
- 精简mybatis-configuration.xml
可以将数据源的配置移到下面的applicationContext-datasource.xml中.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="mybatis/mapper/UserDAO.xml"/>
</mappers>
</configuration>
- 定义applicationContext-datasource.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="${mysql.driver.class}"/>
<property name="jdbcUrl" value="${mysql.url}"/>
<property name="username" value="${mysql.user}"/>
<property name="password" value="${mysql.password}"/>
<property name="maximumPoolSize" value="5"/>
<property name="maxLifetime" value="700000"/>
<property name="idleTimeout" value="600000"/>
<property name="connectionTimeout" value="10000"/>
<property name="dataSourceProperties">
<props>
<prop key="dataSourceClassName">com.mysql.jdbc.jdbc2.optional.MysqlDataSource</prop>
<prop key="cachePrepStmts">true</prop>
<prop key="prepStmtCacheSize">250</prop>
<prop key="prepStmtCacheSqlLimit">2048</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig"/>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/mybatis-configuration.xml"/>
</bean>
<!-- 根据mapper接口生成代理对象 -->
<bean id="dao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.fq.mybatis.UserDAO"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
上面的配置存在一个问题:需要针对每个mapper配置一个MapperFactoryBean(繁琐),因此这段根据mapper接口生成代理对象的配置可更改如下:
<!-- 基于包扫描的mapper配置 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fq.mybatis"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
附: applicationContext-database.xml完整配置可参考: Git地址
- 定义Spring主配置文件applicationContext.xml
定义注解驱动及加载静态配置文件datasource:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解驱动 -->
<context:annotation-config/>
<!-- 加载静态配置文件 -->
<import resource="applicationContext-datasource.xml"/>
</beans>
- Client
/**
* @author jifang
* @since 16/2/22 上午10:20.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
public class UserDAOClient {
@Autowired
private UserDAO dao;
@Test
public void client() throws Exception {
User user = dao.selectUserById(1);
System.out.println(user);
}
}
缓存
与大多数持久层框架一样,MyBatis也支持一级缓存和二级缓存.

缓存作用是提升系统整体性能(不是提升数据库性能:因为缓存将数据库中的数据存放到内存,下次查询同样内容时直接从内存读取,减轻数据库压力,而且直接从内存中读取数据要比从数据库检索快很多,因此可以提升系统整体性能).
缓存数据更新:当一个作用域(一级缓存为
SqlSession/二级缓存为namespace)进行了C/U/D操作后,默认该作用域下所有缓存都被清空.
一级缓存
MyBatis默认开启了一级缓存.一级缓存是基于org.apache.ibatis.cache.impl.PerpetualCache的HashMap本地缓存,其存储作用域为SqlSession,同一个SqlSession几次执行相同SQL,后面的查询会直接从缓存中加载,从而提高查询效率/减轻数据库压力.当SqlSession经flush/close后,该SqlSession中的所有Cache数据被清空.
二级缓存
与一级缓存机制类似,MyBatis二级缓存默认也是采用PerpetualCache的HashMap存储,不同在于二级缓存存储作用域为namespace/mapper,并且可以自定义缓存实现,如Ehcache.
MyBatis默认没有开启二级缓存,需要经过以下步骤才能使用:
- 启用二级缓存(可选)
其需要在mybatis-configuration.xml的settings全局参数中开启:
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
cacheEnabled对此配置文件下的所有cache进行全局性开/关设置(默认为true).
- 配置缓存策略
在mapper映射文件中添加<cache/>标签,以指定该namespace开启二级缓存, 并指定缓存策略:
<cache eviction="LRU" flushInterval="60000" size="512" readOnly="true"/>
1) eviction:缓存淘汰算法:
| 算法 | 描述 | 释义 |
|---|---|---|
| LRU | 最近最少使用 | 移除最长时间不被使用的对象(默认). |
| FIFO | 先进先出 | 按对象进入缓存的顺序移除. |
| SOFT | 软引用 | 移除基于垃圾回收器状态和软引用规则的对象. |
| WEAK | 弱引用 | 更积极地移除基于垃圾收集器状态和弱引用规则的对象. |
2) flushInterval:刷新间隔(缓存过期时间),单位为毫秒,MyBatis会每隔一段时间自动清空缓存(默认刷新间隔为空, 即永不过期,仅调用语句时刷新).
3) size:引用数目,要记住你缓存的对象的数目和运行环境可用内存资源数目(默认1024).
4) readOnly: 只读.如果为true,则所有相同SQL返回同一对象(因此这些对象不能修改,有助于提高性能,但并发操作同一条数据时,可能不安全);如果为false,则相同SQL后面返回的是cache的clone副本(通过序列化,慢一些但更是安全,因此默认是false).
- 序列化
PO对象要实现Serializable序列化,因为二级缓存的存储介质不一定只是内存:
public class User implements Serializable {
//...
}
- Client
@Test
public void cacheClient() throws Exception {
testCache(factory.openSession());
testCache(factory.openSession());
testCache(factory.openSession());
}
private void testCache(SqlSession session) throws Exception {
UserDAO dao = session.getMapper(UserDAO.class);
dao.selectUserById(1);
// 需要将SqlSession关闭才能将数据写入缓存.
session.close();
}
运行代码, 并观察log输出的命中率(Cache Hit Ratio).
- Statement配置
1) 禁用缓存: 在Statement中设置useCache="false"可以禁用当前select语句的二级缓存(默认为true:该SQL启用二级缓存).
<select id="selectUserById" parameterType="java.lang.Integer" resultType="com.fq.domain.User" useCache="true">
SELECT *
FROM user
WHERE id = #{id};
</select>
2)刷新缓存: 同一个namespace中,如果还有其它insert/update/delete操作,需要刷新缓存,使用flushCache="true"属性设置(默认为true刷新缓存).
<insert id="insertUserList" parameterType="java.util.List" flushCache="true">
INSERT INTO user(name, password) VALUES
<if test="list != null and list.size != 0">
<foreach collection="list" item="user" separator=",">
(#{user.name}, #{user.password})
</foreach>
</if>
</insert>
整合Ehcache
MyBatis暴露一个org.apache.ibatis.cache.Cache接口出来,通过实现该接口,可以实现各类缓存产品(如Ehcache/Redis/Memcached)与MyBatis的整合(MyBatis的特长操作数据库,缓存管理并不是其擅长,因此整合其他缓存产品可以提高系统整体性能).
Ehcache是一个纯Java开发的进程内缓存框架,具有开源/快速/灵活等特点,是Hibernate默认的CacheProvider.使用Ehcache需要在pom.xml中添加如下依赖:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.3</version>
</dependency>
- 配置Ehcache
在Resources目录下添加ehcache.xml配置文件
<ehcache>
<diskStore path="/data/cache"/>
<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
| 属性 | 描述 |
|---|---|
diskStore |
指定缓存数据在磁盘的存储位置 |
maxElementsInMemory |
在内存中缓存element的最大数目 |
maxElementsOnDisk |
在磁盘上缓存element的最大数目,0表示无穷大 |
eternal |
设定缓存的elements是否永远不过期.true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断 |
overflowToDisk |
设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上 |
timeToIdleSeconds |
刷新间隔:缓存数据前后两次访问时间超过timeToIdleSeconds时,这些数据便会删除(默认为0,时间间隔无穷大) |
timeToLiveSeconds |
缓存element的有效生命期(默认为0,时间无限) |
diskSpoolBufferSizeMB |
设置DiskStore(磁盘缓存)缓存区大小.默认是30MB. |
diskPersistent |
在JVM重启时是否使用磁盘保存Ehcache数据,默认是false. |
diskExpiryThreadIntervalSeconds |
磁盘缓存的清理线程运行间隔,默认是120秒. |
memoryStoreEvictionPolicy |
当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略.默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出) |
- mapper配置ehcache
<cache type="org.mybatis.caches.ehcache.EhcacheCache" eviction="LRU" flushInterval="60000" size="1024"
readOnly="true"/>
还可以根据需求调整当前namespace的缓存参数:
<cache type="org.mybatis.caches.ehcache.EhcacheCache">
<property name="timeToIdleSeconds" value="3600"/>
<property name="timeToLiveSeconds" value="3600"/>
<!-- 同ehcache参数maxElementsInMemory -->
<property name="maxEntriesLocalHeap" value="1000"/>
<!-- 同ehcache参数maxElementsOnDisk -->
<property name="maxEntriesLocalDisk" value="10000000"/>
<property name="memoryStoreEvictionPolicy" value="LRU"/>
</cache>
二级缓存小结
- 适用场景
对于查询请求多且对查询结果实时性要求不高的场景,可采用二级缓存降低数据库负担,提高访问速度(业务场景如:微博/动态/订单信息等). - 局限
二级缓存对细粒度级别的缓存实现不好,如”缓存所有的商品信息时,二级缓存就无法实现当一个商品信息变化时只刷新该商品缓存而不刷新全部商品缓存“,因为二级缓存区域以namespace为单位划分,当一个商品发生变化会将所有商品缓存清空,因此解决此类问题需要在上层对数据进行业务划分.
MyBatis 实践 -配置的更多相关文章
- SSM ( Spring 、 SpringMVC 和 Mybatis )配置详解
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- MyBatis 实践 -动态SQL/关联查询
MyBatis 实践 标签: Java与存储 动态SQL 动态SQL提供了对SQL语句的灵活操作,通过表达式进行判断,对SQL进行拼接/组装. if 对查询条件进行判断,如果输入参数不为空才进行查询条 ...
- MyBatis 实践 -Mapper与DAO
MyBatis 实践 标签: Java与存储 MyBatis简介 MyBatis前身是iBatis,是一个基于Java的数据持久层/对象关系映射(ORM)框架. MyBatis是对JDBC的封装,使开 ...
- Spring+MyBatis实践—MyBatis数据库访问
关于spring整合mybatis的工程配置,已经在Spring+MyBatis实践—工程配置中全部详细列出.在此,记录一下几种通过MyBatis访问数据库的方式. 通过sqlSessionTempl ...
- mybatis全局配置mybatis-config.xml
大部分时候,我们都是在Spring 里面去集成MyBatis.因为Spring 对MyBatis 的一些操作进行的封装,我们不能直接看到它的本质,所以先看下不使用容器的时候,也就是编程的方式,MyBa ...
- Mybatis学习-配置、作用域和生命周期
核心配置文件:Mybatis-config.xml Mybatis的配置文件包含了会深深影响Mybatis行为的设置和属性信息 配置(configuration) 在mybatis-config.xm ...
- Mybatis XML配置
Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- MyBatis Cache配置
@(MyBatis)[Cache] MyBatis Cache配置 MyBatis提供了一级缓存和二级缓存 配置 全局配置 配置 说明 默认值 可选值 cacheEnabled 全局缓存的开关 tru ...
- spring和mybatis整合配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
随机推荐
- 关于表 datatable的条件查询
关于表 datatable的条件查询 从数据库中获得一个datatable dt .里面有很多的死的数据.然后,我就是要在这个表中来按条件查询,例如 dt中有个列叫"palte", ...
- AvalonDock 2.0+Caliburn.Micro+MahApps.Metro实现Metro风格插件式系统(一)
随着IOS7由之前UI的拟物化设计变为如今的扁平化设计,也许扁平化的时代要来了,当然我们是不是该吐槽一下,苹果什么时候也开始跟风了,自GOOGLE和微软界面扁平化过后,苹果也加入了这一队伍. Aval ...
- hadoop 数据采样
http://www.cnblogs.com/xuxm2007/archive/2012/03/04/2379143.html 原文地址如上: 关于Hadoop中的采样器 .为什么要使用采样器 在这个 ...
- DevExpress控件使用系列--ASPxTreeList
控件功能 结合列表控件及树控件的优点,在列表控件中实现类型树的多层级操作 官方说明 http://documentation.devexpress.com/#AspNet/clsDevExpres ...
- 网站常用css必备css reset
在我们写前端代码页面的时候,很多常用的CSS类都是固定的!但没有一个标准或者大家都按自己的方式去随意的写,这样就每次都重复写一些固定的类! 为此HTML5 Doctor(HTML5医生)为我们总结了一 ...
- watch your tone
老板要求邮件注意语气... 木想到混了这么久这种事情还要老板提醒
- Python 资源
转:http://www.360doc.com/content/16/0308/14/31385575_540482688.shtml 本页面是俺收集的各种 Python 资源,不定期更新. 下面列出 ...
- 【leetcode】Add Two Numbers(middle) ☆
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- VisualSVN Server的windows 2003配置和使用方法(图文并茂)
1.为什么要用VisualSVN Server,而不用Subversion? 回答: 因为如果直接使用Subversion,那么在Windows 系统上,要想让它随系统启动,就要封装SVN Serve ...
- C Primer Plus 第4章 字符串和格式化输入/输出 编程练习
1. #include <stdio.h> int main(void) { ]; ]; printf("请输入您的名字: "); scanf("%s&quo ...