50,549
社区成员
发帖
与我相关
我的任务
分享
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
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-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="com.gg.spring.dao"></context:component-scan>
<!-- 获取资源文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 得到C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${passWord}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
</bean>
<!-- 得到 Spring 的 jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用事物注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
package com.gg.spring.dao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class DaoServiceImpl implements DaoService {
private ShopDao shopDao = new ShopDaoImpl();
@Transactional
public void shopDaoExecute(int userID, int isbn) {
int price = shopDao.getPrice(isbn);
shopDao.updateShop(isbn);
shopDao.updateUser(userID, price);
}
}
这儿调用
package com.gg.spring.dao;
public class TestDao {
private static DaoServiceImpl serviceDao = new DaoServiceImpl();
public static void main(String[] args) {
serviceDao.shopDaoExecute(1, 1001);
}
}