<?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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
创建数据库:
create database spring;
use spring;
创建表:
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)character set utf8 collate utf8_general_ci;
package com.yiidian.domain;import java.io.Serializable;
/*** @author http://www.yiidian.com* */
public class Account implements Serializable{private Integer id;private String name;private Float 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;}@Overridepublic String toString() {return "Account [id=" + id + ", name=" + name + ", money=" + money+ "]";}}
jdbc java。AccountService:
package com.yiidian.service;import com.yiidian.domain.Account;
/*** @author http://www.yiidian.com* */
public interface AccountService {/*** 根据id查询账户信息* @param id* @return*/Account findAccountById(Integer id);//查/*** 转账* @param sourceName 转出账户名称* @param targeName 转入账户名称* @param money 转账金额*/void transfer(String sourceName,String targeName,Float money);//增删改
}
AccountServiceImpl:
package com.yiidian.service.impl;import com.yiidian.dao.AccountDao;
import com.yiidian.domain.Account;
import com.yiidian.service.AccountService;
/*** @author http://www.yiidian.com* */
public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic Account findAccountById(Integer id) {return accountDao.findAccountById(id);}@Overridepublic void transfer(String sourceName, String targeName, Float money) {// 1.根据名称查询两个账户Account source = accountDao.findAccountByName(sourceName);Account target = accountDao.findAccountByName(targeName);// 2.修改两个账户的金额source.setMoney(source.getMoney() - money);// 转出账户减钱target.setMoney(target.getMoney() + money);// 转入账户加钱// 3.更新两个账户accountDao.updateAccount(source);//模拟异常int i = 1 / 0;accountDao.updateAccount(target);}
}
AccountDao:
package com.yiidian.dao;import com.yiidian.domain.Account;/*** * @author http://www.yiidian.com* */
public interface AccountDao {/*** 根据id查询账户信息* @param id* @return*/Account findAccountById(Integer id);/*** 根据名称查询账户信息* @return*/Account findAccountByName(String name);/*** 更新账户信息* @param account*/void updateAccount(Account account);
}
AccountDaoImpl:
package com.yiidian.dao.impl;import java.util.List;import org.springframework.jdbc.core.support.JdbcDaoSupport;import com.yiidian.dao.AccountDao;
import com.yiidian.domain.Account;
/*** * @author http://www.yiidian.com* */
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{@Overridepublic Account findAccountById(Integer id) {List<Account> list = getJdbcTemplate().query("select * from account where id = ? ",new AccountRowMapper(),id);return list.isEmpty()?null:list.get(0);}@Overridepublic Account findAccountByName(String name) {List<Account> list = getJdbcTemplate().query("select * from account where name = ? ",new AccountRowMapper(),name);if(list.isEmpty()){return null;}if(list.size()>1){throw new RuntimeException("结果集不唯一,不是只有一个账户对象");}return list.get(0);}@Overridepublic void updateAccount(Account account) {getJdbcTemplate().update("update account set money = ? where id = ? ",account.getMoney(),account.getId());}
}
servlet/session。AccountRowMapper:
package com.yiidian.dao.impl;import java.sql.ResultSet;
import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;import com.yiidian.domain.Account;
/*** 账户的封装类RowMapper的实现类* @author http://www.yiidian.com**/
public class AccountRowMapper implements RowMapper<Account>{@Overridepublic Account mapRow(ResultSet rs, int rowNum) throws SQLException {Account account = new Account();account.setId(rs.getInt("id"));account.setName(rs.getString("name"));account.setMoney(rs.getFloat("money"));return account;}
}
<?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.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties" /></bean><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="${jdbc.driverClass}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- 在AccountDaoImpl类直接注入dataSource即可 --><bean id="accountDao" class="com.yiidian.dao.impl.AccountDaoImpl"><property name="dataSource" ref="dataSource" /></bean><bean id="accountService" class="com.yiidian.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><!-- 配置一个事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 注入DataSource --><property name="dataSource" ref="dataSource"></property></bean><!-- 事务的配置 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><!--在tx:advice标签内部 配置事务的属性 --><tx:attributes><!-- 指定方法名称:是业务核心方法 read-only:是否是只读事务。默认false,不只读。 isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。 propagation:指定事务的传播行为。 timeout:指定超时时间。默认值为:-1。永不超时。 rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。没有默认值,任何异常都回滚。 no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值,任何异常都回滚。 --><tx:method name="*" read-only="false" propagation="REQUIRED" /><tx:method name="find*" read-only="true" propagation="SUPPORTS" /></tx:attributes></tx:advice><!-- 配置aop --><aop:config><!-- 配置切入点表达式 --><aop:pointcut expression="execution(* com.yiidian.service.impl.*.*(..))"id="pt1" /><aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/></aop:config></beans>
package com.yiidian.test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiidian.service.AccountService;/*** @author http://www.yiidian.com* */
public class JdbcTemplateDemo {/*** 添加操作*/@Testpublic void test1() {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");AccountService accountService = (AccountService)ac.getBean("accountService");accountService.transfer("小苍", "小泽", 300f);}}
源码下载:http://pan.baidu.com/s/1bpNelwr
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态