spring整合JDBC
导入所需jar包
Bank.java
package com.twy.pojo;
public class Bank {
Integer accountId;
String accountname;
Double accountmoney;
public Integer getAccountId() {
return accountId;
}
public void setAccountId(Integer accountId) {
this.accountId = accountId;
}
public String getAccountname() {
return accountname;
}
public void setAccountname(String accountname) {
this.accountname = accountname;
}
public Double getAccountmoney() {
return accountmoney;
}
public void setAccountmoney(Double accountmoney) {
this.accountmoney = accountmoney;
}
@Override
public String toString() {
return "Bank [accountId=" + accountId + ", accountname=" + accountname + ", accountmoney=" + accountmoney + "]";
}
}
IBankDao.java
package com.twy.dao;
import java.util.List;
import com.twy.pojo.Bank;
public interface IBankDao {
void add(Bank bank);
List<Bank> queryAll();
}
BankDao.java
package com.twy.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.twy.pojo.Bank;
public class BankDao extends JdbcDaoSupport implements IBankDao {
// 模板需要一个数据源DataSource,注入
@Override
public void add(Bank bank) {
String sql = "insert into bank values(null,?,?)";
this.getJdbcTemplate().update(sql, bank.getAccountname(), bank.getAccountmoney());
}
@Override
public List<Bank> queryAll() {
String sql = "select * from bank";
List<Bank> data = this.getJdbcTemplate().query(sql, new RowMapper<Bank>() {
@Override
public Bank mapRow(ResultSet rs, int arg1) throws SQLException {
Bank bk = new Bank();
bk.setAccountId(rs.getInt("accountid"));
bk.setAccountname(rs.getString("accountname"));
bk.setAccountmoney(rs.getDouble("accountmoney"));
return bk;
}
});
return data;
}
}
src/beans.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 定义一个数据源,采用系统自带的,也可以采用DBCP, cp30等三方的数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="bankDao" class="com.twy.dao.BankDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
测试类
package com.twy.test;
import java.util.List;
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;
import com.twy.dao.IBankDao;
import com.twy.pojo.Bank;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class TestBankDao {
@Autowired
IBankDao dao;
@Test
public void testAddBank() {
Bank bank = new Bank();
bank.setAccountmoney(600d);
bank.setAccountname("张三");
dao.add(bank);
}
@Test
public void testQueryAll() {
List<Bank> banks = dao.queryAll();
for (Bank bank : banks) {
System.out.println(bank);
}
}
}
结果
事务在spring中基于xml的配置
案例解析: 转账(A账户-----B账户)
IBankDao.java
package com.twy.dao;
public interface IBankDao {
public void inMoney(Integer no, Double money);
public void outMoney(Integer no, Double money);
}
BankDao.java
package com.twy.dao;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class BankDao extends JdbcDaoSupport implements IBankDao {
@Override
public void inMoney(Integer no, Double money) {
String sql = "update bank set accountmoney = accountmoney+? where accountid=?";
this.getJdbcTemplate().update(sql, money, no);
}
@Override
public void outMoney(Integer no, Double money) {
String sql = "update bank set accountmoney = accountmoney-? where accountid=?";
this.getJdbcTemplate().update(sql, money, no);
}
}
IBankService.java
package com.twy.service;
public interface IBankService {
void trans(Integer source, Integer target, Double money);
}
BankService.java
package com.twy.service;
import org.springframework.beans.factory.annotation.Autowired;
import com.twy.dao.IBankDao;
public class BankService implements IBankService {
@Autowired
IBankDao dao;
@Override
public void trans(Integer source, Integer target, Double money) {
// 开始事务
dao.outMoney(source, money);
int i = 10 / 0;//这个抛了一个异常 就回滚上面执行的sql
dao.inMoney(target, money);
// 提交事务
// 如果发生异常,就要回滚事务
// 定义事务的切面
// 定义目标对象
// 将切面切入到目标对象中
}
}
src/beans.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 定义一个数据源,采用系统自带的,也可以采用DBCP, cp30等三方的数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 需要定义事务通知 -->
<tx:advice transaction-manager="transactionManager" id="mytx">
<tx:attributes>
<tx:method name="trans" propagation="REQUIRED" read-only="false" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 织入 -->
<aop:config>
<aop:pointcut expression="execution(* com.twy.service.BankService.trans(..))" id="myCut"/>
<aop:advisor advice-ref="mytx" pointcut-ref="myCut"/>
</aop:config>
<!-- 目标对象 -->
<bean id="bankService" class="com.twy.service.BankService"></bean>
<bean id="bankDao" class="com.twy.dao.BankDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
测试类
package com.twy.test;
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;
import com.twy.service.IBankService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class TestBankDao {
@Autowired
IBankService bs;
@Test
public void testTrans() {
bs.trans(1, 3, 100d);
}
}
执行结果 数据没有变化 回滚了
事务在spring中基于注解的配置
src/beans2.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 定义一个数据源,采用系统自带的,也可以采用DBCP, cp30等三方的数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven/>
<!-- 目标对象 -->
<bean id="bankService" class="com.twy.service.BankService"></bean>
<bean id="bankDao" class="com.twy.dao.BankDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
BankService.java
package com.twy.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.twy.dao.IBankDao;
//可以在类上加注解 也可以添加到方法上
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
public class BankService implements IBankService {
@Autowired
IBankDao dao;
@Override
public void trans(Integer source, Integer target, Double money) {
// 开始事务
dao.outMoney(source, money);
int i = 10 / 0;
dao.inMoney(target, money);
// 提交事务
// 如果发生异常,就要回滚事务
// 定义事务的切面
// 定义目标对象
// 将切面切入到目标对象中
}
}
测试类
package com.twy.test;
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;
import com.twy.service.IBankService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans2.xml")
public class TestBankDao2 {
@Autowired
IBankService bs;
@Test
public void testTrans() {
bs.trans(1, 3, 100d);
}
}
执行结果 和上面是一样的