Java框架spring學習筆記(十七):事務操做

 

事務操做建立service和dao類,完成注入關係java

  • service層叫業務邏輯層
  • dao層單純對數據庫操做層,在dao層不添加業務

 

假設如今有一個轉帳的需求,狗蛋有10000元,建國有20000元,狗蛋向建國轉帳1000元錢。mysql

 

編寫service層建立業務邏輯,OrderService.javaspring

 1 import cn.dao.OrderDao;
 2 
 3 public class OrderService {
 4     private OrderDao orderDao;
 5 
 6     public void setOrderDao(OrderDao orderDao) {
 7         this.orderDao = orderDao;
 8     }
 9 
10     //調用dao的方法
11     //業務邏輯層,寫轉帳業務
12     public void accountMoney(){
13         //狗蛋轉帳給建國,在帳面上看就是狗蛋減錢,建國多錢
14         //狗蛋減錢
15         orderDao.lessMoney();
16         //建國多錢
17         orderDao.moreMoney();
18     }
19 }

 

編寫dao層進行數據庫操做,OrderDao.javasql

 1 package cn.dao;
 2 
 3 import org.springframework.jdbc.core.JdbcTemplate;
 4 
 5 public class OrderDao {
 6     //注入jdbcTemplate
 7     private JdbcTemplate jdbcTemplate;
 8     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
 9         this.jdbcTemplate = jdbcTemplate;
10     }
11 
12     /**
13      * 對數據庫操做,不作業務操做
14      */
15     //狗蛋減錢的方法
16     public void lessMoney(){
17         String sql = "update account set salary=salary-? where username=?";
18         jdbcTemplate.update(sql,1000,"狗蛋");
19     }
20     //建國加錢的方法
21     public void moreMoney(){
22         String sql = "update account set salary=salary+? where username=?";
23         jdbcTemplate.update(sql,1000,"建國");
24     }
25 }

 

編寫配置文件bean.xml數據庫

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:tx="http://www.springframework.org/schema/tx"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 7        xsi:schemaLocation="
 8             http://www.springframework.org/schema/beans
 9             http://www.springframework.org/schema/beans/spring-beans.xsd
10             http://www.springframework.org/schema/context
11             http://www.springframework.org/schema/context/spring-context.xsd
12             http://www.springframework.org/schema/tx
13             http://www.springframework.org/schema/tx/spring-tx.xsd
14             http://www.springframework.org/schema/aop
15             http://www.springframework.org/schema/aop/spring-aop.xsd ">
16 
17     <!-- 配置c3p0鏈接池 -->
18     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
19         <!-- 注入dao對象 -->
20         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
21         <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
22         <property name="user" value="root"></property>
23         <property name="password" value="jqbjqbjqb123"></property>
24     </bean>
25 
26     <bean id="orderService" class="cn.service.OrderService">
27         <property name="orderDao" ref="orderDao"></property>
28     </bean>
29     <bean id="orderDao" class="cn.dao.OrderDao">
30         <!-- 注入jdbcTemplate對象-->
31         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
32     </bean>
33 
34     <!-- 建立jdbcTemplate對象 -->
35     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
36         <!-- 把dataSource傳遞到模板對象中-->
37         <property name="dataSource" ref="dataSource"></property>
38     </bean>
39 
40 </beans>

 

編寫測試文件TestService.javaless

 1 package cn.test;
 2 import cn.service.OrderService;
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class TestService {
 8     @Test
 9     public void testDemo(){
10         ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
11         OrderService orderService = (OrderService) context.getBean("orderService");
12         orderService.accountMoney();
13     }
14 }

文件結構測試

 

運行以後this

數據庫內容發生變化,完成轉帳spa

相關文章
相關標籤/搜索