springDao

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);
	}
}

执行结果 和上面是一样的 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值