ssh備考-08 spring的JDBC模板以及事務

目錄

一、spring JDBC模板的使用

1.搭建環境

建表:

新建後臺工程

2.寫applicationContext.xml配置文件

3.測試模板類的使用

JDBCDemo1.java

4.補充:以後主要使用c3p0連接池   其實都一樣,配法不同而已

5.jbdc模板類具體方法使用學習(學過hibernate就都很簡單了 )(瞭解)

二、Spring框架的事務管理

1.搭建基本環境

2.引入xml 注意學會配置

applicationContext.xml

AccountDao.java

AccountDaoImpl.java

AccountService.java

AccountServiceImpl.java

測試類:TestAction.java


今日資料下載: 直接下載spring02.zip       網盤備份下載

一、spring JDBC模板的使用

1.搭建環境

建表:

use spring02;
create table t_account(
	id int primary key auto_increment,
	name varchar(20),
	money double
);

新建後臺工程

2.寫applicationContext.xml配置文件

注入兩個類 連接池類:DriverManagerDataSource      和模板類:jdbcTemplate

兩個正常的類,不是自己寫的罷了

配置好了DriverManagerDataSource,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/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">
	
	<!-- 先配置連接池 
		也是讓spring 幫忙管理一個類 只不過這個類不是自己寫的罷了
	 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql:///spring02"/>
		<property name="username" value="root"/>
		<property name="password" value="root"/>
	</bean>
	
	<!-- 
		再配置jdbc模板類,需要用到的類都丟給spring
		也是一個類,不是自己寫的罷了
	 -->
	 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	 	<!-- 直接注入dataSource 一定是jdbcTemplate類的一個屬性 -->
	 	<property name="dataSource" ref="dataSource"/>
	 </bean>
	
</beans>

3.測試模板類的使用

JDBCDemo1.java

package cn.ahpu.demo;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 演示jdbc的模板類
 * @author 軟件163韓竹安
 * 2019年12月25日-下午2:45:34
 */
//第一行是spring測試的註解   第二行是讓spring自動加載的註解    
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JDBCDemo1 {
	
	/*//演示模板類1  原始new的方式 不需要配置文件
	@Test
	public void run1(){
		//很麻煩,需要自己獲取連接池(DataSource就是連接池)  
		DriverManagerDataSource dataSource=new DriverManagerDataSource();//spring內置連接池
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql:///spring02");
		dataSource.setUsername("root");
		dataSource.setPassword("root");
		
		//創建jdbc模板
		JdbcTemplate template=new JdbcTemplate(dataSource);
		//調用api完成操作
		template.update("insert into t_account values(null,?,?)","天河",10000);
	}*///可以打開直接運行
	
	//IOC方式 spring創建 我們直接拿來用
	//name寫applicationContext.xml裏配置的id 自動幫你注入測試類裏了
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	@Test
	public void run2(){
		jdbcTemplate.update("insert into t_account values(null,?,?)","紫英",20000);
	}
	
}

 

4.補充:以後主要使用c3p0連接池   其實都一樣,配法不同而已

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"/>
		<property name="jdbcUrl" value="jdbc:mysql:///spring02"/>
		<property name="user" value="root"/>
		<property name="password" value="root"/>
</bean>

 

5.jbdc模板類具體方法使用學習(學過hibernate就都很簡單了 )(瞭解)

package cn.ahpu.demo;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 演示jdbc的模板類
 * @author 軟件163韓竹安
 * 2019年12月25日-下午2:45:34
 */
//第一行是spring測試的註解   第二行是讓spring自動加載的註解    
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JDBCDemoMethod {
	
	//IOC方式 spring創建 我們直接拿來用
	//name寫applicationContext.xml裏配置的id 自動幫你注入測試類裏了
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	
	
	/**
	 * update(String sql,Object ...params)   可以完成增刪改 類似dbUtils
	 */
	@Test
	public void run1(){
		//增
		//jdbcTemplate.update("insert into t_account values(null,?,?)","紫英2",20000);
		
		//刪
		//jdbcTemplate.update("delete from t_account where id=?",3);
		
		//改
		jdbcTemplate.update("update t_account set money=? where id=?",8888,2);
		
	}
	//查詢啥的就不測了 反正用不到
}

 

 

二、Spring框架的事務管理

轉賬案例

1.搭建基本環境

 

2.引入xml 注意學會配置

applicationContext.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: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/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">
	
	<!-- 配置連接池類 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql:///spring02" />
		<property name="user" value="root" />
		<property name="password" value="root" />
	</bean>
	
	<!-- spring管理事務必須的配置
		 配資平臺事務管理器
	 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 配資平臺事務管理器也需要連接池 -->
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- 並開啓事務註解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	
	<!-- 配置業務層和持久層 -->
	<bean id="accountService" class="cn.ahpu.demo1.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"/>
	</bean>
	<bean id="accountDao" class="cn.ahpu.demo1.AccountDaoImpl">
		<!-- 直接在dao裏配個連接池  模板類不用配置了 -->
		<property name="dataSource" ref="dataSource"/>
	</bean>

</beans>

AccountDao.java

package cn.ahpu.demo1;

public interface AccountDao {
	public void outMoney(String out,double money);//扣錢
	public void inMoney(String in,double money);//加錢
}

AccountDaoImpl.java

package cn.ahpu.demo1;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

	//配置文件裏有jdbcTemplate  寫個屬性提供set 然後注入不就完了
	/*private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}*/ //更簡單 繼承父類  父類已經有這個屬性了

	
	@Override
	public void outMoney(String out, double money) {
		this.getJdbcTemplate().update("update t_account set money=money-? where name=?",money,out);
	}

	@Override
	public void inMoney(String in, double money) {
		this.getJdbcTemplate().update("update t_account set money=money+? where name=?",money,in);
	}

}

AccountService.java

package cn.ahpu.demo1;

public interface AccountService {
	public void pay(String out,String in,double money);
}

AccountServiceImpl.java

package cn.ahpu.demo1;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class AccountServiceImpl implements AccountService{

	private AccountDao accountDao;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	@Override
	public void pay(String out, String in, double money) {
		//先扣錢
		accountDao.outMoney(out, money);
		int i=1/0;
		//再加錢
		accountDao.inMoney(in, money);
	}

}

測試類:TestAction.java

package cn.ahpu.demo1;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:applicationContext.xml")
public class TestAction {
	@Resource(name="accountService")
	private AccountService accountService;
	
	@Test
	public void run1(){
		//調用支付方法 天河給紫英轉1000
		accountService.pay("天河", "紫英", 1000);
	}
}

 

2個配置:
<!-- spring管理事務必須的配置
         配資平臺事務管理器
     -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 配資平臺事務管理器也需要連接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 並開啓事務註解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

一個註解:
service類上面加"@Transactional" 整個ServiceImpl類的所有方法都添加事務
service類某個具體方法加s"@Transactional" 則僅加了的方法有事務

 

發佈了388 篇原創文章 · 獲贊 89 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章