💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 配置 ~~~ <?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描包--> <context:component-scan base-package="com.like"/> <!--配置JDBCTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.10.10:3306/jdbc"/> <property name="username" value="homestead"/> <property name="password" value="secret"/> </bean> <!-- spring中基于XML的声明式事务控制配置步骤 1.配置事务管理器 2.配置事务的通知 此时我们需要导入事务的约束,tx名称空间和约束,同时也需要AOP的 使用tx:advice标签配置事务通知 属性: id:给事务起唯一标识 transaction-manager:给事务通知听一个事务管理器饮引用 3.配置AOP中的通用切入点表达式 4.建立事务通知和切入点表达式的对应关系 5.配置事务的属性 在事务tx:advice标签的内部 --> <!-- 1.配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置事务的通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--transfer表示业务实现层的一个方,它需要有一些属性 isolation:用于指定事务的隔离级别.默认值是default,表示使用数据库的默认隔离级别 propagation:用于指定事务的传播行为,默认值是REQUIRED,表示一定会有事务,增删改的选择.查询方法可以选择SUPPORTS read-only:用于指定事务是否可读.只有查询方法才能设置为true.默认值是false,表示读写. timeout:用于指定事务的超时时间,默认值是-1,表示永不超时.如果指定了数值,以秒为单位. rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚,没有默认值,表示任何异常都回滚. no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚.没有默认值,表示任何异常都回滚. --> <tx:method name="transfer" propagation="REQUIRED" read-only="false"/> <!--针对以find开头的方法使用以下配置--> <!-- <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>--> </tx:attributes> </tx:advice> <!--配置AOP--> <aop:config> <aop:pointcut id="pt1" expression="execution(* com.like.service.impl.*ServiceImpl.*(..))"/> <!--建立事务通知和切入点表达式的对应关系--> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/> </aop:config> </beans> ~~~ ## 代码 ~~~ package com.like.domain; import java.io.Serializable; public class Account implements Serializable { private Integer id; private String name; private Float money; @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Float getMoney() { return money; } public void setMoney(Float money) { this.money = money; } } ~~~ ~~~ package com.like.dao; import com.like.domain.Account; public interface IAccountDao { Account findAccountByName(String name); void updateAccount(Account account); } ~~~ ~~~ package com.like.dao.impl; import com.like.dao.IAccountDao; import com.like.domain.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import java.util.List; @Repository public class AccountDaoImpl implements IAccountDao { @Autowired private JdbcTemplate jt; public Account findAccountByName(String name) { List<Account> accounts = jt.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), name); if (accounts.isEmpty()) { return null; } if (accounts.size() > 1) { throw new RuntimeException("结果集不唯一"); } return accounts.get(0); } public void updateAccount(Account account) { jt.update("update account set money = ? where name = ?", account.getMoney(), account.getName()); } } ~~~ ~~~ package com.like.service; public interface IAccountService { void transfer(String sourceName,String targetName,Float money); } ~~~ ~~~ package com.like.service.impl; import com.like.dao.IAccountDao; import com.like.domain.Account; import com.like.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; public void transfer(String sourceName, String targetName, Float money) { Account source = accountDao.findAccountByName(sourceName); Account target = accountDao.findAccountByName(targetName); source.setMoney(source.getMoney() - money); target.setMoney(target.getMoney() + money); accountDao.updateAccount(source); int a = 1/0; accountDao.updateAccount(target); } } ~~~ ## 测试 ~~~ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IAccountService accountServiceImpl = (IAccountService)context.getBean("accountServiceImpl"); accountServiceImpl.transfer("milan","jack",500f); ~~~