一、配置 bean.xml , 链接数据库。
c3p0数据库连接池
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 12 13 <!--注册类 --> 14 <bean id="userDao" class="com.spring_jdbc.dao.UserDAO"> 15 <property name="jdbcTemplate" ref="jdbcTemplate"></property> 16 </bean> 17 18 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 19 <property name="dataSource" ref="comboPooledDataSource"></property> 20 </bean> 21 22 <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 23 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 24 <property name="jdbcUrl" value="jdbc:mysql:///spring"></property> 25 <property name="user" value="root"></property> 26 <property name="password" value="root"></property> 27 </bean> 28 29 </beans>
UserDAO.java
1 package com.spring_jdbc.dao; 2 import org.springframework.jdbc.core.JdbcTemplate; 3 4 public class UserDAO { 5 private JdbcTemplate jdbcTemplate; 6 7 public JdbcTemplate getJdbcTemplate() { 8 return jdbcTemplate; 9 } 10 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 11 this.jdbcTemplate = jdbcTemplate; 12 } 13 14 public void update(){ 15 //第四步:通过模板进行操作 16 String str = "update t_user set money=money-1000 where username=?;"; 17 jdbcTemplate.update(str,"rose"); 18 System.out.println("操作成功。。。"); 19 } 20 } 21 /* 22 //使用编码方式实现c3p0数据库连接池 23 private ComboPooledDataSource dataSource; 24 private JdbcTemplate jdbcTemplate; 25 //第一步:创建连接池核心工具类 26 ComboPooledDataSource dataSource = new ComboPooledDataSource(); 27 28 //第二步:连接池,url,驱动,账号,密码,初始连接数,最大连接数 29 dataSource.setJdbcUrl("jdbc:mysql:///spring");//设置url 30 dataSource.setDriverClass("com.mysql.jdbc.Driver");//设置驱动 31 dataSource.setUser("root");//mysql的账号 32 dataSource.setPassword("root");//mysql的密码 33 34 //第三步:创建模板 35 JdbcTemplate jdbcTemplate = new JdbcTemplate(); 36 jdbcTemplate.setDataSource(dataSource); 37 38 //第四步:通过模板进行操作 39 String str = "update t_user set money=money-1000 where username=?;"; 40 jdbcTemplate.update(str,"rose"); 41 System.out.println("操作成功。。。"); 42 */
service.java
1 package com.spring_jdbc.dao; 2 import org.springframework.context.ApplicationContext; 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 public class Service { 6 7 public static void main(String[] args) throws Exception { 8 ApplicationContext con = new ClassPathXmlApplicationContext("com/spring_jdbc/dao/UserDAO.xml"); 9 UserDAO userDao = (UserDAO)con.getBean("userDao"); 10 userDao.update(); 11 } 12 }
事务管理
一、配置 bean.xml , 链接数据库,创建一个事务管理器。
c3p0数据库连接池
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 11 12 13 14 <!-- 初始化模板 --> 15 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 16 <property name="dataSource" ref="comboPooledDataSource"></property> 17 </bean> 18 19 <!-- 创建数据源(连接池) --> 20 <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 21 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 22 <property name="jdbcUrl" value="jdbc:mysql:///spring"></property> 23 <property name="user" value="root"></property> 24 <property name="password" value="root"></property> 25 </bean> 26 27 <bean id="service" class="com.spring_jdbc.transaction.Service"> 28 <property name="userDao" ref="userDao"></property> 29 </bean> 30 31 <!--注册类 --> 32 <bean id="userDao" class="com.spring_jdbc.transaction.UserDAO"> 33 <property name="jdbcTemplate" ref="jdbcTemplate"></property> 34 </bean> 35 36 <!-- 创建一个事务管理器 --> 37 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 38 <property name="dataSource" ref="comboPooledDataSource"></property> 39 </bean> 40 41 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 42 <tx:attributes> 43 <tx:method name="change*" propagation="REQUIRED" isolation="DEFAULT"/> 44 </tx:attributes> 45 </tx:advice> 46 47 <aop:config> 48 <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.spring_jdbc.transaction.*.*(..))" /> 49 </aop:config> 50 51 52 </beans>
UserDAO.java
1 package com.spring_jdbc.transaction; 2 3 import org.springframework.jdbc.core.JdbcTemplate; 4 5 public class UserDAO { 6 7 private JdbcTemplate jdbcTemplate; 8 9 public JdbcTemplate getJdbcTemplate() { 10 return jdbcTemplate; 11 } 12 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 13 this.jdbcTemplate = jdbcTemplate; 14 } 15 16 17 public void update() throws Exception{ 18 19 //第四步:通过模板进行操作 20 String str = "update t_user set money=money-1000 where username=?;"; 21 jdbcTemplate.update(str,"rose"); 22 System.out.println("操作成功。。。"); 23 24 } 25 26 /** 27 * 转账方 28 * */ 29 public void money_less(){ 30 String str = "update t_user set money=money-? where username=?;"; 31 jdbcTemplate.update(str,1000,"rose"); 32 System.out.println("转出成功。。。"); 33 } 34 /** 35 * 接收方 36 * */ 37 public void money_more(){ 38 String str = "update t_user set money=money+? where username=?;"; 39 jdbcTemplate.update(str,1000,"jack"); 40 System.out.println("接收成功。。。"); 41 } 42 43 }
Service.java
1 package com.spring_jdbc.transaction; 2 3 public class Service { 4 5 private UserDAO userDao; 6 7 public UserDAO getUserDao() { 8 return userDao; 9 } 10 public void setUserDao(UserDAO userDao) { 11 this.userDao = userDao; 12 } 13 14 // 转账的实际操作 15 public void changeAccount(){ 16 userDao.money_less(); 17 // String s1 = null; //模拟异常。 事务回滚,数据不改变 18 // s1.charAt(0); 19 userDao.money_more(); 20 } 21 22 }
test.java
1 package com.spring_jdbc.transaction; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class Action { 7 public static void main(String[] args) { 8 9 ApplicationContext con = new ClassPathXmlApplicationContext("com/spring_jdbc/transaction/UserDao.xml"); 10 Service service = (Service)con.getBean("service"); 11 service.changeAccount(); 12 13 } 14 }