This paper is the development of web applications based on spring introductory article on the front end using Struts MVC framework, the middle layer spring, the background using Hibernate.
This article contains the following:
* Configure Hibernate and Services
* Spring's applicationContext.xml file loading
鈥?Establish business layer and DAO dependencies between
* Will be applied to the Struts in Spring
Introduction
This example is a simple web application, called MyUsers, complete user management, and contains a simple database to add, delete, check the the CRUD (new, access, update, delete) operations. This is a three-tier web applications, through the Action (Struts) visit to the business layer, business layer to access DAO. Figure 1 briefly illustrates the overall structure of the application. Chart shows the number of the order process - from the web (UserAction) to the middle layer (UserManager), to the data access layer (UserDAO), then the result is returned.
Spring layer is that it really powerful statement of type of business, to help set and the persistence layer support (such as Hiberate and iBATIS)
The following next steps to complete this example:
1. To install Eclipse plug-ins
2. Database build table
3. Configuring Hibernate and Spring
4. Establish Hibernate DAO implementation class interfaces
5. Run the test class, test DAO's CRUD operations
6. To create a processing class, a statement Affairs
7. To create web layer of Action and the model
8. Run the test class Test Action CRUD operations
9. To create jsp files via browser CRUD operations
10. Jsp browser check
Installing eclipse plugin
1. Hibernate plug-in http://www.binamics.com/hibernatesync
2. Spring plug-in http://springframework.sourceforge.net/spring-ide/eclipse/updatesite/
3. MyEclipse plugin (hack)
4. Tomcat plug-in. Tanghan
5. Other plug-ins, including xml, jsp,
Database construction table
create table app_user (id number not null primary, firstname vchar (32), lastname vchar (32));
New Project
Create a new web project, the new directory structure after the new folder also contains a page for the put jsp files, and put the source folder test for junit test file. The package also will be used, including struts, hibernate, spring are imported into the lib directory.
Create a persistence layer O / R mapping
1. In the src / com.jandar.model under the export from the database using hibernate plugin app_user's. Hbm.xml file renamed User.hbm.xml
"- / / Hibernate / Hibernate Mapping DTD / / EN"
"Http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
column = "ID"
name = "id"
type = "integer"
>
column = "LASTNAME"
length = "10"
name = "lastname"
not-null = "false"
type = "string"
/>
column = "FIRSTNAME"
length = "10"
name = "firstname"
not-null = "true"
type = "string"
/>
2. By hibernate synchronizer-> synchronizer file generated User.java file, User object corresponding to the database app_user table
Note: In eclipse automatically generated object files are not identical, the same file every object must implement Serializable interface, they must toString and hashCode methods;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
public class BaseObject implements Serializable (
public String toString () (
return ToStringBuilder.reflectionToString (this,
ToStringStyle.MULTI_LINE_STYLE);
)
public boolean equals (Object o) (
return EqualsBuilder.reflectionEquals (this, o);
)
public int hashCode () (
return HashCodeBuilder.reflectionHashCode (this);
)
)
public class User extends BaseObject (
private Long id;
private String firstName;
private String lastName;
/ **
* @ Return Returns the id.
* /
public Long getId () (
return id;
)
/ **
* @ Param id The id to set.
* /
public void setId (Long id) (
this.id = id;
)
/ **
* @ Return Returns the firstName.
* /
public String getFirstName () (
return firstName;
)
/ **
* @ Param firstName The firstName to set.
* /
public void setFirstName (String firstName) (
this.firstName = firstName;
)
/ **
* @ Return Returns the lastName.
* /
public String getLastName () (
return lastName;
)
/ **
* @ Param lastName The lastName to set.
* /
public void setLastName (String lastName) (
this.lastName = lastName;
)
)
Creating DAO to access an object
1. In the src / com.jandar.service.dao New IDAO.java interface, all of the DAO inherit the interface
package com.jandar.services.dao;
public interface IDAO (
)
2. In src / com.jandar.service.dao interfaces under the new IUserDAO.java
public interface IUserDAO extends DAO (
List getUsers ();
User getUser (Integer userid);
void saveUser (User user);
void removeUser (Integer id);
)
This interface provides access to object methods
3. In src / com.jandar.service.dao.hibernate under the new UserDAOHiberante.java
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import com.jandar.model.User;
import com.jandar.service.dao.IUserDAO;
public class UserDaoHibernate extends HibernateDaoSupport implements IUserDAO (
private Log log = LogFactory.getLog (UserDaoHibernate.class);
/ * (Non-Javadoc)
* @ See com.jandar.dao.IUserDAO # getUsers ()
* /
public List getUsers () (
return getHibernateTemplate (). find ("from User");
)
/ * (Non-Javadoc)
* @ See com.jandar.dao.IUserDAO # getUser (java.lang.Long)
* /
public User getUser (Integer id) (
/ / TODO Auto generated method stub
return (User) getHibernateTemplate (). get (User.class, id);
)
/ * (Non-Javadoc)
* @ See com.jandar.dao.IUserDAO # saveUser (com.jandar.model.User)
* /
public void saveUser (User user) (
log.debug ("xxxxxxx");
System.out.println ("yyyy");
getHibernateTemplate (). saveOrUpdate (user);
if (log.isDebugEnabled ())
(
log.debug ("userId set to" + user.getId ());
)
)
/ * (Non-Javadoc)
* @ See com.jandar.dao.IUserDAO # removeUser (java.lang.Long)
* /
public void removeUser (Integer id) (
Object user = getHibernateTemplate (). Load (User.class, id);
getHibernateTemplate (). delete (user);
if (log.isDebugEnabled ()) (
log.debug ("del user" + id);
)
)
)
In this class implements IUserDAO interface methods, and inherited HibernateDAOSupport class. The role of this class by hibernate to access, operate objects, so as to realize operation of the database.
相关链接:
Dell Amended Financial Statements Over The Past Four Years, Net Profit Is Expected To Cut 150 Millio
Family Background To Influence The Fourth Factors Of Female Students
3G2 to MOV
Striving to be China's "Shipbuilding City", on the shipbuilding industry along the Yangtze River
All roads lead to full high-definition video equipment HD Thunder
15 dollars a simplified version of windows 7 what
comparison E-Mail Tools
Multinational Corporations, Where The Moral Bottom Line
XviD To MP4
Unicom's first response to iPhone parallel: Black-earned money through tax evasion
Switch to a three-year-old can not afford to skip how should I do?
H.264 To AVI
For You Graphic
Delphi study: to find any string in the stream
CUSTOMER is not his wife is a lover of God is