操作代码示例
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"
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:component-scan base-package="com.atguigu.spring5"/>
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${prop.url}"/>
<property name="username" value="${prop.username}"/>
<property name="password" value="${prop.password}"/>
<property name="driverClassName" value="${prop.driverClassName}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
Service
public class UserService {
@Autowired
private UserDAO userDAO;
public void accountMoney() {
try {
userDAO.reduceMoney();
int i = 10/0;
userDAO.addMoney();
} catch (Exception e) {
}
}
}
dao
@Repository
public class UserDAOImpl implements UserDAO{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void reduceMoney() {
String sql = "update t_account set money = money - ? where userName = ?";
jdbcTemplate.update(sql,100,"lucy");
}
@Override
public void addMoney() {
String sql = "update t_account set money = money + ? where userName = ?";
jdbcTemplate.update(sql,100,"mary");
}
}