SSH实现简单增删改查的功能实例

// 4/21/2021更新

  1. 更新jdk版

  2. 解决无法找到sun.misc.BASE64Encoder的jar包的解决方法
    原因是因为jdk版本更新后,就不在支持。
    这篇文章写的非常清楚,大家可以看看。链接
    删除原本的包,导入新的包
    import org.apache.commons.codec.binary.Base64;
    导入这个包需要另外添加一个包,[下载链接] (https://blog.csdn.net/qq_40378795/article/details/109482864)
    jar下载

  3. 我更新一下,重新上传,下载链接

  4. 代码中有个cookie问题,需要修改。
    问题链接

Cookie cookie = new Cookie(USER_COOKIE, user.getFirstName() + "+" + user.getLastName());
  1. 有问题,可以沟通,欢迎交流。

#############################################
在网上看到一篇写的很不错的关于SSH 整合实现简单的增删改查功能的实例。
因为也是初次使用SSH框架,通过这样的实例更好的理解SSH框架。对原本的代码进行了修改和优化。如有不足之处,还请指教。
原文链接:http://blog.csdn.net/zhubinqiang/article/details/8498808

SSH实现简单增删改查的功能实例_第1张图片

与原实例更新之处和优化的地方。
**新增1.**实现hibernate自动创建数据表。
部分重要代码


	create

  
	
		
	
	
	
	
	

**新增2.**优化查找功能
查找功能在原先的代码中有的。但是返回的是user对象。我对它进行优化后,直接返回list,和查找全部对象的方法一样,当action返回SUCCESS时,/list.jsp

@Override
public List findUserById(int id) {
	// TODO Auto-generated method stub
	System.out.println("-------UserDaoImp.findUserById-----------"+id);
	String hql = "from User u where u.id=:id order by u.id asc";
	Query query=getSession().createQuery(hql);
	query.setInteger("id", id);
	List list =query.list();
	return list;
}

**优化3.**原先在运行过程中会报错。
报错提示:

严重: Could not create and/or set value back on to object
java.lang.IllegalArgumentException
	at org.springframework.asm.ClassReader.(Unknown Source)
	at org.springframework.asm.ClassReader.(Unknown Source)
	at org.springframework.asm.ClassReader.(Unknown Source)

这个原因是:

在表单提交的时候,action并没有创建表单对应的实例(只在action中声明:private User user)。
因为在写表单对应的类时,写了一个带有参数的构造方法,而在action中只是定义了这个类,没有实现 。

解决办法:
在action中,new出表单对应的类

private User user= new User();

优化4
hibernate3换成hibernate4
hibernate4中getHibernateTemplate()没有这个方法了。

private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

直接用getSession()和getHibernateTemplate()一样的效果。
解决了和spring3.0和hibernate4的结合。

主要优化就这些。

完整代码

aciton

package com.sqt.action.user;

import com.opensymphony.xwork2.ActionSupport;
import com.sqt.bean.User;
import com.sqt.service.UserService;

public class DeleteUserAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private User user=new 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 {
		System.out.println("-------UserAction.deleteUser-----------"+user.getFirstName());
		this.userService.delete(user);
		return SUCCESS;
	}
}
package com.sqt.action.user;

import java.util.Map;
import com.sqt.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ListUserAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

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

package com.sqt.action.user;

import com.sqt.bean.User;
import com.sqt.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class SaveUserAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	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相关的方法,完成实际的业务处理
		System.out.println("-------UserAction.SaveUser-----------"+user.getFirstName());
		this.userService.doCreateUser(user);
		return SUCCESS ;
	}
}
package com.sqt.action.user;

import com.sqt.bean.User;
import com.sqt.service.UserService;

import java.util.Map;

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

public class UpdatePUser extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private User user=new 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 {
		System.out.println("-------UserAction.UpdatePUser-----------"+user.getId());
		Map request = (Map) ActionContext.getContext().get("request");
		
		request.put("list", this.userService.findUserById(user.getId()));
		return SUCCESS;
	}
}
package com.sqt.action.user;


import com.opensymphony.xwork2.ActionSupport;
import com.sqt.bean.User;
import com.sqt.service.UserService;

public class UpdateUserAction extends ActionSupport {
	private User user=new 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 {
		System.out.println("-------UserAction.UpdateUser-----------"+user.getFirstName());
		this.userService.update(this.user);
		return SUCCESS;
	}
}

bean

package com.sqt.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;  
    }  
}  



	  
	
		
			
		
		
		
		
		
		
	

dao

package com.sqt.dao;

import java.util.List;

import com.sqt.bean.User;

public interface UserDAO {  
    public void doCreateUser(User user);  
    public List findAllUsers();  
    public void removeUser(User user);  
    public void updateUser(User user);  
    public List findUserById(int id);
}  
package com.sqt.dao.impl;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.sqt.bean.User;
import com.sqt.dao.UserDAO;


public class UserDAOImpl  implements UserDAO {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void doCreateUser(User user) {
		// TODO Auto-generated method stub
		System.out.println("-------UserDaoImp.doCreateUser-----------"+user.getFirstName());
		System.out.println("id"+user.getId());
		System.out.println("firstname"+user.getFirstName());
		System.out.println("lastname"+user.getLastName());
		System.out.println("age"+user.getAge());
		getSession().save(user);
	}

	@Override
	public List findAllUsers() {
		// TODO Auto-generated method stub
		String hql = "from User u order by u.id asc";
		Query query=getSession().createQuery(hql);
		List list =query.list();
		return list;
	}

	@Override
	public void removeUser(User user) {
		// TODO Auto-generated method stub
		System.out.println("-------UserDaoImp.removeUser-----------"+user.getFirstName());
		getSession().delete(user);
	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		System.out.println("-------UserDaoImp.updateUser-----------"+user.getFirstName());
		getSession().update(user);
	}

	@Override
	public List findUserById(int id) {
		// TODO Auto-generated method stub
		System.out.println("-------UserDaoImp.findUserById-----------"+id);
		String hql = "from User u where u.id=:id order by u.id asc";
		Query query=getSession().createQuery(hql);
		query.setInteger("id", id);
		List list =query.list();
		return list;
	}
}

service

package com.sqt.service;

import java.util.List;
import com.sqt.bean.User;

public interface UserService {
	public void doCreateUser(User user);
	public List findAllUsers();
	public void delete(User user);
	public void update(User user);
	public List findUserById(int id);
}
package com.sqt.service.impl;

import java.util.List;

import com.sqt.bean.User;
import com.sqt.dao.UserDAO;
import com.sqt.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) {
		System.out.println("-------UserService.doCreateUser-----------"+user.getFirstName());
		this.dao.doCreateUser(user);
	}

	public List findAllUsers() {
		System.out.println("-------UserService.findAllUsers-----------");
		return this.dao.findAllUsers();
	}

	public void delete(User user) {
		System.out.println("-------UserService.deleteUser-----------"+user.getFirstName());
		this.dao.removeUser(user);
	}

	public void update(User user) {
		System.out.println("-------UserService.updateUser-----------"+user.getFirstName());
		this.dao.updateUser(user);
	}

	public List findUserById(int id) {
		System.out.println("-------UserService.findUserById-----------"+id);
		return this.dao.findUserById(id);
	}

}

spring和hibernate的配置文件
applicationContext.xml




	
	
	
	
	
	
		
			org.gjt.mm.mysql.Driver
		
		
		
			jdbc:mysql://127.0.0.1:3306/db_hibernate
		
		
		
			root
		
		
		
			123456
		
		
	

	
		
			
		
		
		
			
				com/sqt/bean/User.hbm.xml					
			
		
		
		
			
				
					org.hibernate.dialect.MySQLDialect
				
				
				
					true
				
				
				
					create
				
			
		
		
	
	
	
		
			
		
	
	
	
		
			
		
	
	
	
		
	
	
	
		
			
		
		
		
			
		
		
		
			
				PROPAGATION_REQUIRED,readOnly
				PROPAGATION_REQUIRED
			
		
	
	
	
		
	
	
	
		
	
	
	
				
	
	
	
		
	
	
	
		
	
	
	
		
	

struts.xml 配置文件





	
		
			listUser.action
		
		
		
			/list.jsp
		
		
		
			listUser.action
		
		
		
			/update.jsp
		
		
		
			listUser.action
		
		
		
			/list.jsp
		
	

web.xml



  
    index.jsp
  
  
  
  	struts2
  	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
  
  
  	struts2
  	/*
  
  
  
  	org.springframework.web.context.ContextLoaderListener
  
  

jsp页面
index.jsp

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



  
    My JSP 'index.jsp' starting page
  
  
	

Operation List

Save User
List Users

list.jsp

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



    
    My JSP 'list.jsp' starting page
	
  
  
  
    

Users List

返回
序号 年龄 删除 更新
Delete Update

save.jsp

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



  
    
    My JSP 'index.jsp' starting page

  
  
  
	

Save User

update.jsp

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



  
  
    My JSP 'update.jsp' starting page

  
  
  
	

Update User

除了这个案例,另外再提供一个完成的案例。
下载地址:
http://download.csdn.net/detail/shaoqianting/9875818

实现列表、增加、删除、修改和登录等功能。

采用环境:Apache Tomcat v7.0,java SE8[1.8.0_20]

数据库db_ssh,数据表tb_Users
SSH实现简单增删改查的功能实例_第2张图片

你可能感兴趣的:(ssh2,java,ssh,实例,增删改查)