读书人

学习札记ssh2(struts2+hibernate3.2+s

发布时间: 2012-10-30 16:13:36 作者: rapoo

学习笔记ssh2(struts2+hibernate3.2+spring2.5)整合

<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

package cn.scau.ssh2.dao;import java.util.List;import cn.scau.ssh2.beans.User;public interface UserDao {public void saveUser(User user);public void removeUser(User user);public User findUserById(int id);public List<User> findAllUserser();public void updateUser(User user);}7.建立cn.scau.ssh2.dao.impl包包下有UserDaoImpl.java为具体的实现:package cn.scau.ssh2.dao.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import cn.scau.ssh2.beans.User;import cn.scau.ssh2.dao.UserDao;//继承HibernateDaoSupport,用到了getHibernateTemplate这个方法。public class UserDaoImpl extends HibernateDaoSupport implements UserDao {@SuppressWarnings("unchecked")public List<User> findAllUserser() {String hql = "from User user order by user.id desc";List<User> list = this.getHibernateTemplate().find(hql);return list;}public User findUserById(int 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);}}

8. 建立cn.scau.ssh2.service包

package cn.scau.ssh2.action;import java.util.Iterator;import java.util.Map;import java.util.Set;import cn.scau.ssh2.beans.User;import cn.scau.ssh2.service.UserService;import com.opensymphony.xwork2.ActionSupport;public class SaveUserAction extends ActionSupport {private static final long serialVersionUID = -4055521880257114396L;private User user;private UserService userService;//get方法可以不用写,因为要用到的只是set方法public UserService getUserService() {return userService;}public void setUserService(UserService userService) {this.userService = userService;}public void setUser(User user) {this.user = user;}public User getUser() {return user;}@Overridepublic String execute() throws Exception {this.userService.save(user);return SUCCESS;}}

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.custom.i18n.resources"value="globalMessages"></constant><constant name="struts.i18n.encoding" value="utf-8"></constant><package name="cn.scau.ssh2.action" extends="struts-default"><action name="saveUser" type="redirect">listUser.action</result><result name="input">/save.jsp</result></action><action name="listUser" type="redirect">listUser.action</result></action><action name="updatePUser" type="redirect">listUser.action</result><result name="input">/update.jsp</result></action></package></struts>14.applicationContext.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- destroy-method="close"将连接放回连接池--><bean id="dataSource"destroy-method="close"><property name="driverClassName"value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/ssh2"></property><property name="username" value="root"></property><property name="password" value="rick"></property><property name="maxActive" value="100"></property><property name="maxIdle" value="30"></property><property name="maxWait" value="500"></property><property name="defaultAutoCommit" value="true"></property></bean><bean id="sessionFactory"ref="dataSource"></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="show_sql">true</prop><!-- <prop key="hbm2ddl.auto">update</prop> --></props></property><property name="mappingResources"><list><value>cn/scau/ssh2/beans/User.hbm.xml</value></list></property></bean><bean id="userDao" /></property></bean><bean id="userService"ref="userDao"></property></bean><!-- scope="prototype"因为struts2中action是有状态的,所以要将scope设为"prototype",如果不设,可以试一下效果,当输入有误信息时会出现错误提示重复输出的情况--><bean id="saveUserAction" ref="userService"></property></bean><bean id="listUserAction" ref="userService"></property></bean><bean id="removeUserAction"scope="prototype"><property name="userService" ref="userService"></property></bean><bean id="updatePUserAction"scope="prototype"><property name="userService" ref="userService"></property></bean><bean id="updateUserAction"scope="prototype"><property name="userService" ref="userService"></property></bean></beans>

[/b]

读书人网 >软件架构设计

热点推荐