struts2+hibernate3.2+spring2.0 Integration Chapter (II)

Source: Internet
Author: User

in the previous article, we have built the struts2+hibernate3.2+spring2.0 development environment successfully, this article we continue to explain the integration process of struts2+hibernate3.2+spring2.0.

Step two: Create a database, a data table, and build a table statement in the MySQL database:

Create Database Mytest;use mytest; CREATE TABLE users (  ID int (one) not NULL,  firstname varchar () is not NULL,  LastName varchar (+) NOT NULL,  a GE Int (one) not NULL,  PRIMARY KEY  (' id ')) engine=innodb DEFAULT Charset=utf8;
Here we need to explain the call relationship between the layers, the action layer-->service layer-->dao layer, the call relationship we configure in spring.

Step three: Establish Hibernate persistent layer access to the data table.

Establish User.java and User.hbm.xml files in the Com.tgb.bean package for mapping the data table users.

User.java

Package Com.tgb.bean;public class User {private Integer id;private string firstname;private string lastname;private int AG E;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String Getfirstname () {return firstname;} public void Setfirstname (String firstname) {this.firstname = FirstName;} Public String Getlastname () {return LastName;} public void Setlastname (String lastname) {this.lastname = LastName;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}
User.hbm.xml

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">Step four: Create an interface and implementation class that operates on the user object. 

To create an interface to manipulate the user object in Com.tgb.dao

Userdao.java

Package Com.tgb.dao;import Java.util.list;import Com.tgb.bean.user;public interface Userdao {public void Saveuser (User User);p ublic void removeuser (user user);p ublic user Finduserbyid (Integer ID);p ublic list<user> findallusers (); public void UpdateUser (user user);
Creating an interface implementation class for manipulating the user object in Com.tgb.dao.impl

Userdaoimpl.java

Package Com.tgb.dao.impl;import Java.util.list;import Org.springframework.orm.hibernate3.support.hibernatedaosupport;import Com.tgb.bean.user;import Com.tgb.dao.userdao;public class Userdaoimpl extends Hibernatedaosupport implements Userdao {@SuppressWarnings (" Unchecked ") public list<user> findallusers () {String hql =" From the user user order by user.id desc "; return (List<use r>) This.gethibernatetemplate (). Find (HQL);} Public user Finduserbyid (Integer ID) {User user = (user) this.gethibernatetemplate (). Get (User.class, id); return User;} public void Removeuser (user user) {this.gethibernatetemplate (). Delete (user); public void Saveuser (user user) {this.gethibernatetemplate (). Save (user); public void UpdateUser (user user) {this.gethibernatetemplate (). Update (user);}}
Step Five: Create the interface and implementation classes for the service layer

create an interface in Com.tgb.service that calls Userdao

Userservice.java

Package Com.tgb.service;import Java.util.list;import Com.tgb.bean.user;public interface UserService {public List< User> findAll ();p ublic void Save (User user);p ublic void Delete (user user);p ublic user Finduserbyid (Integer ID);p ublic void update (user user);
Creating an interface implementation class that calls Userdao in Com.tgb.service.impl

Userserviceimpl.java

Package Com.tgb.service.impl;import Java.util.list;import Com.tgb.bean.user;import com.tgb.dao.userdao;import Com.tgb.service.userservice;public class Userserviceimpl implements UserService {private Userdao Userdao;public Userdao Getuserdao () {return userdao;} public void Setuserdao (Userdao userdao) {This.userdao = Userdao;} public void Delete (user user) {this.userDao.removeUser (user);} Public list<user> FindAll () {return this.userDao.findAllUsers ();} Public User Finduserbyid (Integer ID) {return This.finduserbyid (ID);} public void Save (user user) {this.userDao.saveUser (user);} public void update (user user) {this.userDao.updateUser (user);}}
Step Six: Create a processing class for Savauser and listuser for the action layer.

Saveuseraction.java

Package Com.tgb.action.user;import Com.opensymphony.xwork2.actionsupport;import Com.tgb.bean.user;import Com.tgb.service.userservice;public class Saveuseraction extends Actionsupport {private User user;private UserService Userservice;public UserService Getuserservice () {return userservice;} public void Setuserservice (UserService userservice) {this.userservice = UserService;} Public User GetUser () {return user;} public void SetUser (user user) {this.user = user;} @Overridepublic String Execute () throws Exception {This.userService.save (this.user); return SUCCESS;}}
Listuseraction.java

Package Com.tgb.action.user;import Java.util.map;import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.actionsupport;import Com.tgb.service.userservice;public class ListUserAction extends Actionsupport {private UserService userservice;public userservice Getuserservice () {return userservice;} public void Setuserservice (UserService userservice) {this.userservice = UserService;} @Overridepublic String Execute () throws Exception {Map request = (map) actioncontext.getcontext (). Get ("request"); Request.put ("List", Userservice.findall ()); return SUCCESS;}}
Step Seven: Create a save.jsp page for saving the user and a list.jsp page to display the user.

save.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "iso-8859-1"%><%@ taglib prefix= "s" uri= "/ Struts-tags "%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";%&GT;&L t;! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > list.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><%@ taglib prefix= "s" uri= "/struts-tags" %><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";%&GT;&L t;! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > In this case, the interfaces and implementation classes between the layers have been created, and the next chapter will explain how to manage dependencies between classes through spring. 

struts2+hibernate3.2+spring2.0 Integration Chapter (II)

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.