Struts + Spring + Hibernate Quick Start (reproduced)

xiaoxiao2021-03-05  23

Struts Spring Hibernate Quick Start

This article is a introductory article developed based on Spring-based web applications. The front end uses the Struts MVC framework. The intermediate layer uses Spring, and Hibernate is adopted in the background. This article contains the following: • Configuring Hibernate and Transaction · Load Spring ApplicationContext.xml File • Depending on Business Layer and DAO • Apply Spring Apply to Struts Introduction This example is a simple web application, called MyUsers, Complete user management operations, including simple database, delete, check, which is CRUD (new, access, update, delete) operation. This is a three-layer web application that accesses the business layer through Action (Struts), and the business layer accesses DAO. A brief description of the overall structure of the application. The number on the figure illustrates the process sequence - from the web (useerAction) to the intermediate layer (UserManager), then goes to the Data Access Layer (UserDao) and then returns the result. The true power of the Spring layer is that its declarative transaction processing, help, and persistent layer support (such as hiberate and ibatis) below the steps of completing this example: 1. Install the Eclipse plugin 2. Database Built table 3. Configure Hibernate and Spring 4. Establish an implementation class 5 for the Hibernate DAO interface. Run the test class, test the DAO's Crud operation 6. Create a processing class, declare transaction 7. Create an Action and Model 8 of the Web layer. Test Class Test CRUD Operation 9 running an action. Create a JSP file for Crud operation 10 through the browser. Install the Eclipse plugin 1 via the browser checking JSP 1. Hibernate plugin http://www.binamics.com/hibernateSync 2. Spring plugin http://springframework.sourceforge.net/spring-ide/eclipse/UpdateSite/ 3. MyECLIPSE Plugin (Crack Edition) 4. Tomcat plugin. TANGHAN 5. Other plugins include XML, JSP, Database built table

Create Table App_User (ID Number Not Null Primary, FirstName Vchar (32), Lastname Vchar (32));

New project New Web Project, the newly created directory structure contains the new folder Page to place the JSP file, and the source folder TEST is used to place the JUnit test file. At the same time, the package, including struts, hibernate, and Spring into the lib directory. Create a persistence layer O / R mapping 1. Using the Hibernate plugin under src / com.jandar.model, use the Hibernate plugin from the database. Hbm.xml file is renamed User.hbm.xml

2. Generate user.java files via Hibernate Synchronizer-> Synchronizer file, the User object corresponds to the App_User speech in the database: the automatically generated object file in Eclipse is not exactly the same, the same is that each object file must implement the serializable interface, required Tostring and havehcode 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;} / ** * @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 (STRIN g firstName) {this.firstName = firstName;}. / ** * @return Returns the lastName * / public String getLastName () {return lastName;}. / ** * @param lastName The lastName to set * / public void setLastName Create a DAO Access Object 1 (String lastname) {this.lastname = LastName;}} Create a DAO Access Object 1. In SRC / com.jandar.service.dao, create new idao.java interface, all DAOs inherit the interface

Package com.jandar.services.dao; public interface idao {}

2. New iUserDao.java interface under src / com.jandar.service.dao

Public Interface iUserdao Extends Dao {list getUsers (); user getuser; void saveuse; void removeuser (Integer ID);

This interface provides a method of accessing an object, 3. New Userdaohibarante.java under src / com.jandar.service.dao.hibernate

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 User GetUser) Automatic Method Store Return (User) gethateTemplate (). 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.Indebugenabled ()) {log.debug ("userid set to" user.getId ());}} / * (Non-javadoc) * @see com.jandar.dao.iuserdao # Removeuser (java.lang.long) * / public void removeuser (Ibject user = gethibernateTemplate (); load (user.class, id); GethibernateTemplate (). delete (user); if (log.isDebugeload ()) {log.debug ("DEL User" ID);}} The iUserdao interface is implemented in this class and inherits the HibernatedAosupport class. This type of role is to access the database through Hibernate, and then implement the operation of the database.

转载请注明原文地址:https://www.9cbs.com/read-33211.html

New Post(0)