Eclipse3.0 Struts + Spring + Hibernate Quick Start (1)

xiaoxiao2021-03-05  20

This article is a introductory article developed based on Spring-based web applications. The front end uses the Struts MVC framework.

This article contains the following:

· Configuring Hibernate and transactions

· Load Spring ApplicationContext.xml file

· Establish dependencies between business stratum and DAO

· Apply Spring to Struts

Overlay

This example is a simple web application called MyUsers, completes user management operations, including simple database increasing, deleting, 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 its declarative transaction, gain, and support for persistence layer (such as Hiberate and Ibatis)

The steps to complete this example is:

1. Install Eclipse plugin

2. Database built table

3. Configuring Hibernate and Spring

4. Establish an implementation class for Hibernate DAO interface

5. Run the test class, test the DAO's crud operation

6. Create a processing class, declarative transaction

7. Create a web layer Action and Model

8. Test class test CRUD operation running Action

9. Create a JSP file for Crud operations via the browser

10. Check JSP via browser

Install Eclipse plugin

1. Hibernate plugin http://www.binamics.com/hibernateSync

2. Spring plugin http://springframework.sourceforge.net/spring-ide/eclipse/UpdateSite/

3. MyECLIPSE plugin (crack version)

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

Create a Web Project,

The newly built directory structure is shown above, and the new folder Page is included 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

"- // 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. Generate user.java files via Hibernate Synchronizer-> Synchronizer File, the User object corresponds to the App_User table in the database

Note: The automatically generated object files automatically under Eclipse is not identical, the same is that each object file must implement the serializable interface, and the TOSTRING and HashCode methods are required;

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 imports 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 dimstname;

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;

}

}

Create 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; integer userid;

Void Saveuser (User User);

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 ID) {

// TODO automatic generation 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.Indebugenabled ())

{

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.Indebugeload ()) {

Log.debug ("

DEL User " ID);

}

}

}

The method of 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-38393.html

New Post(0)