Spring-mybatis的整合

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已经为我们准备好了一切,只需要在配置文件中配置即可。

其中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>

其中省略了一个bean对象的创建(sqlSessionTemplate)

如何实现的呢?

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整合起来才实现的。这种方法简化了代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值