一步一教你搭建SSH框架,实现简单的增删改查功能
一、首先创建一个Java web项目,并按下图建好项目目录
二、在网上搜索ssh所需jar包,并将其放到WEB_INF下的lib里面(这里截图了部分)
三、如下图配置web.xml(添加spring监听器以及struts过滤器等)
ssh
index.jsp
OpenSessionInView
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
singleSession
true
OpenSessionInView
/*
setCharactor
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
setCharactor
/*
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.do
*.jsp
FORWARD
REQUEST
INCLUDE
contextConfigLocation
/WEB-INF/classes/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
四、在bo包建立User实体类及其Hibernate映射文件
User.java
package com.suo.bo;
/**
* User entity. @author MyEclipse Persistence Tools
*/
public class User implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private String password;
private String email;
private String address;
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
}
User类的Hibernate映射文件User.hbm.xml
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
Mapping file autogenerated by MyEclipse Persistence Tools
-->
五、Dao层编写 (该层用于数据的持久化操作)
UserDAO.java
package com.suo.dao;
import com.suo.bo.User;
public interface UserDAO {
/**
* 保存
* @param user
*/
void saveUser(User user);
/**
* 更新
* @param user
*/
void updateUser(User user);
/**
* 删除
*/
void deleteUser(User user);
/**
* 查询
* @return
*/
User queryUserbyName(String name);
}
UserDAO.Java
package com.suo.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.suo.bo.User;
import com.suo.dao.UserDAO;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
@Override
public void saveUser(User user) {
// TODO Auto-generated method stub
getHibernateTemplate().save(user);
}
@Override
public void updateUser(User user) {
// TODO Auto-generated method stub
getHibernateTemplate().update(user);
}
@Override
public void deleteUser(User user) {
// TODO Auto-generated method stub
getHibernateTemplate().delete(user);
}
@Override
public User queryUserbyName(String name) {
// TODO Auto-generated method stub
String queryString="from User t where t.name=?";
// return (User)getHibernateTemplate().find(queryString,new String[]{name}).get(0);
return (User) getHibernateTemplate().find(queryString, name).get(0);
}
}
六、Service层编写(该层用于数据的业务处理)
UserService.java
package com.suo.service;
import com.suo.bo.User;
public interface UserService {
void save(User user);
User queryUserbyName(String name);
void updateUser(User user);
void deleteUser(String name);
}
UserServiceImpl.java
package com.suo.service.impl;
import com.suo.bo.User;
import com.suo.dao.UserDAO;
import com.suo.service.UserService;
public class UserServiceImpl implements UserService{
private UserDAO userDAO;
@Override
public void save(User user) {
// TODO Auto-generated method stub
userDAO.saveUser(user);
}
@Override
public User queryUserbyName(String name) {
// TODO Auto-generated method stub
return userDAO.queryUserbyName(name);
}
@Override
public void updateUser(User user) {
// TODO Auto-generated method stub
User userdata=userDAO.queryUserbyName(user.getName());
userdata.setAddress(user.getAddress());
userdata.setEmail(user.getEmail());
userdata.setPassword(user.getPassword());
userDAO.updateUser(userdata);
}
@Override
public void deleteUser(String name) {
// TODO Auto-generated method stub
User user=userDAO.queryUserbyName(name);
userDAO.deleteUser(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
七、Action编写
package com.suo.action;
import com.opensymphony.xwork2.ActionSupport;
import com.suo.bo.User;
import com.suo.service.UserService;
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private UserService userService;
private User user;
private String name;
public String save(){
userService.save(user);
return "add_success";
}
public String queryUserbyName(){
user =userService.queryUserbyName(name);
return "query_success";
}
public String deleteUser(){
userService.deleteUser(name);
return "delete_success";
}
public String updateUser(){
String str=user.getName();
//更改前preUser
User preuUser=userService.queryUserbyName(str);
userService.updateUser(user);
//更改后user
User afteruser=userService.queryUserbyName(str);
return "update_success";
}
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
八、在hibernate.properties中配置数据相关数据,以及在applicationContext.xml中配置Spring管理
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
classpath:hibernate.properties
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
${mysql.database.driver_class}
${mysql.database.url}
${mysql.database.username}
${mysql.database.password}
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
org.hibernate.dialect.MySQL5Dialect
classpath:com/suo/bo
PROPAGATION_NOT_SUPPORTED
PROPAGATION_NOT_SUPPORTED
PROPAGATION_NOT_SUPPORTED
PROPAGATION_NOT_SUPPORTED
PROPAGATION_NOT_SUPPORTED
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED,-Exception
PROPAGATION_REQUIRED
九、Struts.xml文件的编写
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
/add_success.jsp
/delete_success.jsp
/update_success.jsp
/query_success.jsp
十、前端Jsp页面的编写
index.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+"/";
%>
My JSP 'index.jsp' starting page
-->
ssh框架整合学习demo
新增
名称:
密码:
邮箱:
住址:
查询
名称:
修改
名称:
密码:
邮箱:
住址:
删除
名称:
query_success.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+"/";
%>
My JSP 'query_success.jsp' starting page
-->
查询成功!
查询结果
名字 :
密码:
邮箱:
住址:
其他的前端jsp类似 就不在写了
源码下载地址:点击打开链接
十一、部署运行
网友评论