One, scenario: Write a user-managed service layer that includes adding users, deleting users, querying users, and so on, and user information is stored as a relational database.
Analysis:
This is a typical application service involving the business layer (Business Objects Layer), the Database access layer (DB access), and the storage layer (EIS).
Adopt A typical architecture: Bo+dao
The DAO is encapsulated using the jdbctemplate provided by spring.
Second, the concrete realization
1 User class Users
public class user{
Private String username;
Private String Email;
Private Date birthday;
Public User () {}
...
The corresponding Setter/getter method.
}
This is a simple bean class that records the user's information.
2 Database access class: Userdao
Public interface Userdao {
public void Save (user user);
public void Remove (String username);
Public User Searchbyname (String username);
}
This is the database access interface class, defines the user's database operations, the implementation of the class is USERDAOJDBC
Userdaojdbc class
public class Userdaojdbc extends Jdbcdaosupport implements Userdao {
Public Userdaojdbc () {
Super ();
}
public void Save (user user) {
Getjdbctemplate (). Update (
"INSERT into Sample_user values (?,?,?)",
New object[] {user.getusername (), User.getemail (),
User.getbirthday ()});
}
...
}
3) Business Class Usermanageservice
Public interface Usermanageservice {
public void Saveuser (user user);
public void Removeuser (String username);
Public User Getuserbyname (String username);
}
This is the user's business class interface, defined and user-related business operations, the implementation of the class Usermanageserviceimpl
Implement class Usermanageserviceimpl
public class Usermanageserviceimpl implements Usermanageservice {
Private Userdao Udao;
Public Usermanageserviceimpl () {
Super ();
}
public void Saveuser (user user) {
Udao.save (user);
}
Public Userdao Getudao () {
return Udao;
}
public void Setudao (Userdao Udao) {
This.udao = Udao;
}
}
4 Spring Bean Description file configuration
<beans>
<bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "Driverclassname" >
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name= "url" >
<value>jdbc:oracle:thin:@172.16.4.219:1521:orcl</value>
</property>
<property name= "username" >
<value>zhangbo</value>
</property>
<property name= "Password" >
<value>password</value>
</property>
</bean>
<bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" >
<ref bean= "DataSource"/>
</property>
</bean>
<bean id= "Userdao" class= "Sample.spring.dao.UserDaoJdbc" >
<property name= "DataSource" >
<ref bean= "DataSource"/>
</property>
</bean>
<bean id= "Usermanageservice" class= "Sample.spring.service.UserManageServiceImpl" >
<property name= "Udao" >
<ref bean= "Userdao"/>
</property>
</bean>
</beans>
5 table Sample_user for storing users in the data
Username VARCHAR2 () not NULL,
Email varchar2 NOT NULL,
Birthday Date
6 to verify the operation in Usermanageservice through unit tests:
public class Usermanageservicetest extends TestCase {
Private Usermanageservice service;
private static Log logger = Logfactory.getlog (Usermanageservicetest.class);
protected void SetUp () throws Exception {
Super.setup ();
Service = (Usermanageservice) springcontextutil
. Getbean ("Usermanageservice");
}
public void Testsaveandsearchandremoveuser () throws Exception {
User user = new user ();
User.setusername ("Zhangsan");
User.setemail ("zhangsan@test.com");
User.setbirthday (New Date ());
Service.saveuser (user);
Logger.debug ("Deposit A new User:" + user);
User Userfromdb = Service.getuserbyname ("Zhangsan");
Assertnotnull (Userfromdb);
Assertequals (Userfromdb.getemail (), "zhangsan@test.com");
Logger.debug ("From the database to find users:" + Userfromdb);
Service.removeuser ("Zhangsan");
Logger.debug ("Delete user from Database:" + Userfromdb);
}
protected void teardown () throws Exception {
Super.teardown ();
}
}