spring mybatis常用集成模式和简单分析
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 配置数据源、事务管理策略、开启声明式事务
(统一由spring管理mapper类型和SqlSessionDaoSupport类型的DAO事务) -->
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:org/mybatis/spring/sample/db/database-schema.sql" />
<jdbc:script location="classpath:org/mybatis/spring/sample/db/database-test-data.sql" />
</jdbc:embedded-database>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven />
<!-- 定义 myBatis的sqlSessionFactory
1、当使用MapperScannerConfigurer时不需要configLocation定义(
1:mapperLocations可以批量载入mapper,但是MapperScannerConfigurer扫描mapper后不会将已存在的mapper加入到Configuration中
2:也可以定义configLocation文件,在里面设置settings和typeAliases等来覆写默认的配置
3:如果使用了configLocation文件,并且里面也定义了mappers,那么在MapperScannerConfigurer扫描动作中就不会加入已经存在的mapper了
(同mapperLocations情况一样)
4:综上所述:建议采用扫描来完成mapper的注册,并且在sqlSessionFactory的bean配置中不需要设置mapperLocations,
如果设置了configLocation文件,那么configLocation文件里也不需要再设置mapper了
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!--扫描 mappers 自动配置 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper" />
</bean>
<!--直接声明mapper的代理实现:!!可能会与扫描动作重复,建议采用扫描来完成 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!--继承 SqlSessionDaoSupport,在dao里可以直接调用mapper xml里的方法,因为所有mapper已经扫描并注册到Configuration里了。
SqlSessionDaoSupport模式和mapper接口模式的事务是一致的,由spring管理-->
<bean id="userDao" class="dao.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!--service示例 -->
<bean id="fooService" class="service.FooServiceDaoImpl">
<property name="userDao" ref="userDao" />
<!-- 或者使用userMapper -->
<property name="userMapper" ref="userMapper" />
</bean>
</beans>
页:
[1]