Spring可谓是软件行业的春天,通过对Spring-mybatis的整合,真切的感受到Spring的强大。
整合版本1:
Spring配置文件:
<!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="a"></property> </bean> <!-- 事务管理 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="select*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.lsw.spring4_mybatis.dao.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /> </aop:config> <!-- 配置sqlSession --> <bean id="sqlSessionFatory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFatory"></constructor-arg> </bean> <bean id="userDao" class="com.lsw.spring4_mybatis.dao.impl.UserDaoImpl"> <property name="sqlSession" ref="sqlSessionTemplate"></property> </bean>
其中Spring生成的对象sqlSessionTemplate,如果有兴趣的同学能够通过源码查看到配置sqlSessionTemplate(用的是构造器注入)
整合版本2:
注意:需要导入mybatis-spring-1.2.3.jar擦<!-- 配置数据源 -->
<!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="a"></property> </bean> <!-- 事务管理 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="select*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.lsw.spring4_mybatis.dao.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /> </aop:config> <!-- 配置sqlSession --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <bean id="userDao" class="com.lsw.spring4_mybatis.dao.impl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
如何实现的呢?
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{ @Override public List<User> selectUser() { return this.getSqlSession().selectList("com.lsw.spring4_mybatis.bean.User.selectUser"); } }继承了SqlSessionDaoSupport类,在SqlSessionDaoSupport类中有setSqlSessionFactory的方法,同时底层也是讲sqlSessionTemplate整合起来才实现的。这种方法简化了代码。