事务操作创建service和dao类,完成注入关系
- service层叫业务逻辑层
- dao层单纯对数据库操作层,在dao层不添加业务
假设现在有一个转账的需求,狗蛋有10000元,建国有20000元,狗蛋向建国转账1000元钱。
编写service层创建业务逻辑,OrderService.java
import cn.dao.OrderDao; public class OrderService {
private OrderDao orderDao; public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
} //调用dao的方法
//业务逻辑层,写转账业务
public void accountMoney(){
//狗蛋转账给建国,在账面上看就是狗蛋减钱,建国多钱
//狗蛋减钱
orderDao.lessMoney();
//建国多钱
orderDao.moreMoney();
}
}
编写dao层进行数据库操作,OrderDao.java
package cn.dao; import org.springframework.jdbc.core.JdbcTemplate; public class OrderDao {
//注入jdbcTemplate
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} /**
* 对数据库操作,不做业务操作
*/
//狗蛋减钱的方法
public void lessMoney(){
String sql = "update account set salary=salary-? where username=?";
jdbcTemplate.update(sql,1000,"狗蛋");
}
//建国加钱的方法
public void moreMoney(){
String sql = "update account set salary=salary+? where username=?";
jdbcTemplate.update(sql,1000,"建国");
}
}
编写配置文件bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "> <!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 注入dao对象 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
<property name="user" value="root"></property>
<property name="password" value="jqbjqbjqb123"></property>
</bean> <bean id="orderService" class="cn.service.OrderService">
<property name="orderDao" ref="orderDao"></property>
</bean>
<bean id="orderDao" class="cn.dao.OrderDao">
<!-- 注入jdbcTemplate对象-->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!-- 创建jdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 把dataSource传递到模板对象中-->
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>
编写测试文件TestService.java
package cn.test;
import cn.service.OrderService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestService {
@Test
public void testDemo(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
OrderService orderService = (OrderService) context.getBean("orderService");
orderService.accountMoney();
}
}
文件结构
运行之后
数据库内容发生变化,完成转账