pyqb

导航

 

 

事务操作创建service和dao类,完成注入关系

  • service层叫业务逻辑层
  • dao层单纯对数据库操作层,在dao层不添加业务

 

假设现在有一个转账的需求,狗蛋有10000元,建国有20000元,狗蛋向建国转账1000元钱。

 

编写service层创建业务逻辑,OrderService.java

 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.java

 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.java

 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 }

文件结构

 

运行之后

数据库内容发生变化,完成转账

 

posted on 2017-11-10 15:44  没有音乐就退化耳朵  阅读(237)  评论(0编辑  收藏  举报