spring中的jdbc
spring中的jdbc與傳統的jdbc有什麼區別?Spring的jdbc:節省代碼,不管連接(Connection),不管事務、不管異常、不管關閉(con.close() ps.close )由JdbcTemplate實現crud
spring代碼式管理事務
流程:
1.在xml中配置數據源(連接池)dataSource
2.配置事務管理器,注入數據源 DataSourceTransactionManager
3.配置事務管理模板transactionTemplate,注入事務管理器
4.配置JDBC模板JdbcTemplate,注入dao
5.配置service,注入dao和transactionTemplate
(如果我們使xxDao繼承JdbcDaoSupport 他會在底層爲我們直接new一個JdbcTemplate 無需注入,直接向注入數據源)
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1.創建鏈接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/springdemo?useUnicode=true&characterEncoding=UTF8"></property>
<property name="user" value="root"></property>
<property name="password" value="admin"></property>
</bean>
<!-- 2.配置事務管理器 事務需要從Connection 中獲得,鏈接從連接池中獲取 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3.創建事務管理模板 -->
<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager"></property>
</bean>
<bean id="accountDao" class="com.spring.transaction.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="AccountService" class="com.spring.transaction.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
</beans>
package com.spring.transaction;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void AccountIn(String inname, int money) {
String sql = "update account set money=money+? where username=? ";
this.getJdbcTemplate().update(sql, money, inname);
}
@Override
public void AccountOut(String outname, int money) {
String sql = "update account set money=money-? where username=? ";
this.getJdbcTemplate().update(sql, money, outname);
}
}
package com.spring.transaction;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
// 注入
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
//沒有返回值類型 使用TransactionCallbackWithoutResult() 如果有TransactionCallback
@Override
public void AccountIn(String inname, String outname, int money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
accountDao.AccountIn(inname, money);
accountDao.AccountOut(outname, money);
}
});
}
}
聲明式事務管理
半自動
半自動事務流程:
1.在xml中配置數據源(連接池)dataSource
2.配置事務管理器,注入數據源 DataSourceTransactionManager
3.配置JDBC模板JdbcTemplate,注入dao
4.配置service,注入dao
5.配置TransactionProxyFactoryBean生成代理對象
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1創建鏈接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/springdemo?useUnicode=true&characterEncoding=UTF8"></property>
<property name="user" value="root"></property>
<property name="password" value="admin"></property>
</bean>
<!-- 2配置事務管理器 事務需要從Connection 中獲得,鏈接從連接池中獲取 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="accountService" class="com.spring.semiauto_transaction.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.spring.semiauto_transaction.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 工廠bean代理 -->
<!--
proxyInterfaces 代理對象的接口
target 目標類
transactionManager 事務管理器
transactionAttributes 事務屬性
prop.key 確定哪些方法使用當前事務配置
prop.text 用於配置事務詳情
格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
傳播行爲 隔離級別 是否只讀 異常回滾 異常提交
-->
<bean id="proxyService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces"
value="com.spring.semiauto_transaction.AccountService"></property>
<property name="target" ref="accountService"></property>
<property name="transactionManager" ref="txManager"></property>
<property name="transactionAttributes">
<props>
<prop key="AccountIn">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
</props>
</property>
</bean>
</beans>
package com.spring.semiauto_transaction;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
// 注入
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void AccountIn(String inname, String outname, int money) {
accountDao.AccountIn(inname, money);
//int x=1/0;
accountDao.AccountOut(outname, money);
}
}
基於aop自動管理
自動事務流程:
1.在xml中配置數據源(連接池)dataSource
2.配置事務管理器,注入數據源 DataSourceTransactionManager
3.配置JDBC模板JdbcTemplate,注入dao
4.配置service,注入dao
5.配置事務詳情 TransactionDefintion 標籤
6.aop編程
<?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:context="http://www.springframework.org/schema/context"
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/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">
<!-- 創建鏈接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/springdemo?useUnicode=true&characterEncoding=UTF8"></property>
<property name="user" value="root"></property>
<property name="password" value="admin"></property>
</bean>
<!-- 配置事務管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事務詳情 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="AccountIn" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- aop編程 -->
<aop:config>
<aop:pointcut expression="execution(* com.spring.auto_transaction..*.*(..))" id="myPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
</aop:config>
<bean id="accountService" class="com.spring.auto_transaction.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.spring.auto_transaction.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
註解配置
package com.spring.transaction_anno;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
@Service("AccountService")
@Transactional(isolation=Isolation.DEFAULT,propagation= Propagation.REQUIRED)
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
private TransactionTemplate transactionTemplate;
@Autowired
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
// 注入
@Autowired
@Qualifier("accountDao")
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void AccountIn(String inname, String outname, int money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
accountDao.AccountIn(inname, money);
// int x=1/0;
accountDao.AccountOut(outname, money);
}
});
}
}
<?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:context="http://www.springframework.org/schema/context" 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/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">
<!-- 創建鏈接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/springdemo?useUnicode=true&characterEncoding=UTF8"></property>
<property name="user" value="root"></property>
<property name="password" value="admin"></property>
</bean>
<!-- 配置事務管理器 事務需要從Connection 中獲得,鏈接從連接池中獲取 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="accountDao" class="com.spring.transaction_anno.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 組件掃描 -->
<context:component-scan base-package="com.spring.transaction_anno"></context:component-scan>
<!-- 將管理器交予spring
* transaction-manager 配置事務管理器
* proxy-target-class
true : 底層強制使用cglib 代理 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>