Spring | JdbcTemplate

1. 作用:用于和数据库交互,实现对表的操作。

2. Spring提供的事务管理器xml配置代码

<?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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

3. 配置事务属性

<tx:attributes>
            <tx:method name="transfer" isolation="" no-rollback-for="" propagation="" read-only="" rollback-for="" timeout=""/>
</tx:attributes>

属性说明:

isolation:用于指定事务的隔离级别,默认值default表示使用数据库的默认隔离级别

propagation:用于指定事务的传播行为,默认值是REQUIRED,表示一定会有事务,是增删改的选择,查询方法可以选择supports

timeout:用于指定事务的超时时间,默认值是-1,永不超时,指定时间以秒为单位

read-only:指定事务是否只读,只有查询方法才能设置为true,默认值是false,表示读写

rollback-for:指定一个异常,当产生该异常时,事务回滚,产生其他异常,事务不回滚,没有默认值则表示任何异常都回滚。

no-rollback-for:指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚,没有默认值则表示任何异常都回滚。

4. Spring的声明式事务控制可以通过xml或者注解形式进行配置

4.1 spring基于xml的声明式事务增强

<?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"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--配置accountService-->
    <bean id="accountService" class="com.alan.service.impl.AccountServiceImpl">
        <property name="accountDAo" ref="accountDao"></property>
    </bean>
    <!--配置dataSource-->
    <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/db03"></property>
        <property name="username" value="root"></property>
        <property name="password" value="xiaoyahuan1997"></property>
    </bean>
    <!--配置accountDao-->
    <bean id="accountDao" class="com.alan.dao.impl.AccountDao">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--基于xml的声明式事务控制配置-->
    <!--1.配置事务管理器-->
    <bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--2.配置事务增强-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--2.1配置事务的属性-->
        <tx:attributes>
            <!--*通配所有方法-->
            <tx:method name="*" propagation="REQUIRED" read-only="false"></tx:method>
            <!--通配find开头的方法-->
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>
    <!--3.配置aop-->
    <aop:config>
        <!--3.1配置切入点表达式-->
        <aop:pointcut id="pt1" expression="execution(* com.alan.service.impl.*.*(..))"/>
        <!--3.2建立事务增强和切入点表达式的对应关系-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>
</beans>

4.2 spring基于注解的声明式事务增强

<?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:context="http://www.springframework.org/schema/context"
       xmlns:http="http://www.springframework.org/schema/c"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--配置创建容器时要扫描的包-->
    <context:component-scan base-package="com.alan"></context:component-scan>
    <!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置dataSource-->
    <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/db03"></property>
        <property name="username" value="root"></property>
        <property name="password" value="xiaoyahuan1997"></property>
    </bean>
    <!--基于注解的声明式事务控制配置-->
    <!--1.配置事务管理器-->
    <bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--2.开启spring对注解事务的支持-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation- 
    driven>
    <!--3.在需要事务支持的地方使用@Transactional-->
</beans>
@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly=true)
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao;
...
}

4.3 两者的区别

注释方式更简单,但如上图所示,在注释中只能声明一种事务增强配置,但不同的方法需要的增强配置不同,比如查询操作可以设readOnly=true,而增删改必须设readOnly=false,所以在使用中使用xml的方式更为灵活。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值