First, create a spring project
Project Name: spring101501
Ii. adding spring support to the project
1. Create a Lib directory in your project
/lib
2. Add the jar package in the Lib directory
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
Iii. adding a property file and a configuration file to a project
1. Create the Conf directory in your project
/conf
2. Add a property file under the Conf directory
Property file name: Jdbc.properties
Properties File Contents:
Jdbc.driver = Com.mysql.jdbc.Driver
Jdbc.url = jdbc:mysql://localhost:3306/spring
Jdbc.user = root
Jdbc.password =root
3. Add the core configuration file under the Conf directory
Configuration file name: Applicationcontext.xml
Configuration file Contents:
<?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>
Four, durable layer design
1. Interface design
Create the package containing the interface in the SRC directory
Package Name: Cn.jbit.spring101501.dao
Creating an interface in a package
Interface Name: Accountdao.java
Interface content:
Public interface Accountdao {
public void Outmoney (int. outaccount,double money);
public void Inmoney (int. inaccount,double money);
}
2. Implementing Class Design
Class Name: Accountdaoimpl.java
Class content:
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);
}
}
Five, business layer design
1. Business Interface Design
Package Name: Cn.jbit.spring101501.service
Interface Name: Accountservice.java
Interface content:
Public interface Accountservice {
public void Transfer (int. outaccount,int inaccount,double money);
}
2. Business Implementation Design
Implementation class Name: Accountserviceimpl.java
Implementing the class content:
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;
}
}
Vi. adding the relevant configuration in the core configuration file
<!--1. Load Properties File---
<context:property-placeholder location= "Classpath:jdbc.properties"/>
<!--2. Configure the database connection pool--
<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. Configure JdbcTemplate-
<bean id= "JdbcTemplate" class= "Org.springframework.jdbc.core.JdbcTemplate" >
<property name= "DataSource" ref= "DataSource" ></property>
</bean>
<!--4. Configure Transaction Manager-
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource" ></property>
</bean>
<!--5. Configuring DAO--
<bean id= "Accountdao" class= "Cn.jbit.spring101501.dao.AccountDaoImpl" >
<property name= "JdbcTemplate" ref= "JdbcTemplate" ></property>
</bean>
<!--6. Configure Service--
<bean id= "Accountservice" class= "Cn.jbit.spring101501.service.AccountServiceImpl" >
<property name= "Accountdao" ref= "Accountdao" ></property>
</bean>
<!--7. Define notifications (Advice)--
<tx:annotation-driven transaction-manager= "TransactionManager"/>
Seven, testing
1. Create the test catalog in the project
/test
2. Add a test package in the test directory
Package Name: Cn.jbit.spring101501.service
3. Create a test class under a test package
Class Name: Accountservicetest.java
Class content:
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);
}
}
This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1564316
spring-Configuring declarative Transactions with annotations