1首先创建
Yinhang 自定义实体类
package com.bin.bean;
public class Yinhang {
private String pname;
private Integer money;
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Integer getMoney() {
return money;
}
public void setMoney(Integer money) {
this.money = money;
}
}
2 创建dao层 接口 提供i银行添加和减少方法
package com.bin.dao;
public interface yinghangdao {
void in(String pname,int money);
void out(String pname,int money);
}
2.1和接口实现类
package com.bin.dao.Imp;
import com.bin.dao.yinghangdao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class yinghangImp implements yinghangdao {
@Autowired
private JdbcTemplate JdbcTemplate;
@Override
public void in(String pname, int money) {
JdbcTemplate.update("update yinhang set money=money+? where yname=?",money,pname);
}
@Override
public void out(String pname, int money) {
JdbcTemplate.update("update yinhang set money=money-? where yname=?",money,pname);
}
}
3.service层接口
public interface yinghangservice {
/**
* 从 from 转给 to 多少钱
* @param from
* @param to
* @param money
*/
void transfer(String from,String to,int money);
}
service接口实现类
package com.bin.service;
import com.bin.bean.Yinhang;
import com.bin.dao.yinghangdao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class yinghangserviceImp implements yinghangservice {
@Autowired
private yinghangdao yinghangdao;
@Override
public void transfer(String from, String to, int money) {
yinghangdao.out(from,money);
// System.out.println(1/0);
yinghangdao.in(to,money);
}
}
xml配置文件
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://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 https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--=================注解配置aop====================-->
<!--配置动态代理 ,支持aop的注解-->
<aop:aspectj-autoproxy/>
<!--开启注解扫描-->
<context:component-scan base-package="com.bin"/>
<!--读取jdbc配置文件-->
<context:property-placeholder location="jdbc.properties"/>
<!--c3p0设置配置信息-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置jdbcTemplate模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--==============xml事务管理 (优势,只写1次,永久生效)===============-->
<!--1.配置事务管理器平台 (无论是xml还是注解都需这个配置)-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--2.配置事务通知 , id :事务通知的名称 ,随意给名-->
<tx:advice id="myAdvice">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--3.aop的配置-->
<aop:config>
<!--表达式 指定 事务作用于哪一层-->
<aop:pointcut id="pointcut1" expression="execution(* com.bin.service.*.*(..))"/>
<!--<aop:pointcut id="pointcut1" expression="execution(* cn.hp.dao.*.*(..))"/>-->
<!--advisor 和 aspect都是切面 , 前者一般只配置1个 切入点-->
<aop:advisor advice-ref="myAdvice" pointcut-ref="pointcut1"/>
</aop:config>
</beans>