一、创建spring项目
项目名称:spring101501
二、在项目中添加spring支持
1.在项目中创建lib目录
/lib
2.在lib目录下添加jar包
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging.jar
junit-4.10.jar
log4j.jar
mysql-connector-java-5.1.18-bin.jar
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
三、在项目中添加属性文件和配置文件
1.在项目中创建conf目录
/conf
2.在conf目录下添加属性文件
属性文件名称:jdbc.properties
属性文件内容:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/spring
jdbc.user = root
jdbc.password =root
3.在conf目录下添加核心配置文件
配置文件名称:applicationContext.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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.xsd">
</beans>
四、持久层设计
1.接口设计
在src目录下创建接口所在的包
包名:cn.jbit.spring101501.dao
在包创建接口
接口名:AccountDao.java
接口内容:
public interface AccountDao {
public void outMoney(int outAccount,double money);
public void inMoney(int inAccount,double money);
}
2.实现类设计
类名:AccountDaoImpl.java
类内容:
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void inMoney(int inAccount, double money) {
String sql = "UPDATE Account SET money = money + ? WHERE id = ?";
this.getJdbcTemplate().update(sql,money,inAccount);
}
@Override
public void outMoney(int outAccount, double money) {
String sql = "UPDATE Account SET money = money - ? WHERE id = ?";
this.getJdbcTemplate().update(sql,money,outAccount);
}
}
五、业务层设计
1.业务接口设计
包名:cn.jbit.spring101501.service
接口名:AccountService.java
接口内容:
public interface AccountService {
public void transfer(int outAccount,int inAccount,double money);
}
2.业务实现设计
实现类名:AccountServiceImpl.java
实现类内容:
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
@Override
@Transactional
public void transfer(int outAccount, int inAccount, double money) {
accountDao.outMoney(outAccount, money);
int a = 1/0;
accountDao.inMoney(inAccount, money);
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public AccountDao getAccountDao() {
return accountDao;
}
}
六、在核心配置文件中添加相关配置
<!-- 1.加载属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3.配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5.配置DAO -->
<bean id="accountDao" class="cn.jbit.spring101501.dao.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- 6.配置Service -->
<bean id="accountService" class="cn.jbit.spring101501.service.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 7.定义通知(Advice) -->
<tx:annotation-driven transaction-manager="transactionManager"/>
七、测试
1.在项目中创建test目录
/test
2.在test目录下添加测试包
包名:cn.jbit.spring101501.service
3.在测试包下创建测试类
类名:AccountServiceTest.java
类内容:
public class AccountServiceTest {
@Test
public void testTranser(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
accountService.transfer(1, 2, 200);
}
项目名称:spring101501
二、在项目中添加spring支持
1.在项目中创建lib目录
/lib
2.在lib目录下添加jar包
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging.jar
junit-4.10.jar
log4j.jar
mysql-connector-java-5.1.18-bin.jar
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
三、在项目中添加属性文件和配置文件
1.在项目中创建conf目录
/conf
2.在conf目录下添加属性文件
属性文件名称:jdbc.properties
属性文件内容:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/spring
jdbc.user = root
jdbc.password =root
3.在conf目录下添加核心配置文件
配置文件名称:applicationContext.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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.xsd">
</beans>
四、持久层设计
1.接口设计
在src目录下创建接口所在的包
包名:cn.jbit.spring101501.dao
在包创建接口
接口名:AccountDao.java
接口内容:
public interface AccountDao {
public void outMoney(int outAccount,double money);
public void inMoney(int inAccount,double money);
}
2.实现类设计
类名:AccountDaoImpl.java
类内容:
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void inMoney(int inAccount, double money) {
String sql = "UPDATE Account SET money = money + ? WHERE id = ?";
this.getJdbcTemplate().update(sql,money,inAccount);
}
@Override
public void outMoney(int outAccount, double money) {
String sql = "UPDATE Account SET money = money - ? WHERE id = ?";
this.getJdbcTemplate().update(sql,money,outAccount);
}
}
五、业务层设计
1.业务接口设计
包名:cn.jbit.spring101501.service
接口名:AccountService.java
接口内容:
public interface AccountService {
public void transfer(int outAccount,int inAccount,double money);
}
2.业务实现设计
实现类名:AccountServiceImpl.java
实现类内容:
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
@Override
@Transactional
public void transfer(int outAccount, int inAccount, double money) {
accountDao.outMoney(outAccount, money);
int a = 1/0;
accountDao.inMoney(inAccount, money);
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public AccountDao getAccountDao() {
return accountDao;
}
}
六、在核心配置文件中添加相关配置
<!-- 1.加载属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3.配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5.配置DAO -->
<bean id="accountDao" class="cn.jbit.spring101501.dao.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- 6.配置Service -->
<bean id="accountService" class="cn.jbit.spring101501.service.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 7.定义通知(Advice) -->
<tx:annotation-driven transaction-manager="transactionManager"/>
七、测试
1.在项目中创建test目录
/test
2.在test目录下添加测试包
包名:cn.jbit.spring101501.service
3.在测试包下创建测试类
类名:AccountServiceTest.java
类内容:
public class AccountServiceTest {
@Test
public void testTranser(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
accountService.transfer(1, 2, 200);
}
}
本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1564316