1.什么是事务
事务是数据库操作的最基本单元,逻辑上是一组操作,如果一个操作失败则是所有操作都失败,都成功才成功
2.事务的特征(ACID)
原子性,隔离性,一致性,持久性
3.搭建事务操作环境
1.创建数据库表,加两条记录
2.创建Service,搭建Dao,完成对象的创建和注入关系
载Service中注入Dao,Dao中注入JdbcTemplate,JdbcTemplate中注入JdbcTemplate
prop.jdbcDriverClassName=com.mysql.jdbc.Driver prop.url=jdbc:mysql://localhost:3306/bookdb?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC prop.username=root prop.password=123456
<?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"
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">
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.jdbcDriverClassName}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dateSource"></property>
</bean>
<context:component-scan base-package="Spring07"></context:component-scan>
</beans>
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao {
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
private JdbcTemplate jdbcTemplate;
}
import Spring07.Dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserDao userDao;
}
public class Account {
private int id;
private String name;
private int money;
public Account(int id, String name, int money) {
this.id = id;
this.name = name;
this.money = money;
}
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 getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public Account() {
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
3.载Dao中创建两个方法多钱和少钱,在Service中创建转账方法,并测试
public void UserMoney(){
//多钱
userDao.UserMoney();
//少钱
userDao.UserMon();
}
void UserMoney();
void UserMon();
@Override
public void UserMoney() {
String sql="UPDATE account SET money=money+? WHERE id=?";
jdbcTemplate.update(sql, 100,2);
}
@Override
public void UserMon() {
String sql="UPDATE account SET money=money-? WHERE id=?";
jdbcTemplate.update(sql, 100,1);
}
@Test
public void test1(){
ApplicationContext context=
new ClassPathXmlApplicationContext("bean7.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.UserMoney();
}
4.在