ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 配置 ~~~ <?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中基于注解的声明式事务控制配置步骤 1.配置事务管理器 2.开启spring对注解事务的支持 3.在需要事务支持的地方使用@Transcational注解 --> <!-- 1.配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--开启spring对注解事务的支持--> <tx:annotation-driven transaction-manager="transactionManager"/> </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; import org.springframework.transaction.annotation.Transactional; @Service @Transactional 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); } } ~~~