spring框架溫習—JdbcTemplate

JdbcTemplate是spring對JDBC的封裝
(1)導入相關jar包
       spring-jdbc-4.2.4.RELEASE.jar
       spring-tx-4.2.4.RELEASE.jar 事務方面
(2)創建對象,設置數據庫信息
(3)創建jdbcTemplate對象,設置數據源
(4)調用jdbcTemplate對象實現crud
dataSource.setUrl(“jdbc:mysql:///test”);
jdbc:mysql://localhost:3306/test和jdbc:mysql:///test,多一個“/”表示本地的默認端口
localhost:3306和/是一樣的,/默認是本地。那個test是數據庫的名字
關於jdbc的雜談
jdbcTemplate(spring)和 QueryRunner(dbUtil)QueryRunner比較方便,但是一個項目儘量避免使用過多技術spring中已經包含jdbcTemplate則就不用其他的了
c3p0連接池
需要jar包c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar(spring依賴)

/**
		 * (原始方式)代碼配置c3p0連接池代碼  這裏不使用,我們使用spring配置文件完成
		 */
//		ComboPooledDataSource dataSource = new ComboPooledDataSource();
//		dataSource.setDriverClass("com.mysql.jdbc.Driver");
//		dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:5176/test");
//		dataSource.setUser("root");
//		dataSource.setPassword("ajpx_201403");

spring配置文件配置c3p0連接池

	<!-- 配置c3p0連接池 -->
	<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:5176/test"></property>
		<property name="user" value="root"></property>
		<property name="password" value="ajpx_201403"></property>
	</bean>
	
	<!-- 創建對象 注入屬性-->
	<bean id="userService" class="com.idea.c3p0.UserService">
		<property name="userDao" ref="userDao"></property>
	</bean>
	<bean id="userDao" class="com.idea.c3p0.UserDao">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>
	
	<!-- 創建jdbcTemplate對象 dataSource是他的屬性-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

然後在類中定義屬性並寫出setter方法

spring事務管理
事務: 對數據庫操作最基本的單元,指一組操作(都成功,一個失敗則都失敗)
特性: 原子性,一致性,隔離性,持久性
不考慮隔離性會產生讀取問題,髒讀、幻讀、虛度,設置隔離級別可解決
事務管理兩種方式:
(1)編程式事務管理(基本不用)
(2)聲明式事務管理
    ①基於xml配置文件實現
    ②基於註解實現
spring事務管理api
接口:
spring事務管理主要接口
具體操作要用接口實現類
針對事務管理器spring給的接口實現類
事務的具體實現代碼(轉賬業務)
①數據庫
在這裏插入圖片描述
②創建service和dao類,完成注入關係
service層—業務邏輯層

public class OrdersService {

	private OrdersDao ordersDao;

	public void setOrdersDao(OrdersDao ordersDao) {
		this.ordersDao = ordersDao;
	}


	/**
	 * 業務邏輯方法
	 * 轉賬方法
	 */
	public void accountMoney() {
		ordersDao.lessMoney();
		//演示轉賬過程中出現異常情況導致失敗
		//String str = null;
		//str.split(",");
		ordersDao.moreMoney();
	}
}

dao層,持久化層,對數據庫進行操作,不添加業務

public class OrdersDao {

	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	
	//操作數據庫,不寫業務
	 
	/**
	 * 少錢方法
	 */
	public void lessMoney() {
		String sql = "update account set salary = salary - ? where user_name = ?";
		jdbcTemplate.update(sql, 1000, "花花");
	}
	
	/**
	 * 加錢方法
	 */
	public void moreMoney() {
		String sql = "update account set salary = salary + ? where user_name = ?";
		jdbcTemplate.update(sql, 1000, "草草");
	}
}

③xml配置文件

<!--文件中約束在spring框架中比較全面-->
<?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:context="http://www.springframework.org/schema/context"
    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.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd"> 

	<!-- 配置c3p0連接池 -->
	<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:5176/test"></property>
		<property name="user" value="root"></property>
		<property name="password" value="ajpx_201403"></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:attributes>
			<!-- 設置進行事務操作的方法匹配規則 直接寫方法名accountMoney,也可以通配account*,可添加多個 -->
			<tx:method name="account*"/> <!-- propagation="REQUIRED"屬性配置隔離級別 -->
			<!-- <tx:method name="insert*"/> -->
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置事務第二步,配置切面 -->
	<aop:config>
		<!-- 切入點 -->
		<aop:pointcut expression="execution(* com.idea.sw.OrdersService.*(..))" id="pointcut1"/>
		<!-- 切面 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
	</aop:config>
	
	<!-- name屬性值與類中的屬性必須一致 -->
	<bean id="ordersService" class="com.idea.sw.OrdersService">
		<!-- service注入dao屬性 -->
		<property name="ordersDao" ref="ordersDao"></property>
	</bean>
	<bean id="ordersDao" class="com.idea.sw.OrdersDao">
		<!-- 注入jdbcTemplate屬性 -->
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>
	
	<!-- 創建jdbcTemplate對象 dataSource是他的屬性-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
</beans>

④測試類

public class TestAccount {

	@Test
	public void testAcc() {
		ApplicationContext context = 
				new ClassPathXmlApplicationContext("spring-tx.xml");
		OrdersService ordersService = (OrdersService)context.getBean("ordersService");
		ordersService.accountMoney();
	}
}

加入事務管理
1.聲明式事務 xml方式
配置文件

<!-- 配置事務第一步,配置事務管理器 -->
	<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:attributes>
			<!-- 設置進行事務操作的方法匹配規則 直接寫方法名accountMoney,也可以通配account*,可添加多個 -->
			<tx:method name="account*"/> <!-- propagation="REQUIRED"屬性配置隔離級別 -->
			<!-- <tx:method name="insert*"/> -->
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置事務第二步,配置切面 -->
	<aop:config>
		<!-- 切入點 -->
		<aop:pointcut expression="execution(* com.idea.sw.OrdersService.*(..))" id="pointcut1"/>
		<!-- 切面 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
	</aop:config>

事務隔離級別
2.註解方式

	<!-- 配置事務第一步,配置事務管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入需要的屬性dataSource -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- 配置事務第二步,開啓事務管理 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

然後在需要開啓事務的類上加註解@Transactional
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章