mybatis注解详解
首先当然得下载 mybatis-3.0.5.jar 和 mybatis-spring-1.0.1.jar 两个 JAR 包,并放在 WEB-INF 的 lib 目录下 (如果你使用 maven,则 jar 会根据你的 pom 配置的依赖自动下载,并存放在你指定的 maven 本地库中,默认是 ~/.m2/repository),前一个是 mybatis 核心包,后一个是和 spring 整合的包。
使用 mybatis,必须有个全局配置文件 configuration.xml,来配置 mybatis 的缓存,延迟加载等等一系列属性,该配置文件示例如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
- "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
- <configuration>
- <settings>
- <!-- 全局映射器启用缓存 -->
- <setting name="cacheEnabled" value="true" />
- <!-- 查询时,关闭关联对象即时加载以提高性能 -->
- <setting name="lazyLoadingEnabled" value="true" />
- <!-- 设置关联对象加载的形态,此处为按需加载字段 (加载字段由 SQL 指 定),不会加载关联表的所有字段,以提高性能 -->
- <setting name="aggressiveLazyLoading" value="false" />
- <!-- 对于未知的 SQL 查询,允许返回不同的结果集以达到通用的效果 -->
- <setting name="multipleResultSetsEnabled" value="true" />
- <!-- 允许使用列标签代替列名 -->
- <setting name="useColumnLabel" value="true" />
- <!-- 允许使用自定义的主键值 ( 比如由程序生成的 UUID 32位编码作为键值 ),数据表的 PK 生成策略将被覆盖 -->
- <setting name="useGeneratedKeys" value="true" />
- <!-- 给予被嵌套的 resultMap 以字段 - 属性的映射支持 -->
- <setting name="autoMappingBehavior" value="FULL" />
- <!-- 对于批量更新操作缓存 SQL 以提高性能 -->
- <setting name="defaultExecutorType" value="BATCH" />
- <!-- 数据库超过25000秒仍未响应则超时 -->
- <setting name="defaultStatementTimeout" value="25000" />
- </settings>
- <!-- 全局别名设置,在映射文件中只需写别名,而不必写出整个类路径 -->
- <typeAliases>
- <typeAlias alias="TestBean"
- type="com.wotao.taotao.persist.test.dataobject.TestBean" />
- </typeAliases>
- <!-- 非注解的 sql 映射文件配置,如果使用 mybatis 注解,该 mapper 无需配置,但是如果 mybatis 注解中包含@resultMap注解,则 mapper 必须配置,给 resultMap 注解使用 -->
- <mappers>
- <mapper resource="persist/test/orm/test.xml" />
- </mappers>
- </configuration>
该文件放在资源文件的任意 classpath 目录下,假设这里就直接放在资源根目录,等会 spring 需要引用该文件。
查看 ibatis-3-config.dtd 发现除了 settings 和 typeAliases 还有其他众多元素,比如 properties,objectFactory,environments 等等,这些元素基本上都包含着一些环境配置,数据源定义,数据库事务等等,在单独使用 mybatis 的时候非常重要,比如通过以构造参数的形式去实例化一个 sqlsessionFactory,就像这样:
- SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader);
- SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader, properties);
- SqlSessionFactory factory = sqlSessionFactoryBuilder.build(reader, environment, properties);
而 typeHandlers 则用来自定义映射规则,如你可以自定义将 Character 映射为 varchar,plugins 元素则放了一些拦截器接口,你可以继承他们并做一些切面的事情,至于每个元素的细节和使用,你参考 mybatis 用户指南即可。
现在我们用的是 spring,因此除 settings 和 typeAliases 元素之外,其他元素将会失效,故不在此配置,spring 会覆盖这些元素的配置,比如在 spring 配置文件中指定 c3p0 数据源定义如下:
- <!-- c3p0 connection pool configuration -->
- <bean id="testDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <!-- 数据库驱动 -->
- <property name="driverClass" value="${db.driver.class}" />
- <!-- 连接 URL 串 -->
- <property name="jdbcUrl" value="${db.url}" />
- <!-- 连接用户名 -->
- <property name="user" value="${db.username}" />
- <!-- 连接密码 -->
- <property name="password" value="${db.password}" />
- <!-- 初始化连接池时连接数量为5个 -->
- <property name="initialPoolSize" value="5" />
- <!-- 允许最小连接数量为5个 -->
- <property name="minPoolSize" value="5" />
- <!-- 允许最大连接数量为20个 -->
- <property name="maxPoolSize" value="20" />
- <!-- 允许连接池最大生成100个 PreparedStatement 对象 -->
- <property name="maxStatements" value="100" />
- <!-- 连接有效时间,连接超过3600秒未使用,则该连接丢弃 -->
- <property name="maxIdleTime" value="3600" />
- <!-- 连接用完时,一次产生的新连接步进值为2 -->
- <property name="acquireIncrement" value="2" />
- <!-- 获取连接失败后再尝试10次,再失败则返回 DAOException 异常 -->
- <property name="acquireRetryAttempts" value="10" />
- <!-- 获取下一次连接时最短间隔600毫秒,有助于提高性能 -->
- <property name="acquireRetryDelay" value="600" />
- <!-- 检查连接的有效性,此处小弟不是很懂什么意思 -->
- <property name="testConnectionOnCheckin" value="true" />
- <!-- 每个1200秒检查连接对象状态 -->
- <property name="idleConnectionTestPeriod" value="1200" />
- <!-- 获取新连接的超时时间为10000毫秒 -->
- <property name="checkoutTimeout" value="10000" />
- </bean>
配置中的 ${} 都是占位符,在你指定数据库驱动打 war 时会自动替换,替换的值在你的父 pom 中配置,至于 c3p0 连接池的各种属性详细信息和用法,你自行参考 c3p0 的官方文档,这里要说明的是 checkoutTimeout 元素,记得千万要设大一点,单位是毫秒,假如设置太小,有可能会导致没等数据库响应就直接超时了,小弟在这里吃了不少苦头,还是基本功太差。
数据源配置妥当之后,我们就要开始非常重要的 sessionFactory 配置了,无论是 hibernate 还是 mybatis,都需要一个 sessionFactory 来生成 session,sessionFactory 配置如下:
- <bean id="testSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="configLocation" value="classpath:configuration.xml" />
- <property name="dataSource" ref="testDataSource" />
- </bean>
testSqlSessionFactory 有两处注入,一个就是前面提到的 mybatis 全局设置文件 configuration.xml,另一个就是上面定义的数据源了(注:hibernate 的 sessionFactory 只需注入 hibernate.cfg.xml,数据源定义已经包含在该文件中),好了,sessionFactory 已经产生了,由于我们用的 mybatis3 的注解,因此 spring 的 sqlSessionTemplate 也不用配置了,sqlSessionTemplate 也不用注入到我们的 BaseDAO 中了,相应的,我们需要配置一个映射器接口来对应 sqlSessionTemplate,该映射器接口定义了你自己的接口方法,具体实现不用关心,代码如下:
- <!-- data OR mapping interface -->
- <bean id="testMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="sqlSessionFactory" ref="testSqlSessionFactory" />
- <property name="mapperInterface" value="com.wotao.taotao.persist.test.mapper.TestMapper" />
- </bean>
对应于 sqlSessionTemplate,testMapper 同样需要 testSqlSessionFactory 注入,另外一个注入就是你自己定义的 Mapper 接口,该接口定义了操作数据库的方法和 SQL 语句以及很多的注解,稍后我会讲到。到此,mybatis 和 spring 整合的文件配置就算 OK 了(注:如果你需要开通 spring 对普通类的代理功能,那么你需要在 spring 配置文件中加入 <aop:aspectj-autoproxy />),至于其他的如事务配置,AOP 切面注解等内容不在本文范围内,不作累述。
至此,一个完整的 myabtis 整合 spring 的配置文件看起来应该如下所示:
- <?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"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <!-- c3p0 connection pool configuration -->
- <bean id="testDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="${db.driver.class}" />
- <property name="jdbcUrl" value="${db.url}" />
- <property name="user" value="${db.username}" />
- <property name="password" value="${db.password}" />
- <property name="initialPoolSize" value="5" />
- <property name="minPoolSize" value="5" />
- <property name="maxPoolSize" value="20" />
- <property name="maxStatements" value="100" />
- <property name="maxIdleTime" value="3600" />
- <property name="acquireIncrement" value="2" />
- <property name="acquireRetryAttempts" value="10" />
- <property name="acquireRetryDelay" value="600" />
- <property name="testConnectionOnCheckin" value="true" />
- <property name="idleConnectionTestPeriod" value="1200" />
- <property name="checkoutTimeout" value="10000" />
- </bean>
- <bean id="testSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="configLocation" value="classpath:configuration.xml" />
- <property name="dataSource" ref="testDataSource" />
- </bean>
- <!-- data OR mapping interface -->
- <bean id="testMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="sqlSessionFactory" ref="testSqlSessionFactory" />
- <property name="mapperInterface" value="com.wotao.taotao.persist.test.mapper.TestMapper" />
- </bean>
- <!-- add your own Mapper here -->
- <!-- comment here, using annotation -->
- <!-- <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> -->
- <!-- <constructor-arg index="0" ref="sqlSessionFactory" /> -->
- <!-- </bean> -->
- <!-- base DAO class, for module business, extend this class in DAO -->
- <!-- <bean id="testBaseDAO" class="com.test.dao.TestBaseDAO"> -->
- <!-- <property name="sqlSessionTemplate" ref="sqlSessionTemplate" /> -->
- <!-- </bean> -->
- <!-- <bean id="testDAO" class="com.test.dao.impl.TestDAOImpl" /> -->
- <!-- you can DI Bean if you don't like use annotation -->
- </beans>
到此为止,我们只讲了 mybatis 和 spring 的整合,还没有真正触及 mybatis 的核心:使用 mybatis 注解代替映射文件编程(不过官方文档也说了,如果真正想发挥 mybatis 功能,还是需要用到映射文件,看来 myabtis 自己都对 mybatis 注解没信心,呵呵),通过上述内容,我们知道配置搞定,但是 testMapper 还没有被实现,而注解的使用,全部集中在这个 testMapper 上,是 mybatis 注解的核心所在,先来看一下这个 testMapper 接口是个什么样的:
- /**
- * The test Mapper interface.
- *
- * @author HuangMin <a href="mailto:minhuang@hengtiansoft.com>send email</a>
- *
- * @since 1.6
- * @version 1.0
- *
- * #~TestMapper.java 2011-9-23 : afternoon 10:51:40
- */
- @CacheNamespace(size = 512)
- public interface TestMapper {
- /**
- * get test bean by UID.
- *
- * @param id
- * @return
- */
- @SelectProvider(type = TestSqlProvider.class, method = "getSql")
- @Options(useCache = true, flushCache = false, timeout = 10000)
- @Results(value = {
- @Result(id = true, property = "id", column = "test_id", javaType = String.class, jdbcType = JdbcType.VARCHAR),
- @Result(property = "testText", column = "test_text", javaType = String.class, jdbcType = JdbcType.VARCHAR) })
- public TestBean get(@Param("id") String id);
- /**
- * get all tests.
- *
- * @return
- */
- @SelectProvider(type = TestSqlProvider.class, method = "getAllSql")
- @Options(useCache = true, flushCache = false, timeout = 10000)
- @Results(value = {
- @Result(id = true, property = "id", column = "test_id", javaType = String.class, jdbcType = JdbcType.VARCHAR),
- @Result(property = "testText", column = "test_text", javaType = String.class, jdbcType = JdbcType.VARCHAR) })
- public List<TestBean> getAll();
- /**
- * get tests by test text.
- *
- * @param testText
- * @return
- */
- @SelectProvider(type = TestSqlProvider.class, method = "getByTestTextSql")
- @Options(useCache = true, flushCache = false, timeout = 10000)
- @ResultMap(value = "getByTestText")
- public List<TestBean> getByTestText(@Param("testText") String testText);
- /**
- * insert a test bean into database.
- *
- * @param testBean
- */
- @InsertProvider(type = TestSqlProvider.class, method = "insertSql")
- @Options(flushCache = true, timeout = 20000)
- public void insert(@Param("testBean") TestBean testBean);
- /**
- * update a test bean with database.
- *
- * @param testBean
- */
- @UpdateProvider(type = TestSqlProvider.class, method = "updateSql")
- @Options(flushCache = true, timeout = 20000)
- public void update(@Param("testBean") TestBean testBean);
- /**
- * delete a test by UID.
- *
- * @param id
- */
- @DeleteProvider(type = TestSqlProvider.class, method = "deleteSql")
- @Options(flushCache = true, timeout = 20000)
- public void delete(@Param("id") String id);
- }
下面逐个对里面的注解进行分析:
@CacheNamespace(size = 512) : 定义在该命名空间内允许使用内置缓存,最大值为 512 个对象引用,读写默认是开启的,缓存内省刷新时间为默认 3600000 毫秒,写策略是拷贝整个对象镜像到全新堆(如同 CopyOnWriteList)因此线程安全。
@SelectProvider(type = TestSqlProvider.class, method = "getSql") : 提供查询的 SQL 语句,如果你不用这个注解,你也可以直接使用 @Select("select * from ....") 注解,把查询 SQL 抽取到一个类里面,方便管理,同时复杂的 SQL 也容易操作,type = TestSqlProvider.class 就是存放 SQL 语句的类,而 method = "getSql" 表示 get 接口方法需要到 TestSqlProvider 类的 getSql 方法中获取 SQL 语句。
@Options(useCache = true, flushCache = false, timeout = 10000) : 一些查询的选项开关,比如 useCache = true 表示本次查询结果被缓存以提高下次查询速度,flushCache = false 表示下次查询时不刷新缓存,timeout = 10000 表示查询结果缓存 10000 秒。
@Results(value = {
@Result(id = true, property = "id", column = "test_id", javaType = String.class, jdbcType = JdbcType.VARCHAR),
@Result(property = "testText", column = "test_text", javaType = String.class, jdbcType = JdbcType.VARCHAR) }): 表示 sql 查询返回的结果集,@Results 是以 @Result 为元素的数组,@Result 表示单条属性 - 字段的映射关系,如:@Result(id = true, property = "id", column = "test_id", javaType = String.class, jdbcType = JdbcType.VARCHAR) 可以简写为:@Result(id = true, property = "id", column = "test_id"),id = true 表示这个 test_id 字段是个 PK,查询时 mybatis 会给予必要的优化,应该说数组中所有的 @Result 组成了单个记录的映射关系,而 @Results 则单个记录的集合。另外还有一个非常重要的注解 @ResultMap 也和 @Results 差不多,到时会讲到。
@Param("id") :全局限定别名,定义查询参数在 sql 语句中的位置不再是顺序下标 0,1,2,3.... 的形式,而是对应名称,该名称就在这里定义。
@ResultMap(value = "getByTestText") :重要的注解,可以解决复杂的映射关系,包括 resultMap 嵌套,鉴别器 discriminator 等等。注意一旦你启用该注解,你将不得不在你的映射文件中配置你的 resultMap,而 value = "getByTestText" 即为映射文件中的 resultMap ID(注意此处的 value = "getByTestText",必须是在映射文件中指定命名空间路径)。@ResultMap 在某些简单场合可以用 @Results 代替,但是复杂查询,比如联合、嵌套查询 @ResultMap 就会显得解耦方便更容易管理。
一个映射文件如下所示:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
- "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
- <mapper namespace="com.wotao.taotao.persist.test.mapper.TestMapper">
- <resultMap id="getByTestText" type="TestBean">
- <id property="id" column="test_id" javaType="string" jdbcType="VARCHAR" />
- <result property="testText" column="test_text" javaType="string" jdbcType="VARCHAR" />
- </resultMap>
- </mapper>
注意文件中的 namespace 路径必须是使用 @resultMap 的类路径,此处是 TestMapper,文件中 id="getByTestText" 必须和 @resultMap 中的 value = "getByTestText" 保持一致。
@InsertProvider(type = TestSqlProvider.class, method = "insertSql") :用法和含义 @SelectProvider 一样,只不过是用来插入数据库而用的。
@Options(flushCache = true, timeout = 20000) :对于需要更新数据库的操作,需要重新刷新缓存 flushCache = true 使缓存同步。
@UpdateProvider(type = TestSqlProvider.class, method = "updateSql") :用法和含义 @SelectProvider 一样,只不过是用来更新数据库而用的。
@Param("testBean") :是一个自定义的对象,指定了 sql 语句中的表现形式,如果要在 sql 中引用对象里面的属性,只要使用 testBean.id,testBean.textText 即可,mybatis 会通过反射找到这些属性值。
@DeleteProvider(type = TestSqlProvider.class, method = "deleteSql") :用法和含义 @SelectProvider 一样,只不过是用来删除数据而用的。
现在 mybatis 注解基本已经讲完了,接下来我们就要开始写 SQL 语句了,因为我们不再使用映射文件编写 SQL,那么就不得不在 java 类里面写,就像上面提到的,我们不得不在 TestSqlProvider 这个类里面写 SQL,虽然已经把所有 sql 语句集中到了一个类里面去管理,但听起来似乎仍然有点恶心,幸好 mybatis 提供 SelectBuilder 和 SqlBuilder 这 2 个小工具来帮助我们生成 SQL 语句,SelectBuilder 专门用来生成 select 语句,而 SqlBuilder 则是一般性的工具,可以生成任何 SQL 语句,我这里选择了 SqlBuilder 来生成,TestSqlProvider 代码如下:
- /*
- * #~ test-afternoon10:51:40
- */
- package com.wotao.taotao.persist.test.sqlprovider;
- import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
- import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
- import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
- import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
- import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;
- import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
- import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
- import static org.apache.ibatis.jdbc.SqlBuilder.SET;
- import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
- import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
- import java.util.Map;
- /**
- * The test sql Provider,define the sql script for mapping.
- *
- * @author HuangMin <a href="mailto:minhuang@hengtiansoft.com>send email</a>
- *
- * @since 1.6
- * @version 1.0
- *
- * #~TestSqlProvider.java 2011-9-23 : afternoon 10:51:40
- */
- public class TestSqlProvider {
- /** table name, here is test */
- private static final String TABLE_NAME = "test";
- /**
- * get test by id sql script.
- *
- * @param parameters
- * @return
- */
- public String getSql(Map<String, Object> parameters) {
- String uid = (String) parameters.get("id");
- BEGIN();
- SELECT("test_id, test_text");
- FROM(TABLE_NAME);
- if (uid != null) {
- WHERE("test_id = #{id,javaType=string,jdbcType=VARCHAR}");
- }
- return SQL();
- }
- /**
- * get all tests sql script.
- *
- * @return
- */
- public String getAllSql() {
- BEGIN();
- SELECT("test_id, test_text");
- FROM(TABLE_NAME);
- return SQL();
- }
- /**
- * get test by test text sql script.
- *
- * @param parameters
- * @return
- */
- public String getByTestTextSql(Map<String, Object> parameters) {
- String tText = (String) parameters.get("testText");
- BEGIN();
- SELECT("test_id, test_text");
- FROM(TABLE_NAME);
- if (tText != null) {
- WHERE("test_text like #{testText,javaType=string,jdbcType=VARCHAR}");
- }
- return SQL();
- }
- /**
- * insert a test sql script.
- *
- * @return
- */
- public String insertSql() {
- BEGIN();
- INSERT_INTO(TABLE_NAME);
- VALUES("test_id", "#{testBean.id,javaType=string,jdbcType=VARCHAR}");
- VALUES("test_text", "#{testBean.testText,javaType=string,jdbcType=VARCHAR}");
- return SQL();
- }
- /**
- * update a test sql script.
- *
- * @return
- */
- public String updateSql() {
- BEGIN();
- UPDATE(TABLE_NAME);
- SET("test_text = #{testBean.testText,javaType=string,jdbcType=VARCHAR}");
- WHERE("test_id = #{testBean.id,javaType=string,jdbcType=VARCHAR}");
- return SQL();
- }
- /**
- * delete a test sql script.
- *
- * @return
- */
- public String deleteSql() {
- BEGIN();
- DELETE_FROM(TABLE_NAME);
- WHERE("test_id = #{id,javaType=string,jdbcType=VARCHAR}");
- return SQL();
- }
- }
BEGIN(); 表示刷新本地线程,某些变量为了线程安全,会先在本地存放变量,此处需要刷新。
SELECT,FROM,WHERE 等等都是 sqlbuilder 定义的公用静态方法,用来组成你的 sql 字符串。如果你在 testMapper 中调用该方法的某个接口方法已经定义了参数 @Param(),那么该方法的参数 Map<String, Object> parameters 即组装了 @Param() 定义的参数,比如 testMapper 接口方法中定义参数为 @Param("testId"),@Param("testText"),那么 parameters 的形态就是:[key="testId",value=object1],[key="testText",value=object2],如果接口方法没有定义 @Param(),那么 parameters 的 key 就是参数的顺序小标:[key=0,value=object1],[key=1,value=object2],SQL() 将返回最终 append 结束的字符串,sql 语句中的形如
#{id,javaType=string,jdbcType=VARCHAR} 完全可简写为 #{id},我只是为了规整如此写而已。另外,对于复杂查询还有很多标签可用,比如:JOIN,INNER_JOIN,GROUP_BY,ORDER_BY 等等,具体使用详情,你可以查看源码。
最后记得把你的 Mapper 接口注入到你的 DAO 类中,在 DAO 中引用 Mapper 接口方法即可。我在 BaseDAO 中的注解注入如下:
- ......
- @Repository("testBaseDAO")
- public class TestBaseDAO {
- ......
- ......
- /**
- * @param testMapper
- * the testMapper to set
- */
- @Autowired
- public void setTestMapper(@Qualifier("testMapper") TestMapper testMapper) {
- this.testMapper = testMapper;
- }
- ......
引自:http://wwww.iteye.com/blog/1235996