1. 配置數據庫連接池
1.1 配置Spring的內置的連接池
< bean id = " dataSource" class = " org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name = " driverClassName" value = " com.mysql.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql://localhost:3306/test04" />
< property name = " username" value = " root" />
< property name = " password" value = " root" />
</ bean>
1.2 配置DBCP連接池
導入DBCP的jar包
< bean id = " dataSource" class = " org.apache.commons.dbcp.BasicDataSource" >
< property name = " driverClassName" value = " com.mysql.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql://localhost:3306/test04" />
< property name = " username" value = " root" />
< property name = " password" value = " root" />
</ bean>
1.3 配置C3P0連接池
導入C3P0的jar包
< bean id = " dataSource" class = " com.mchange.v2.c3p0.ComboPooledDataSource" >
< property name = " driverClass" value = " com.mysql.jdbc.Driver" />
< property name = " jdbcUrl" value = " jdbc:mysql://localhost:3306/test04" />
< property name = " user" value = " root" />
< property name = " password" value = " root" />
</ bean>
1.4 使用屬性文件存儲數據庫信息
在屬性文件jdbc.properties中配置數據庫信息
jdbc. driverClass= com. mysql. jdbc. Driver
jdbc. url= jdbc: mysql: / / localhost: 3306 / test04
jdbc. username= root
jdbc. password= root
< context: property-placeholder location = " classpath:jdbc.properties" />
< bean id = " dataSource" class = " com.mchange.v2.c3p0.ComboPooledDataSource" >
< property name = " driverClass" value = " ${jdbc.driverClass}" />
< property name = " jdbcUrl" value = " ${jdbc.url}" />
< property name = " user" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ bean>
2. JdbcTemplate
2.1 配置Spring的JDBC模版實例
< bean id = " jdbcTemplate" class = " org.springframework.jdbc.core.JdbcTemplate" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
2.2 使用的JdbcTemplate的增刪改操作
ApplicationContext ac= new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
JdbcTemplate jdbcTemplate= ac. getBean ( "jdbcTemplate" , JdbcTemplate. class ) ;
jdbcTemplate. update ( "insert into Account values (null,?,?)" , "田七" , 1000 ) ;
jdbcTemplate. update ( "update Account set money=? where id=?" , 800 , 1 ) ;
jdbcTemplate. update ( "delete from Account where id=?" , 11 ) ;
2.3 查詢返回單個值
ApplicationContext ac= new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
JdbcTemplate jdbcTemplate= ac. getBean ( "jdbcTemplate" , JdbcTemplate. class ) ;
String name= jdbcTemplate. queryForObject ( "select name from Account where id=?" , String. class , 1 ) ;
System. out. println ( name) ;
Long count= jdbcTemplate. queryForObject ( "select count(*) from Account " , Long. class ) ;
System. out. println ( count) ;
2.4 查詢返回封裝成對象的數據
@Test
public void test3 ( ) {
ApplicationContext ac= new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
JdbcTemplate jdbcTemplate= ac. getBean ( "jdbcTemplate" , JdbcTemplate. class ) ;
Account account= jdbcTemplate. queryForObject ( "select * from Account where id=?" , new AccountRowMapper ( ) , 1 ) ;
System. out. println ( account) ;
List< Account> list= jdbcTemplate. query ( "select * from Account" , new AccountRowMapper ( ) ) ;
for ( Account a : list) {
System. out. println ( a) ;
}
}
class AccountRowMapper implements RowMapper < Account> {
@Override
public Account mapRow ( ResultSet rs, int rowNum) throws SQLException {
Account account= new Account ( ) ;
account. setId ( rs. getInt ( "id" ) ) ;
account. setName ( rs. getString ( "name" ) ) ;
account. setMoney ( rs. getDouble ( "money" ) ) ;
return account;
}
}
3.Spring聲明式事務(AOP)
3.1 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" >
< context: property-placeholder location = " classpath:jdbc.properties" />
< bean id = " dataSource" class = " com.mchange.v2.c3p0.ComboPooledDataSource" >
< property name = " driverClass" value = " ${jdbc.driverClass}" />
< property name = " jdbcUrl" value = " ${jdbc.url}" />
< property name = " user" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< tx: advice id = " txAdvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " find*" read-only = " true" />
< tx: method name = " search*" read-only = " true" />
< tx: method name = " get*" read-only = " true" />
< tx: method name = " query*" read-only = " true" />
< tx: method name = " add*" propagation = " REQUIRED" />
< tx: method name = " save*" propagation = " REQUIRED" />
< tx: method name = " del*" propagation = " REQUIRED" />
< tx: method name = " update*" propagation = " REQUIRED" />
< tx: method name = " do*" propagation = " REQUIRED" />
< tx: method name = " transfer" />
</ tx: attributes>
</ tx: advice>
< aop: config proxy-target-class = " true" >
< aop: pointcut expression = " execution(* com.service.impl.*.*(..))" id = " servicePointcut" />
< aop: advisor advice-ref = " txAdvice" pointcut-ref = " servicePointcut" />
</ aop: config>
< bean id = " accountDao" class = " com.dao.impl.AccountDaoImpl" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< bean id = " accountService" class = " com.service.impl.AccountServiceImpl" >
< property name = " accountDao" ref = " accountDao" > </ property>
</ bean>
</ beans>
3.2 轉賬例子
package com. dao. impl;
import org. springframework. jdbc. core. support. JdbcDaoSupport;
import com. dao. AccountDao;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void in ( String name, double money) {
String sql= "update Account set money=money+? where name=?" ;
getJdbcTemplate ( ) . update ( sql, money, name) ;
}
@Override
public void out ( String name, double money) {
String sql= "update Account set money=money-? where name=?" ;
getJdbcTemplate ( ) . update ( sql, money, name) ;
}
}
package com. service. impl;
import com. dao. AccountDao;
import com. service. AccountService;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao ( AccountDao accountDao) {
this . accountDao = accountDao;
}
@Override
public void transfer ( String from, String to, double money) {
accountDao. out ( from, money) ;
accountDao. in ( to, money) ;
}
}
package com. test;
import org. junit. Test;
import org. springframework. context. ApplicationContext;
import org. springframework. context. support. ClassPathXmlApplicationContext;
import com. service. AccountService;
import com. service. impl. AccountServiceImpl;
public class TestAccount {
@Test
public void test ( ) {
ApplicationContext ac= new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
AccountService accountService= ac. getBean ( "accountService" , AccountServiceImpl. class ) ;
accountService. transfer ( "張三" , "李四" , 500 ) ;
}
}
3.3 註解事務
< context: property-placeholder location = " classpath:jdbc.properties" />
< bean id = " dataSource" class = " com.mchange.v2.c3p0.ComboPooledDataSource" >
< property name = " driverClass" value = " ${jdbc.driverClass}" />
< property name = " jdbcUrl" value = " ${jdbc.url}" />
< property name = " user" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ bean>
< bean id = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< tx: annotation-driven transaction-manager = " transactionManager" proxy-target-class = " true" />
@Transactional
public class AccountServiceImpl implements AccountService { }