日期:2014-05-20 浏览次数:20830 次
<!-- session factory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="yourpackage.dao.entity" />
</bean>
<!-- 用sqlsessionTemplate 替换defaultSqlSession,后者不能参与spring事务,且线程不安全 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- 启用spring事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="yourpackage.dao.ibatis" />
</bean>
<!-- 绑定所有dao的注解 -->
<context:component-scan base-package="yourpackage.dao" />
<context:component-scan base-package="yourpackage.*.dao" />
<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dynamicDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="typeAliasesPackage" value="com.shadow.mvc.model" />
<property name="mapperLocations" value="classpath:com/shadow/mvc/mapper/*.xml" />
</bean>
<!-- use the sqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />