Dao层:
studentDao类:
package dao;
public interface StudentDao {
void out(int id,int age);
void in(int id,int age);
}
StudentDaoImpl类:
package dao;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
public class StudentDaoImpl implements StudentDao{
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
}
@Override
public void out(int outid, int age) {
String sql = "update student set age=age-? where id=?";
jdbcTemplate.update("update student set age=age-'"+age+"' where id='"+outid+"'");
}
@Override
public void in(int inid, int age) {
jdbcTemplate.update("update student set age=age+'"+age+"' where id='"+inid+"'");
}
}
数据类:
package domain;
public class student {
private int id;
private String name;
private int age;
private int region_id;
@Override
public String toString() {
return "student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", region_id=" + region_id +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getRegion_id() {
return region_id;
}
public void setRegion_id(int region_id) {
this.region_id = region_id;
}
public student() {
}
public student(int id, String name, int age, int region_id) {
this.id = id;
this.name = name;
this.age = age;
this.region_id = region_id;
}
}
服务层:
StudentService接口:
package service;
public interface StudentService {
void transfer(int outid,int inid,int age);
}
StudentService接口实现类:
package service;
import dao.StudentDao;
public class StudentServiceImpl implements StudentService{
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao){
this.studentDao=studentDao;
}
@Override
public void transfer(int outid, int inid, int age) {
studentDao.out(outid,age);
studentDao.in(inid,age);
}
}
测试类:
package controller;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.StudentService;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class StudentController {
public static void main(String[] args) throws SQLException {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService bean = classPathXmlApplicationContext.getBean(StudentService.class); //通过bean标签的id名,获取数据
bean.transfer(4,5,10);
}
}
spring配置文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 加载外部的properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="studentDao" class="dao.StudentDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="studentService" class="service.StudentServiceImpl">
<property name="studentDao" ref="studentDao"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--通知,事务的增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 对不同的方法进行增强-->
<tx:attributes>
<!-- * 代表增强任何方法-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务的aop织入-->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
</beans>
jdbc.properties文件配置:
jdbc.password=123456 jdbc.username=root jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql:///gb1?characterEncoding=UTF-8 & serverTimezone=Asia/Shanghai jdbc.testConnectionOnCheckout=true
pom.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring_tx</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring_tx Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.8.M1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
</dependencies>
</project>