17.Spring中的事务基于XML的配置
1.结构图
2.Account实体类:
package com.itheima.pojo; //Serializable数据序列化 import java.io.Serializable; public class Account implements Serializable { private int id; private String name; private Float money; public int getId() { return id; } public void setId(int 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; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } } /* * 1.Spring中的JdbcTemplate JdbcTemplate的作用: 它就是用于和数据库交互的,实现对表的CRUD操作 如何创建对象: 对象中的常用方法: 2.作业: spring基于AOP的事务控制 3.spring中的事务控制 基于XML的 基于注解的 * */
3.业务层接口:IAccountService
package com.itheima.service; import com.itheima.pojo.Account; public interface IAccountService { //根据Id查询账户 Account findAccountById(Integer accountId); //转账 //目的账户sourceName //对象账户targetName //金额money void transfer(String sourceName,String targetName,Float money); }
4.业务层实现类AccountServiceImpl
package com.itheima.service.imp; import com.itheima.dao.IAccountDao; import com.itheima.pojo.Account; import com.itheima.service.IAccountService; public class AccountServiceImpl implements IAccountService { private IAccountDao accountDao; public void setAccountDao(IAccountDao accountDao) { this.accountDao = accountDao; } @Override public Account findAccountById(Integer accountId) { return accountDao.findAccountById(accountId); } @Override public void transfer(String sourceName, String targetName, Float money) { //2.1.根据名称查询转出账户 Account source = accountDao.findAccountByName(sourceName); //2.2.根据名称查询转入账户 Account target = accountDao.findAccountByName(targetName); //2.3.转出账户减钱 source.setMoney(source.getMoney()-money); //2.4.转入账户加钱 target.setMoney(target.getMoney()+money); //int i = 1/0; //2.5.更新转出账户 accountDao.updateAccount(source); //2.6.更新转入账户 accountDao.updateAccount(target); } }
5.持久层接口IAccountDao:
package com.itheima.dao; import com.itheima.pojo.Account; //账号持久层的实现类接口 public interface IAccountDao { //根据Id查询账户 Account findAccountById(Integer accountId); //根据名称查询账户 Account findAccountByName(String accountName); //更新账户 void updateAccount(Account account); }
6.持久层实现类AccountDaoImpl:
package com.itheima.dao.imp; import com.itheima.dao.IAccountDao; import com.itheima.pojo.Account; import org.springframework.jdbc.core.BeanPropertyRowMapper; //JdbcDaoSupport 自己就有的类,不用外部写 import org.springframework.jdbc.core.support.JdbcDaoSupport; import java.util.List; //持久层的实现类(extends基于XML的方式,就选择这样的) public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao { //按id查询(return accounts.isEmpty()?null:accounts.get(0);id的结果只有一个,查来查去只有一个,所以能使用这种方法) @Override public Account findAccountById(Integer accountId) { /*List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId); return accounts.isEmpty()?null:accounts.get(0);*/ //第二种抽取代码块之后 List<Account> accounts = super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId); return accounts.isEmpty()?null:accounts.get(0); } //这个名字能有多个 @Override public Account findAccountByName(String accountName) { //未抽取代码块之前 /*List<Account> accounts = jdbcTemplate.query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);*/ List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName); if (accounts.isEmpty()){ return null; }if (accounts.size()>1){ throw new RuntimeException("结果集不唯一"); } return accounts.get(0); } //更新账户 @Override public void updateAccount(Account account) { /*jdbcTemplate.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());*/ //使用第二种方法:抽取代码块 super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId()); /*jdbcTemplate.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());*/ } }
7.bean.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置accountService--> <bean id="accountService" class="com.itheima.service.imp.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <bean id="accountDao" class="com.itheima.dao.imp.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置数据源 --> <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/eesy?serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> </beans>
8.pom.xml加入依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- jdbc模版依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- spring环境依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- 事务依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- 连接数据库的依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!--aop依赖--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> <!--整合依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> </dependency> </dependencies>
9.TestCode.java
package com.itheima; import com.itheima.service.IAccountService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) /*说明位置*/ @ContextConfiguration(locations ="classpath:bean.xml") /*pom.xml加上spring-test依赖*/ public class TestCode { @Autowired /*在这个时候:就有两个AccountService,通过@Qualifier来指定名称*/ //@Qualifier("proxyAccountService") //@Resource(name="proxyAccountService") /*这时候当然也可以用@Resource替换上面两个,但是需要导入相关依赖,否则会报空指针异常*/ private IAccountService as; @Test public void testTransfer(){ as.transfer("test","zhou",1000f); } }