当前位置:   article > 正文

SSH实例(简单地增删改查功能)_用ssh框架做的增删改查案例

用ssh框架做的增删改查案例

在网上看到一篇写的很不错的关于SSH 整合实现简单的增删改查功能的实例。

因为也是初次使用SSH框架,如有不足,请多指教。

先看一下我们完整的工程目录:

 好了  我们废话不多说 直接上操作:

1.(1.)Dept   我们的Bean  包名:com.bdqn.entity(根据自己的习惯定义就可以)

  1. package com.bdqn.entity;
  2. import java.io.Serializable;
  3. public class Dept implements Serializable {
  4. //封装字段
  5. private int deptno;
  6. private String dname;
  7. private String loc;
  8. //无参构造函数
  9. public Dept() {
  10. // TODO Auto-generated constructor stub
  11. }
  12. //有参构造函数
  13. public Dept(int deptno, String dname, String loc) {
  14. super();
  15. this.deptno = deptno;
  16. this.dname = dname;
  17. this.loc = loc;
  18. }
  19. //封装GET SET方法
  20. public int getDeptno() {
  21. return deptno;
  22. }
  23. public void setDeptno(int deptno) {
  24. this.deptno = deptno;
  25. }
  26. public String getDname() {
  27. return dname;
  28. }
  29. public void setDname(String dname) {
  30. this.dname = dname;
  31. }
  32. public String getLoc() {
  33. return loc;
  34. }
  35. public void setLoc(String loc) {
  36. this.loc = loc;
  37. }
  38. }

(2.)Dept.hbm.xml  配置映射文件:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <!-- Dept实体的映射文件
  6. 把实体类的属性和表中的字段映射在一起
  7. -->
  8. <hibernate-mapping package="com.bdqn.entity">
  9. <class name="com.bdqn.entity.Dept" table="dept">
  10. <!-- 配置主键 -->
  11. <id name="deptno" >
  12. <!-- 主键生成策略 -->
  13. <generator class="increment"/>
  14. </id>
  15. <property name="dname" />
  16. <property name="loc" />
  17. </class>
  18. </hibernate-mapping>

2.IDeptDao  配置接口   包名:com.bdqn.dao(根据自己的习惯定义就可以)

  1. package com.bdqn.dao;
  2. import java.util.List;
  3. import com.bdqn.entity.Dept;
  4. /**
  5. * 定义接口
  6. * @author 萌萌里的小高冷
  7. *
  8. */
  9. public interface IDeptDao {
  10. //查询
  11. public List<Dept> findAll();
  12. //增加
  13. public int save(Dept d);
  14. //删除
  15. public int delete(int id);
  16. //修改
  17. public int update(Dept d);
  18. //根据ID查询
  19. public Dept findById(int id);
  20. }


3.DeptDaoImpl  配置实现类  包名:com.bdqn.dao.impl(根据自己的习惯定义就可以)

  1. package com.bdqn.dao.impl;
  2. import java.util.List;
  3. import org.hibernate.SessionFactory;
  4. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  5. import com.bdqn.dao.IDeptDao;
  6. import com.bdqn.entity.Dept;
  7. public class DeptDaoImpl
  8. extends HibernateDaoSupport
  9. implements IDeptDao {
  10. public int delete(int id) {
  11. // TODO Auto-generated method stub
  12. try {
  13. //获取对象的id
  14. Dept d=this.getHibernateTemplate().get(Dept.class,id);
  15. //执行删除方法 删除id
  16. this.getHibernateTemplate().delete(d);
  17. //删除成功 return 1;
  18. return 1;
  19. } catch (Exception e) {
  20. // TODO: handle exception
  21. System.out.println(e.getMessage());
  22. System.out.println(e.getStackTrace());
  23. }
  24. return 0;
  25. }
  26. public List<Dept> findAll() {
  27. // TODO Auto-generated method stub
  28. //查询获取全部的数据
  29. List<Dept> list=(List<Dept>) this.getHibernateTemplate().find("from Dept");
  30. return list;
  31. }
  32. public Dept findById(int id) {
  33. // TODO Auto-generated method stub
  34. //查询出对象的id
  35. Dept dd=this.getHibernateTemplate().get(Dept.class, id);
  36. return dd;
  37. }
  38. public int save(Dept d) {
  39. // TODO Auto-generated method stub
  40. try {
  41. //调用我们定义的接口 增加数据
  42. this.getHibernateTemplate().save(d);
  43. return 1;
  44. } catch (Exception e) {
  45. // TODO: handle exception
  46. System.out.println(e.getMessage());
  47. System.out.println(e.getStackTrace());
  48. }
  49. return 0;
  50. }
  51. public int update(Dept d) {
  52. // TODO Auto-generated method stub
  53. try {
  54. //调用我们定义的接口 增加数据
  55. this.getHibernateTemplate().update(d);
  56. return 1;
  57. } catch (Exception e) {
  58. // TODO: handle exception
  59. System.out.println(e.getMessage());
  60. System.out.println(e.getStackTrace());
  61. }
  62. return 0;
  63. }
  64. }


4.IDeptService 配置我们的业务逻辑层  包名:com.bdqn.service(根据自己的习惯定义就可以)

  1. package com.bdqn.service;
  2. import java.util.List;
  3. import com.bdqn.entity.Dept;
  4. public interface IDeptService {
  5. //跟我们的dao层一样
  6. public List<Dept> findAll();
  7. public boolean save(Dept d);
  8. public boolean delete(int id);
  9. public boolean update(Dept d);
  10. public Dept findById(int id);
  11. }


5.DeptServiceImpl   包名:com.bdqn.service.Impl(根据自己的习惯定义就可以)

  1. package com.bdqn.service.impl;
  2. import java.util.List;
  3. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  4. import com.bdqn.dao.IDeptDao;
  5. import com.bdqn.dao.impl.DeptDaoImpl;
  6. import com.bdqn.entity.Dept;
  7. import com.bdqn.service.IDeptService;
  8. public class DeptServiceImpl extends HibernateDaoSupport implements IDeptService{
  9. // 调用我们实现类的接口 定义成私有字段进行封装
  10. private IDeptDao idd;
  11. //查询
  12. public List<Dept> findAll() {
  13. // TODO Auto-generated method stub
  14. return this.idd.findAll();
  15. }
  16. //增加
  17. public boolean save(Dept d) {
  18. // TODO Auto-generated method stub
  19. int aa=this.idd.save(d);
  20. if(aa==0){
  21. return true;
  22. }else{
  23. return false;
  24. }
  25. }
  26. //删除
  27. public boolean delete(int id) {
  28. // TODO Auto-generated method stub
  29. int aa=this.idd.delete(id);
  30. if(aa==0){
  31. return true;
  32. }else{
  33. return false;
  34. }
  35. }
  36. //修改
  37. public boolean update(Dept d) {
  38. // TODO Auto-generated method stub
  39. int aa=this.idd.update(d);
  40. if(aa==0){
  41. return true;
  42. }else{
  43. return false;
  44. }
  45. }
  46. //根据ID查询
  47. public Dept findById(int id) {
  48. // TODO Auto-generated method stub
  49. return this.idd.findById(id);
  50. }
  51. //封装的GET SET方法
  52. public IDeptDao getIdd() {
  53. return idd;
  54. }
  55. public void setIdd(IDeptDao idd) {
  56. this.idd = idd;
  57. }
  58. }


6.DeptAction   包名:com.bdqn.action(根据自己的习惯定义就可以)

  1. package com.bdqn.action;
  2. import java.util.List;
  3. import com.bdqn.dao.IDeptDao;
  4. import com.bdqn.entity.Dept;
  5. import com.bdqn.service.IDeptService;
  6. import com.opensymphony.xwork2.ActionSupport;
  7. public class DeptAction extends ActionSupport {
  8. //定义出来我们的对象名 id 以及泛型集合
  9. private IDeptService ids;
  10. private List<Dept> list;
  11. private Dept dept;
  12. private int id;
  13. //查询
  14. public String show(){
  15. list=ids.findAll();
  16. return "zhanshi";
  17. }
  18. //删除
  19. public String delete(){
  20. ids.delete(id);
  21. return "find";
  22. }
  23. //预修改
  24. public String prepup(){
  25. dept=ids.findById(id);
  26. return "prepup";
  27. }
  28. //增加
  29. public String save(){
  30. ids.save(dept);
  31. return "find";
  32. }
  33. //修改
  34. public String update(){
  35. ids.update(dept);
  36. return "find";
  37. }
  38. public IDeptService getIds() {
  39. return ids;
  40. }
  41. public void setIds(IDeptService ids) {
  42. this.ids = ids;
  43. }
  44. public List<Dept> getList() {
  45. return list;
  46. }
  47. public void setList(List<Dept> list) {
  48. this.list = list;
  49. }
  50. public Dept getDept() {
  51. return dept;
  52. }
  53. public void setDept(Dept dept) {
  54. this.dept = dept;
  55. }
  56. public int getId() {
  57. return id;
  58. }
  59. public void setId(int id) {
  60. this.id = id;
  61. }
  62. }


JSP页面:

index.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'index.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <a href="showdept.action">展示dept表数据</a>
  22. <a href="save.jsp">增加dept表数据</a>
  23. </body>
  24. </html>


save.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'save.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <form action="savedept.action" method="post">
  22. 部门:<input type="text" name="dept.dname" />
  23. 地址:<input type="text" name="dept.loc" />
  24. <input type="submit" />
  25. </form>
  26. </body>
  27. </html>


list.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>My JSP 'list.jsp' starting page</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. <!--
  18. <link rel="stylesheet" type="text/css" href="styles.css">
  19. -->
  20. </head>
  21. <body>
  22. <table border="1">
  23. <tr>
  24. <td>编号:</td>
  25. <td>部门:</td>
  26. <td>地址:</td>
  27. <td>操作:</td>
  28. </tr>
  29. <c:forEach var="d" items="${list}">
  30. <tr>
  31. <td>${d.deptno}</td>
  32. <td>${d.dname}</td>
  33. <td>${d.loc}</td>
  34. <td>
  35. <a href="prepupdept.action?id=${d.deptno}">修改</a>
  36. <a href="deletedept.action?id=${d.deptno}">删除</a>
  37. </td>
  38. </tr>
  39. </c:forEach>
  40. </table>
  41. </body>
  42. </html>


update.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>My JSP 'update.jsp' starting page</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. <!--
  18. <link rel="stylesheet" type="text/css" href="styles.css">
  19. -->
  20. </head>
  21. <body>
  22. <form action="updatedept.action" method="post">
  23. id:<input type="text" value="${dept.deptno}" name="dept.deptno" />
  24. 部门:<input type="text" value="${dept.dname}" name="dept.dname" />
  25. 地址:<input type="text" value="${dept.loc}" name="dept.loc" />
  26. <input type="submit" />
  27. </form>
  28. </body>
  29. </html>


配置文件:applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  14. ">
  15. <!-- 加载 hibernate的配置 定义sessionFactory -->
  16. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
  17. <property name="configLocation">
  18. <value>classpath:hibernate.cfg.xml</value>
  19. </property>
  20. </bean>
  21. <!-- dao层 -->
  22. <bean id="deptdaoimpl" class="com.bdqn.dao.impl.DeptDaoImpl" >
  23. <property name="sessionFactory" ref="sessionFactory"></property>
  24. </bean>
  25. <!-- service层 -->
  26. <bean id="deptservice" class="com.bdqn.service.impl.DeptServiceImpl" >
  27. <property name="idd" ref="deptdaoimpl" ></property>
  28. <property name="sessionFactory" ref="sessionFactory"></property>
  29. </bean>
  30. <!-- action层 -->
  31. <bean id="deptaction" class="com.bdqn.action.DeptAction" >
  32. <property name="ids" ref="deptservice" ></property>
  33. </bean>
  34. <!-- 事务的功能实现类-->
  35. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  36. <property name="sessionFactory" ref="sessionFactory" />
  37. </bean>
  38. <!-- 把事务封装好的一个 面板 -->
  39. <tx:advice id="txAdvice" transaction-manager="txManager" >
  40. <tx:attributes>
  41. <tx:method name="save(..)" read-only= "true" />
  42. <tx:method name="*" />
  43. </tx:attributes>
  44. </tx:advice>
  45. <!-- 封装结束 -->
  46. <!-- 切入事务开始 织入 -->
  47. <aop:config>
  48. <aop:pointcut id="pointcut" expression="execution(* com.bdqn.dao.impl.*.*(..))" />
  49. <aop:advisor pointcut-ref="pointcut" advice-ref="txAdvice"/>
  50. </aop:config>
  51. <!-- 切入事务结束 -->
  52. </beans>


hibernate.cfg.xml

  1. <!DOCTYPE hibernate-configuration PUBLIC
  2. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  4. <hibernate-configuration>
  5. <session-factory>
  6. <!-- 数据库连接信息 -->
  7. <property name="connection.driver_class">
  8. com.mysql.jdbc.Driver
  9. </property>
  10. <property name="connection.url">
  11. jdbc:mysql://localhost:3306/sanban?useUnicode=true&characterEncoding=UTF-8
  12. </property>
  13. <property name="connection.username">root</property>
  14. <property name="connection.password">root</property>
  15. <!-- 数据库方言 -->
  16. <property name="dialect">
  17. org.hibernate.dialect.MySQLDialect
  18. </property>
  19. <!-- 是否显示sql语句 -->
  20. <property name="show_sql">true</property>
  21. <mapping resource="com/bdqn/entity/Dept.hbm.xml" />
  22. </session-factory>
  23. </hibernate-configuration>


struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <!-- 设置编码方式 -->
  7. <constant name="struts.i18n.encoding" value="utf-8"></constant>
  8. <package name="default" extends="struts-default" namespace="/" >
  9. <action name="*dept" class="deptaction" method="{1}">
  10. <result name="zhanshi">list.jsp</result>
  11. <result name="find" type="chain">showdept</result>
  12. <result name="prepup">update.jsp</result>
  13. </action>
  14. </package>
  15. </struts>

WEB.XML

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <welcome-file-list>
  4. <welcome-file>index.jsp</welcome-file>
  5. </welcome-file-list>
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>
  9. classpath:applicationContext.xml
  10. </param-value>
  11. </context-param>
  12. <listener>
  13. <listener-class>
  14. org.springframework.web.context.ContextLoaderListener
  15. </listener-class>
  16. </listener>
  17. <filter>
  18. <filter-name>struts2</filter-name>
  19. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  20. </filter>
  21. <filter-mapping>
  22. <filter-name>struts2</filter-name>
  23. <url-pattern>/*</url-pattern>
  24. </filter-mapping>
  25. </web-app>


如有不明白的地方  可以下载案例  附带JAR包:

案例的链接:点击下载案例

博主QQ:3044793043 写上 QQ,是因为有的问题评论区,不方便解决。而不是发源码,个人建议手写一遍比较好!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/185260
推荐阅读
相关标签
  

闽ICP备14008679号