在网上看到一篇写的很不错的关于SSH 整合实现简单的增删改查功能的实例。
因为也是初次使用SSH框架,如有不足,请多指教。
先看一下我们完整的工程目录:
好了 我们废话不多说 直接上操作:
1.(1.)Dept 我们的Bean 包名:com.bdqn.entity(根据自己的习惯定义就可以)
package com.bdqn.entity;
import java.io.Serializable;
public class Dept implements Serializable {
//封装字段
private int deptno;
private String dname;
private String loc;
//无参构造函数
public Dept() {
// TODO Auto-generated constructor stub
}
//有参构造函数
public Dept(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
//封装GET SET方法
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
(2.)Dept.hbm.xml 配置映射文件:
2.IDeptDao 配置接口 包名:com.bdqn.dao(根据自己的习惯定义就可以)
package com.bdqn.dao;
import java.util.List;
import com.bdqn.entity.Dept;
/**
* 定义接口
* @author 萌萌里的小高冷
*
*/
public interface IDeptDao {
//查询
public List findAll();
//增加
public int save(Dept d);
//删除
public int delete(int id);
//修改
public int update(Dept d);
//根据ID查询
public Dept findById(int id);
}
3.DeptDaoImpl 配置实现类 包名:com.bdqn.dao.impl(根据自己的习惯定义就可以)
package com.bdqn.dao.impl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bdqn.dao.IDeptDao;
import com.bdqn.entity.Dept;
public class DeptDaoImpl
extends HibernateDaoSupport
implements IDeptDao {
public int delete(int id) {
// TODO Auto-generated method stub
try {
//获取对象的id
Dept d=this.getHibernateTemplate().get(Dept.class,id);
//执行删除方法 删除id
this.getHibernateTemplate().delete(d);
//删除成功 return 1;
return 1;
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
}
return 0;
}
public List findAll() {
// TODO Auto-generated method stub
//查询获取全部的数据
List list=(List) this.getHibernateTemplate().find("from Dept");
return list;
}
public Dept findById(int id) {
// TODO Auto-generated method stub
//查询出对象的id
Dept dd=this.getHibernateTemplate().get(Dept.class, id);
return dd;
}
public int save(Dept d) {
// TODO Auto-generated method stub
try {
//调用我们定义的接口 增加数据
this.getHibernateTemplate().save(d);
return 1;
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
}
return 0;
}
public int update(Dept d) {
// TODO Auto-generated method stub
try {
//调用我们定义的接口 增加数据
this.getHibernateTemplate().update(d);
return 1;
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
}
return 0;
}
}
4.IDeptService 配置我们的业务逻辑层 包名:com.bdqn.service(根据自己的习惯定义就可以)
package com.bdqn.service;
import java.util.List;
import com.bdqn.entity.Dept;
public interface IDeptService {
//跟我们的dao层一样
public List findAll();
public boolean save(Dept d);
public boolean delete(int id);
public boolean update(Dept d);
public Dept findById(int id);
}
5.DeptServiceImpl 包名:com.bdqn.service.Impl(根据自己的习惯定义就可以)
package com.bdqn.service.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bdqn.dao.IDeptDao;
import com.bdqn.dao.impl.DeptDaoImpl;
import com.bdqn.entity.Dept;
import com.bdqn.service.IDeptService;
public class DeptServiceImpl extends HibernateDaoSupport implements IDeptService{
// 调用我们实现类的接口 定义成私有字段进行封装
private IDeptDao idd;
//查询
public List findAll() {
// TODO Auto-generated method stub
return this.idd.findAll();
}
//增加
public boolean save(Dept d) {
// TODO Auto-generated method stub
int aa=this.idd.save(d);
if(aa==0){
return true;
}else{
return false;
}
}
//删除
public boolean delete(int id) {
// TODO Auto-generated method stub
int aa=this.idd.delete(id);
if(aa==0){
return true;
}else{
return false;
}
}
//修改
public boolean update(Dept d) {
// TODO Auto-generated method stub
int aa=this.idd.update(d);
if(aa==0){
return true;
}else{
return false;
}
}
//根据ID查询
public Dept findById(int id) {
// TODO Auto-generated method stub
return this.idd.findById(id);
}
//封装的GET SET方法
public IDeptDao getIdd() {
return idd;
}
public void setIdd(IDeptDao idd) {
this.idd = idd;
}
}
6.DeptAction 包名:com.bdqn.action(根据自己的习惯定义就可以)
package com.bdqn.action;
import java.util.List;
import com.bdqn.dao.IDeptDao;
import com.bdqn.entity.Dept;
import com.bdqn.service.IDeptService;
import com.opensymphony.xwork2.ActionSupport;
public class DeptAction extends ActionSupport {
//定义出来我们的对象名 id 以及泛型集合
private IDeptService ids;
private List list;
private Dept dept;
private int id;
//查询
public String show(){
list=ids.findAll();
return "zhanshi";
}
//删除
public String delete(){
ids.delete(id);
return "find";
}
//预修改
public String prepup(){
dept=ids.findById(id);
return "prepup";
}
//增加
public String save(){
ids.save(dept);
return "find";
}
//修改
public String update(){
ids.update(dept);
return "find";
}
public IDeptService getIds() {
return ids;
}
public void setIds(IDeptService ids) {
this.ids = ids;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
JSP页面:
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
展示dept表数据
增加dept表数据
save.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'save.jsp' starting page
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'list.jsp' starting page
编号:
部门:
地址:
操作:
${d.deptno}
${d.dname}
${d.loc}
修改
删除
update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'update.jsp' starting page
配置文件:applicationContext.xml
classpath:hibernate.cfg.xml
hibernate.cfg.xml
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/sanban?useUnicode=true&characterEncoding=UTF-8
root
root
org.hibernate.dialect.MySQLDialect
true
struts.xml
list.jsp
showdept
update.jsp
WEB.XML
index.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
如有不明白的地方 可以下载案例 附带JAR包:
案例的链接:点击下载案例
博主QQ:3044793043 写上 QQ,是因为有的问题评论区,不方便解决。而不是发源码,个人建议手写一遍比较好!!