版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36537546/article/details/78749344
在网上看到一篇写的很不错的关于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 配置映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Dept实体的映射文件
把实体类的属性和表中的字段映射在一起
-->
<hibernate-mapping package="com.bdqn.entity">
<class name="com.bdqn.entity.Dept" table="dept">
<!-- 配置主键 -->
<id name="deptno" >
<!-- 主键生成策略 -->
<generator class="increment"/>
</id>
<property name="dname" />
<property name="loc" />
</class>
</hibernate-mapping>
2.IDeptDao 配置接口 包名:com.bdqn.dao(根据自己的习惯定义就可以)
package com.bdqn.dao;
import java.util.List;
import com.bdqn.entity.Dept;
/**
* 定义接口
* @author 萌萌里的小高冷
*
*/
public interface IDeptDao {
//查询
public List<Dept> 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<Dept> findAll() {
// TODO Auto-generated method stub
//查询获取全部的数据
List<Dept> list=(List<Dept>) 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<Dept> 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<Dept> 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<Dept> 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<Dept> getList() {
return list;
}
public void setList(List<Dept> 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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="showdept.action">展示dept表数据</a>
<a href="save.jsp">增加dept表数据</a>
</body>
</html>
save.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'save.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="savedept.action" method="post">
部门:<input type="text" name="dept.dname" />
地址:<input type="text" name="dept.loc" />
<input type="submit" />
</form>
</body>
</html>
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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'list.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table border="1">
<tr>
<td>编号:</td>
<td>部门:</td>
<td>地址:</td>
<td>操作:</td>
</tr>
<c:forEach var="d" items="${list}">
<tr>
<td>${d.deptno}</td>
<td>${d.dname}</td>
<td>${d.loc}</td>
<td>
<a href="prepupdept.action?id=${d.deptno}">修改</a>
<a href="deletedept.action?id=${d.deptno}">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
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+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'update.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="updatedept.action" method="post">
id:<input type="text" value="${dept.deptno}" name="dept.deptno" />
部门:<input type="text" value="${dept.dname}" name="dept.dname" />
地址:<input type="text" value="${dept.loc}" name="dept.loc" />
<input type="submit" />
</form>
</body>
</html>
配置文件: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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
">
<!-- 加载 hibernate的配置 定义sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- dao层 -->
<bean id="deptdaoimpl" class="com.bdqn.dao.impl.DeptDaoImpl" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- service层 -->
<bean id="deptservice" class="com.bdqn.service.impl.DeptServiceImpl" >
<property name="idd" ref="deptdaoimpl" ></property>
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- action层 -->
<bean id="deptaction" class="com.bdqn.action.DeptAction" >
<property name="ids" ref="deptservice" ></property>
</bean>
<!-- 事务的功能实现类-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 把事务封装好的一个 面板 -->
<tx:advice id="txAdvice" transaction-manager="txManager" >
<tx:attributes>
<tx:method name="save(..)" read-only= "true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 封装结束 -->
<!-- 切入事务开始 织入 -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.bdqn.dao.impl.*.*(..))" />
<aop:advisor pointcut-ref="pointcut" advice-ref="txAdvice"/>
</aop:config>
<!-- 切入事务结束 -->
</beans>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接信息 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sanban?useUnicode=true&characterEncoding=UTF-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 是否显示sql语句 -->
<property name="show_sql">true</property>
<mapping resource="com/bdqn/entity/Dept.hbm.xml" />
</session-factory>
</hibernate-configuration>
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>
<!-- 设置编码方式 -->
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<package name="default" extends="struts-default" namespace="/" >
<action name="*dept" class="deptaction" method="{1}">
<result name="zhanshi">list.jsp</result>
<result name="find" type="chain">showdept</result>
<result name="prepup">update.jsp</result>
</action>
</package>
</struts>
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>
</web-app>
如有不明白的地方 可以下载案例 附带JAR包:
案例的链接:点击下载案例