标签:ssh
在上篇文章中,我们已经把struts2+hibernate3.2+spring2.0的开发环境搭建成功,这篇我们接着上篇,继续讲解struts2+hibernate3.2+spring2.0的整合过程。
步骤二:在MySQL数据库中创建数据库、数据表,建表语句:
create database mytest; use mytest; CREATE TABLE users ( id int(11) NOT NULL, firstname varchar(50) NOT NULL, lastname varchar(50) NOT NULL, age int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;在此我们需要说明各层之间的调用关系,action层-->service层-->dao层,调用关系我们在spring中配置。
步骤三:建立hibernate对数据表的持久层访问。
在com.tgb.bean包中建立User.java和User.hbm.xml文件,用于建立和数据表users的映射。
User.java
package com.tgb.bean; public class User { private Integer id; private String firstname; private String lastname; private int age; 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 age) { 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"> <hibernate-mapping> <class name="com.tgb.bean.User" table="users"> <id name="id" type="java.lang.Integer" column="id"> <generator class="increment"></generator> </id> <property name="firstname" type="string" column="firstname" length="50"></property> <property name="lastname" type="string" column="lastname" length="50"></property> <property name="age" type="java.lang.Integer" column="age"></property> </class> </hibernate-mapping>步骤四:创建操作User对象的接口和实现类。
在com.tgb.dao中创建操作User对象的接口
UserDAO.java
package com.tgb.dao; import java.util.List; import com.tgb.bean.User; public interface UserDAO { public void saveUser(User user); public void removeUser(User user); public User findUserById(Integer id); public List<User> findAllUsers(); public void updateUser(User user); }在com.tgb.dao.impl中创建操作User对象的接口实现类
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 User user order by user.id desc"; return (List<User>) 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); } }步骤五:创建service层的接口和实现类
在com.tgb.service中创建调用UserDAO的接口
UserService.java
package com.tgb.service; import java.util.List; import com.tgb.bean.User; public interface UserService { public List<User> findAll(); public void save(User user); public void delete(User user); public User findUserById(Integer id); public void update(User user); }在com.tgb.service.impl中创建调用UserDAO的接口实现类
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); } }步骤六:创建action层的对于SavaUser和ListUser的处理类。
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; } @Override public 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; } @Override public String execute() throws Exception { Map request = (Map) ActionContext.getContext().get("request"); request.put("list", userService.findAll()); return SUCCESS; } }步骤七:创建用于保存用户的save.jsp页面和显示用户的list.jsp页面。
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 + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Save User</title> </head> <body> <h1> <font color="red">Operation List</font> </h1> <s:form action="saveUser"> <s:textfield name="user.firstname" label="%{getText('firstname')}"></s:textfield> <s:textfield name="user.lastname" label="%{getText('lastname')}"></s:textfield> <s:textfield name="user.age" label="age"></s:textfield> <s:submit></s:submit> </s:form> </body> </html>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 + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>List User</title> </head> <body> <h1> <font color="red"><center> Users List </center> </font> </h1> <table> <tr> <td>序号</td> <td>姓</td> <td>名</td> <td>年龄</td> <td>删除</td> <td>更新</td> </tr> <s:iterator value="#request.list" id="us"> <tr> <td> <s:property value="#us.id" /> </td> <td> <s:property value="#us.firstname" /> </td> <td> <s:property value="#us.lastname" /> </td> <td> <s:property value="#us.age" /> </td> <td> <s:a href="deleteUser.action?user.id=%{#us.id}">delete</s:a> </td> <td> <s:a href="updatePUser.action?user.id=%{#us.id}">update</s:a> </td> </tr> </s:iterator> </table> </body> </html>到此,各层之间的接口和实现类已经创建完毕,下篇我们将讲解如何通过spring管理类之间的依赖关系。
struts2+hibernate3.2+spring2.0整合篇(二)
标签:ssh
原文地址:http://blog.csdn.net/quwenzhe/article/details/41624523