Eclipse3.0 under Struts +spring+ Hibernate Quick Start (1)

Source: Internet
Author: User
Tags contains implement integer interface log version tostring access
Quick Start
This article is a primer for developing Web applications based on spring, using the Struts MVC framework, the middle tier using spring, and the background using hibernate.

This article contains the following elements:

• Configure Hibernate and transactions















• Load Spring's applicationcontext.xml file















• Establish dependencies between the business layer and the DAO















• Apply spring to Struts














Overview cable
This example is the establishment of a simple Web application called Myusers, complete user management operations, including simple database add, delete, check, that is crud (new, Access, update, delete) operations. This is a three-tier Web application that accesses the business layer through action (Struts), and the business layer accesses the DAO. Figure I briefly describes the overall structure of the application. The figures on the diagram illustrate the sequence of processes-from the web (useraction) to the middle tier (Usermanager), to the data Access layer (USERDAO), and then return the results.







The real strength of the spring layer is its declarative transactions, the support and persistence layer (e.g. hiberate and Ibatis)

Below is the steps to complete this example:

1. Install Eclipse plug-in

2. Database Build Table

3. Configuring Hibernate and Spring

4. Build the implementation class for the Hibernate DAO interface

5. Run the test class to test the DAO's CRUD operations

6. Create a processing class that declares a transaction

7. Create the action and model for the Web layer

8. Test class to run action test CRUD operations

9. Create a JSP file for CRUD operations through the browser

10. Validate JSP via Browser
Install Eclipse plug-in
1. Hibernate plugin Http://www.binamics.com/hibernatesync

2. Spring Plugin http://springframework.sourceforge.net/spring-ide/eclipse/updatesite/

3. MyEclipse plugin (cracked version)

4. Tomcat plug-in. Tanghan

5. Other plug-ins include xml,jsp,
Database CREATE table App_user (ID number not NULL primary,firstname Vchar (), LastName Vchar (32)); New project
Create a new Web project,



The new directory structure, as shown in the previous illustration, also contains the new Folder page for the JSP file, and the source folder test for the JUnit test file. The packages that are used at the same time, including struts,hibernate,spring, are imported into the Lib directory.
Create a persistent layer O/R mapping
1. Export App_user. hbm.xml file from the database with the Hibernate plugin under Src/com.jandar.model renamed to User.hbm.xml

<?xml version= "1.0"?>















<! DOCTYPE hibernate-mapping Public















"-//hibernate/hibernate Mapping dtd//en"















"Http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >






























<class name= "User" table= "App_user" >















<id















Column= "ID"















Name= "id"















Type= "Integer"















>















<generator class= "Assigned"/>















</id>















<property















Column= "LASTNAME"















Length= "10"















Name= "LastName"















Not-null= "false"















Type= "string"















/>















<property















Column= "FIRSTNAME"















Length= "10"















Name= "FirstName"















Not-null= "true"















Type= "string"















/>















</class>














































2. User.java files are generated by Hibernate Synchronizer->synchronizer file, and the user object corresponds to the App_user table in the database

Note: Automatically generated object files are not exactly the same under Eclipse, the same is that each object file must implement the Serializable interface, both 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;

}

}
To create a DAO to access an object
1. New Idao.java interface in Src/com.jandar.service.dao, all DAO inherits the interface

Package Com.jandar.services.dao;

Public interface Idao {

}

2. Create a 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 way to access an object,

3. Create a new Userdaohiberante.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 automatically generate method stubs

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);

}

















}

















}

The method of Iuserdao interface is implemented in this class, and the Hibernatedaosupport class is inherited. The role of this class is to access, manipulate objects through hibernate, and then implement the operation of the database.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.