MyBatis-Spring要点

概述

MyBatis-Spring用于将 MyBatis 代码无缝地整合到 Spring 中。
Spring 使用MyBatis-Spring类库中的类加载必要的 MyBatis 工厂类和 session 类。
MyBatis-Spring提供一个简单的方式把MyBatis 数据映射器和 SqlSession 注入到业务层的 bean 中。
MyBatis-Spring会处理事务。如果使用 了 Spring 的事务,那么当事务完成时,session 将会提交或回滚。最终,任何异常都会被翻 译成 Spring 的 DataAccessException 异常。
MyBatis-Spring要求Java5及以上版本还有下面列出的MyBatis和Spring版本:
|MyBatis-Spring| MyBatis Spring
|1.0.0 或 1.0.1 |3.0.1 到 3.0.5 |3.0.0 或以上
|1.0.2 |3.0.6 |3.0.0 或以上
|1.1.0 |3.1.0 或以上 |3.0.0 或以上

要点

MyBatis-Spring 要和 Spring 一起使用 MyBatis,需要在 Spring 应用上下文中定义至少两样东西:一个 SqlSessionFactory 和至少一个数据映射器类
2、在 MyBatis-Spring 中,SqlSessionFactoryBean 是用于创建 SqlSessionFactory 的。要配置这个工厂 bean,放置下面的代码在 Spring 的 XML 配置文件中:
3、要注意 SqlSessionFactory 需要一个 DataSource(数据源,译者注) 。这可以是任意 的 DataSource,配置它就和配置其它 Spring 数据库连接一样。
4、一旦配置好,你可以用注入其它任意 Spring 的 bean 相同的方式直接注入映射器到你的 business/service 对象中。MapperFactoryBean 处理 SqlSession 的创建和关闭它。如果使用了Spring的事务,那么当事务完成时,session 将会提交或回滚。最终,任何异常都会被翻 译成 Spring 的 DataAccessException 异常。
5、所指定的映射器类必须是一个接口,而不是具体的实现类。在这个示例中,注 解被用来指定 SQL 语句,但是 MyBatis 的映射器 XML 文件也可以用。
那么可以使用 MapperFactoryBean,像下面这样来把接口加入到 Spring 中:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="xxxMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="com.xyz.mapper.XxxMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

6、在yBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建。而在 MyBatis-Spring 中,则使用 SqlSessionFactoryBean 来替代。
7、 SqlSessionFactoryBean 实现了 Spring 的 FactoryBean 接口,这就说明了由 Spring 最终创建的 bean 不是 SqlSessionFactoryBean 本身, 。 而是工厂类的 getObject()返回的方法的结果。这种情况下,Spring 将会在应用启动时为你 创建 SqlSessionFactory 对象,然后将它以 SqlSessionFactory 为名来存储。在 Java 中, 相同的代码是:

SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
SqlSessionFactory sessionFactory = factoryBean.getObject();

8、在一般的 MyBatis-Spring 用法中, 不需要直接使用 SqlSessionFactoryBean 或和其对应的 SqlSessionFactory。相反,session 工厂将会被注入到 MapperFactoryBean 或其它扩 展了 SqlSessionDaoSupport 的 DAO(Data Access Object,数据访问对象)中。
9、如果 MyBatis 映射器 XML 文件在和映射器类相同的路径下不存在,那么另外一个需要 配置文件的原因就是它了。使用这个配置,有两种选择。第一是手动在 MyBatis 的 XML 配 置文件中使用部分来指定类路径。第二是使用工厂 bean 的 mapperLocations 属 性。
10、mapperLocations 属性使用一个资源位置的 list。 这个属性可以用来指定 MyBatis 的 XML 映射器文件的位置。 它的值可以包含 Ant 样式来加载一个目录中所有文件, 或者从基路径下 递归搜索所有路径。比如:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />
</bean>

11、在 MyBatis 中,使用 SqlSessionFactory 来创建 SqlSession。获得一个 session 之后,可以使用它来执行映射语句,提交或回滚, 关闭 session。 使用 MyBatis-Spring 之后, 不需要直接使用 SqlSessionFactory ,因为 bean 可以通过一个线程安全的 SqlSession 来注入,基于 Spring 的事务配置 来自动提交,回滚,关闭 session。
12、注意通常不必直接使用 SqlSession。 在大多数情况下 MapperFactoryBean, 将会在 bean 中注入所需要的映射器。
13、SqlSessionTemplate 是 MyBatis-Spring 的核心。 这个类负责管理 MyBatis 的 SqlSession, 调用 MyBatis 的 SQL 方法。 SqlSessionTemplate 是线程安全的, 可以被多个 DAO 所共享使用。
14、 SqlSessionTemplate 将会保证使用的 SqlSession 是和当前 Spring 的事务相关的。此外,它管理 session 的生命 周期,包含必要的关闭,提交或回滚操作。
15、SqlSessionTemplate 实现了 SqlSession 接口,这就是说,在代码中无需对 MyBatis 的 SqlSession 进行替换。 16、SqlSessionTemplate 通常是被用来替代默认的 MyBatis 实现的 DefaultSqlSession , 因为模板可以参与到 Spring 的事务中并且被多个注入的映射器类所使用时也是线程安全的,相同应用程序中两个类之间的转换可能会引起数据一致性的问题。
17、SqlSessionTemplate 对象可以使用 SqlSessionFactory 作为构造方法的参数来创建。

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory" />
</bean>

这个 bean 现在可以直接注入到 DAO bean 中。你需要在 bean 中添加一个 SqlSession 属性,就像下面的代码:

public class XxxDaoImpl implements XxxDao {
  private SqlSession sqlSession;
  public void setSqlSession(SqlSession sqlSession) {
    this.sqlSession = sqlSession;
  }
  public User getUser(String userId) {
    return (User) sqlSession.selectOne("com.xyz.mapper.XxxMapper.getXxx", xxId);
  }
}
<bean id="xxxDao" class="com.xyz.dao.XxxDaoImpl">
  <property name="sqlSession" ref="sqlSession" />
</bean>

17、SqlSessionTemplate 有一个使用 ExecutorType 作为参数的构造方法。对这种形式需要说明的是当这个方法被调用时,不能有一个存在使用不同 ExecutorType 运行的事务。也要保证在不同的事务中,使用不同执行器来调用 SqlSessionTemplate 时, (比如 PROPAGATION_REQUIRES_NEW)或完全在一个事务外面。这允许你用来 创建对象,比如,一个批量 SqlSession,但是使用了下列 Spring 配置的 XML 文件:

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory" />
  <constructor-arg index="1" value="BATCH" />
</bean>

18、SqlSessionDaoSupport 需要一个 sqlSessionFactory 或 sqlSessionTemplate 属性来 设 置 。 这 些 被 明 确 地 设 置 或 由 Spring 来 自 动 装 配 。 如 果 两 者 都 被 设 置 了 , 那 么 SqlSessionFactory 是被忽略的。
假设类 UserMapperImpl 是 SqlSessionDaoSupport 的子类,它可以在 Spring 中进行如 下的配置:

<bean id="xxxMapper" class="com.xyz.mapper.XxxDaoImpl">
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
public class XxxDaoImpl extends SqlSessionDaoSupport implements XxxDao {
  public Xxx getUser(String xxxId) {
    return (Xxx) getSqlSession().selectOne("com.xyz.mapper.XxxMapper.getXxx", xxxId);
  }
}

使用 MyBatis API
19、使用 MyBatis-Spring,可以继续直接使用 MyBatis 的 API。仅仅在代码中使用 Spring 中 的 SqlSessionFactoryBean 来创建一个 SqlSessionFactory。小心使用此选项, 因为错误的使用会产生运行时错误, 或者更糟糕的数据一致性的问题。

public class XxxMapperSqlSessionImpl implements XxxMapper {
// SqlSessionFactory would normally be set by SqlSessionDaoSupport
private SqlSessionFactory sqlSessionFactory;
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public Xxx getXxx(String xxxId) {
// note standard MyBatis API usage - opening and closing the session manually
SqlSession session = sqlSessionFactory.openSession();
try {
return (Xxx) session.selectOne(“com.xyz.mapper.XxxMapper.getXxx”, xxxId);
} finally {
session.close();
}
}
}
20、Spring中使用 MyBatis 的 API注意事项:
【1】它不会参与到 Spring 的事务之中。
【2】如果 SqlSession 使用 DataSource,它也会被 Spring 事务管理器使用,而且当前 有事务在进行时,这段代码会抛 出异常。
【3】MyBatis 的 DefaultSqlSession 是线程不安全的。如果在 bean 中注入了它,就会 发生错误。
【4】使用 DefaultSqlSession 创建的映射器也不是线程安全的。如果你将它们注入到 bean 中,是会发生错误的。
【5】必须保证在 finally 块中来关闭 SqlSession。

结构

猜你喜欢

转载自blog.csdn.net/zhuralll112/article/details/83387312