SSH整合 简单的增删改查

本文转载自 点击打开链接

Database 使用 Mysql ,

建立User 表

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `firstName` varchar(20) DEFAULT NULL,
  `lastName` varchar(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) 

所需jar包

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
	<classpathentry kind="con" path="melibrary.com.genuitec.eclipse.j2eedt.core.MYECLIPSE_JAVAEE_5_CONTAINER"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts2-spring-plugin-2.3.4.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/antlr-2.7.6.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/cglib-2.2.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-collections-3.1.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/dom4j-1.6.1.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ehcache-1.2.3.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ejb3-persistence.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate-annotations.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate-commons-annotations.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate-entitymanager.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate-validator.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate3.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/javassist-3.9.0.GA.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jta-1.1.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/log4j-1.2.14.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-api-1.5.8.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-log4j12-1.5.8.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.net.sf.cglib-2.2.0.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.aopalliance-1.0.0.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.apache.commons.fileupload-1.2.0.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.apache.commons.httpclient-3.1.0.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.apache.commons.logging-1.1.1.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.apache.log4j-1.2.15.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.codehaus.jackson.mapper-1.0.0.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.aop-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.asm-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.aspects-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.beans-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.context-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.core-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.expression-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.instrument-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.instrument.tomcat-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.jdbc-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.orm-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.oxm-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.transaction-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.web-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.web.portlet-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.web.servlet-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.web.struts-3.0.1.RELEASE-A.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/persistence.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-3.3.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-commons-3.3.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-tree-3.3.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-fileupload-1.2.2.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-io-2.0.1.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-lang3-3.1.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/freemarker-2.3.19.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/javassist-3.11.0.GA.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ognl-3.0.5.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts2-core-2.3.4.jar" sourcepath="F:/Java/SSH Source/struts-2.3.4/src/core/src/main"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xwork-core-2.3.4.jar"/>
	<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java-5.1.7-bin.jar"/>
	<classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
</classpath>

package org.zbq.bean;

public class User {
	private int id;
	private String firstName;
	private String lastName;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int 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;
	}
	@Override
	public String toString() {
		return "ID: " + id + "FirstName: " + firstName;
	}
}

User.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="org.zbq.bean.User" table="User">
		<id name="id" type="int">
			<generator class="increment"></generator>
		</id>
		
		<property name="firstName" column="firstName" type="string"></property>
		<property name="lastName" column="lastName" type="string"></property>
		<property name="age" column="age" type="int"></property>
		
	</class>
</hibernate-mapping>
UserDAO.java (Interface)

package org.zbq.dao;

import java.util.List;

import org.zbq.bean.User;

public interface UserDAO {
	public void doCreateUser(User user);
	public List<User> findAllUsers();
	public void removeUser(User user);
	public void updateUser(User user);
	public User findUserById(int id);
}

UserDAOImpl.java

package org.zbq.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.zbq.bean.User;
import org.zbq.dao.UserDAO;

public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {

	public void doCreateUser(User user) {
		this.getHibernateTemplate().save(user);
	}

	@SuppressWarnings("unchecked")
	public List<User> findAllUsers() {
		String hql = "from User u order by u.id asc";
		List<User> list = this.getHibernateTemplate().find(hql);
		return list;
	}

	public void removeUser(User user) {
		this.getHibernateTemplate().delete(user);
	}

	public void updateUser(User user) {
		this.getHibernateTemplate().update(user);
	}

	public User findUserById(int id) {
		User user = null;
		user = (User) this.getHibernateTemplate().get(User.class, id);
		return user;
	}
	
}

UserService.java (Interface)

package org.zbq.service;

import java.util.List;

import org.zbq.bean.User;

public interface UserService {
	public void doCreateUser(User user);
	public List<User> findAllUsers();
	public void delete(User user);
	public void update(User user);
	public User findUserById(int id);
}

UserServiceImpl

package org.zbq.service.impl;

import java.util.List;

import org.zbq.bean.User;
import org.zbq.dao.UserDAO;
import org.zbq.service.UserService;

public class UserServiceImpl implements UserService {
	private UserDAO dao;
	
	public UserDAO getDao() {
		return dao;
	}

	public void setDao(UserDAO dao) {
		this.dao = dao;
	}

	public void doCreateUser(User user) {
		this.dao.doCreateUser(user);
	}

	public List<User> findAllUsers() {
		return this.dao.findAllUsers();
	}

	public void delete(User user) {
		this.dao.removeUser(user);
	}

	public void update(User user) {
		this.dao.updateUser(user);
	}

	public User findUserById(int id) {
		return this.dao.findUserById(id);
	}

}

关于User的Action

SaveUserAction.java

package org.zbq.action.user;

import org.zbq.bean.User;
import org.zbq.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

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 {
//		调用service相关的方法,完成实际的业务处理
		this.userService.doCreateUser(user);
		return SUCCESS ;
	}
}

ListUserAction.java

package org.zbq.action.user;

import java.util.Map;

import org.zbq.service.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ListUserAction extends ActionSupport {
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		
		Map request = (Map) ActionContext.getContext().get("request");
		
		request.put("list", this.userService.findAllUsers());
		return SUCCESS;
	}
}


DeleteUserAction.java

package org.zbq.action.user;

import org.zbq.bean.User;
import org.zbq.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class DeleteUserAction extends ActionSupport {
	private User user;
	private UserService userService;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public String execute() throws Exception {
		this.userService.delete(user);
		return SUCCESS;
	}
}

UpdatePUser.java

package org.zbq.action.user;

import org.zbq.bean.User;
import org.zbq.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class UpdatePUser extends ActionSupport {
	private User user;
	private UserService userService;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public String execute() throws Exception {
		this.user = this.userService.findUserById(user.getId());
		
		return SUCCESS;
	}
}

UpdateUserAction.java

package org.zbq.action.user;

import org.zbq.bean.User;
import org.zbq.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class UpdateUserAction extends ActionSupport {
	private User user;
	private UserService userService;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@Override
	public String execute() throws Exception {
		this.userService.update(this.user);
		return SUCCESS;
	}
}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<package name="ssh2" extends="struts-default">
		<action name="saveUser" class="saveUserAction">
			<result name="success" type="redirect">listUser.action</result>
		</action>
		
		<action name="listUser" class="listUserAction">
			<result name="success">/list.jsp</result>
		</action>
		
		<action name="deleteUser" class="removeUserAction">
			<result name="success" type="redirect">listUser.action</result>
		</action>
		
		<action name="updatePUser" class="updatePUserAction">
			<result name="success">/update.jsp</result>
		</action>
		
		<action name="updateUser" class="updateUserAction">
			<result name="success" type="redirect">listUser.action</result>
		</action>
	</package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</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>
  
</web-app>

applictionContext.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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	<!-- 想到与Hibernate的hibernate.cfg.xml -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>org.gjt.mm.mysql.Driver</value>
		</property>
		
		<property name="url">
			<value>jdbc:mysql://127.0.0.1:3306/ssh</value>
		</property>
		
		<property name="username">
			<value></value>
		</property>
		
		<property name="password">
			<value></value>
		</property>
		
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dataSource"/>
		</property>
		
		<property name="mappingResources">
			<list>
				<value>org/zbq/bean/User.hbm.xml</value>					
			</list>
		</property>
		
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				
				<prop key="hibernate.show_sql">
					true
				</prop>
			</props>
		</property>
		
	</bean>
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory"/>
		</property>
	</bean>
	
	<bean id="userDAO" class="org.zbq.dao.impl.UserDAOImpl" scope="singleton">
		<property name="sessionFactory">
			<ref local="sessionFactory"/>
		</property>
	</bean>
	
	<bean id="userServiceTarget" class="org.zbq.service.impl.UserServiceImpl" scope="singleton">
		<property name="dao" ref="userDAO" />
	</bean>
	
	<bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="target">
			<ref local="userServiceTarget"/>
		</property>
		
		<property name="transactionManager">
			<ref local="transactionManager"/>
		</property>
		
		<property name="transactionAttributes">
			<props>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	
	<bean id="saveUserAction" class="org.zbq.action.user.SaveUserAction" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>
	
	<bean id="listUserAction" class="org.zbq.action.user.ListUserAction" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>
	
	<bean id="removeUserAction" class="org.zbq.action.user.DeleteUserAction" scope="prototype">
		<property name="userService" ref="userService"></property>		
	</bean>
	
	<bean id="updatePUserAction" class="org.zbq.action.user.UpdatePUser">
		<property name="userService" ref="userService"></property>
	</bean>
	
	<bean id="updateUserAction" class="org.zbq.action.user.UpdateUserAction" scope="prototype">
		<property name="userService" ref="userService"></property>
	</bean>
</beans>


1358081789_2065.png

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'index.jsp' starting page</title>

  </head>
  
  <body>
	<h1><font color="red">Operation List</font></h1>
	<s:a href="save.jsp">Save User</s:a><br/>
	<s:a href="listUser.action">List Users</s:a>
	
	
  </body>
</html>

list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'list.jsp' starting page</title>
	<script type="text/javascript">
		function del(){
			if(confirm("Are you sure?")){
				return true;
			}
			return false;
		}
	</script>
  </head>
  
  <body>
    <h1><font color="red">Users List</font></h1>
    <s:a href="/ssh2/index.jsp"><<--</s:a>
    <table border="1" width="80%" align="center">
    	<tr>
    		<th>序号</th>
    		<th>姓</th>
    		<th>名</th>
    		<th>年龄</th>
    		<th>删除</th>
    		<th>更新</th>
    	</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 align="center">
					<s:a href="deleteUser.action?user.id=%{#us.id}" οnclick="return del()">Delete</s:a>
    			</td>
    			<td align="center">
					<s:a href="updatePUser.action?user.id=%{#us.id}">Update</s:a>		
    			</td>
    		</tr>
    	</s:iterator>
    </table>
    
  </body>
</html>

update.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'update.jsp' starting page</title>

  </head>
  
  <body>
	<h1><font color="red">Update User</font></h1>
	
	<s:form action="updateUser">
	<table>
		<tr>
			<td>
				<s:hidden name="user.id" value="%{user.id}"></s:hidden>
			</td>
		</tr>
	
		<tr>
			<td>
				<s:textfield name="user.firstName" value="%{user.firstName}" label="First Name"></s:textfield>
			</td>
		</tr>
		
		<tr>
			<td>
				<s:textfield name="user.lastName" value="%{user.lastName}" label="Last Name"></s:textfield>
			</td>
		</tr>
		
		<tr>
			<td>
				<s:textfield name="user.age" value="%{user.age}" label="Age"></s:textfield>
			</td>
		</tr>
		
		<tr>
			<td>
				<s:submit></s:submit>
			</td>
		</tr>
	</table>	
	</s:form>
	
  </body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值