不啰嗦直接看东西

package charpter0619.one;
//接口,定义涨价和降价的动物
public interface AnimalManger {
public void Transfer(String animalout,String animalin,float price );
}

和之前的代码差不多,这次简化了数据库的增删查操作,直接进行修改–

package charpter0619.one.next;

public class Animal {
private Integer animal_ID;
private String animal_type;
private String animal_name;
private float animal_price;
private Integer animal_num;
public Integer getAnimal_ID() {
return animal_ID;
}
public void setAnimal_ID(Integer animal_ID) {
this.animal_ID = animal_ID;
}
public String getAnimal_type() {
return animal_type;
}
public void setAnimal_type(String animal_type) {
this.animal_type = animal_type;
}
public String getAnimal_name() {
return animal_name;
}
public void setAnimal_name(String animal_name) {
this.animal_name = animal_name;
}
public float getAnimal_price() {
return animal_price;
}
public void setAnimal_price(float animal_price) {
this.animal_price = animal_price;
}
public Integer getAnimal_num() {
return animal_num;
}
public void setAnimal_num(Integer animal_num) {
this.animal_num = animal_num;
}
@Override
public String toString() {
return "Animal [animal_ID=" + animal_ID + ", animal_type=" + animal_type + ", animal_name=" + animal_name
+ ", animal_price=" + animal_price + ", animal_num=" + animal_num + "]";
}

}
package charpter0619.one.next;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import charpter0619.one.AnimalManger;

@Controller("manageAimal") // 控制层
public class ManageAnaimal implements AnimalManger {
@Resource(name = "jdbcTemplate") // 注入
private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Resource(name = "animal") // 注入
private Animal animal;

public void setAnimal(Animal animal) {
this.animal = animal;
}
@Transactional//在这里进行声明事务管理--表示支队该方法有效,如果有其他方法也涉及到事务,则不对他们生效,相关参数已在配置文件中出现,所以这里不重复定义
@Override
public void Transfer(String animalout, String animalin, float price) {// 动物涨价和降价
String sql = "update animalshop set animal_price=animal_price+? where animal_name=?";
this.jdbcTemplate.update(sql, price, animalin);
System.out.println(animalin + "--涨价成功");
String parm = "update animalshop set animal_price=animal_price-? where animal_name=?";
this.jdbcTemplate.update(parm, price, animalout);
System.out.println(animalout + "--降价成功");

}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
">
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/company?serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="955945" />
</bean>
<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="animal" class="charpter0619.one.next.Animal" />
<bean id="managerAnimal"
class="charpter0619.one.next.ManageAnaimal">
<property name="jdbcTemplate" ref="jdbcTemplate" />
<property name="animal" ref="animal" />
</bean>
<!-- 配置事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
autowire="byName" />
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"
isolation="DEFAULT" read-only="false" />
</tx:attributes>

</tx:advice>

<!--和xml配置相比,annotation只是缺少了AOP切面,少了切面,切入点的相应配置,但是要注册annotation驱动,同时如果执行方法在别的包的话,还有加上扫描-->
<!-- 注册事务管理器驱动 -->

<tx:annotation-driven transaction-manager="transactionManager" />

</beans>
package charpter0619.one;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("NewFile.xml");
AnimalManger animal =(AnimalManger)app.getBean("managerAnimal");
animal.Transfer("小刚","小花", 100.5f);
}

}

Spring事务管理入门实操--annotation方式的事务声明_xml

总结,如果aop配置量少的话,可以用xml进行配置,如果多的话,还是建议annotation;