这里配置hibernate与struts不再过多赘述,配置搭建前文已经详细讲解,配置如下:

hibernate.hbm.xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  8. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/zhenzai?characterEncoding=GBK</property>
  9. <property name="hibernate.connection.username">root</property>
  10. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  11. <property name="show_sql">true</property>
  12. <mapping resource="com/model/News.hbm.xml"/>
  13. </session-factory>
  14. </hibernate-configuration>

com.model.News.java配置:

  1. package com.model;
  2. // Generated 2017-3-14 10:57:00 by Hibernate Tools 5.2.0.CR1
  3.  
  4. import java.util.Date;
  5.  
  6. /**
  7. * News generated by hbm2java
  8. */
  9. public class News implements java.io.Serializable {
  10.  
  11. private Integer ids;
  12. private String title;
  13. private Date time;
  14. private String content;
  15.  
  16. public News() {
  17. }
  18.  
  19. public News(String title, Date time, String content) {
  20. this.title = title;
  21. this.time = time;
  22. this.content = content;
  23. }
  24.  
  25. public Integer getIds() {
  26. return this.ids;
  27. }
  28.  
  29. public void setIds(Integer ids) {
  30. this.ids = ids;
  31. }
  32.  
  33. public String getTitle() {
  34. return this.title;
  35. }
  36.  
  37. public void setTitle(String title) {
  38. this.title = title;
  39. }
  40.  
  41. public Date getTime() {
  42. return this.time;
  43. }
  44.  
  45. public void setTime(Date time) {
  46. this.time = time;
  47. }
  48.  
  49. public String getContent() {
  50. return this.content;
  51. }
  52.  
  53. public void setContent(String content) {
  54. this.content = content;
  55. }
  56.  
  57. }

com.model.News..hbm.xml配置:

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  4. <!-- Generated 2017-3-14 10:57:01 by Hibernate Tools 5.2.0.CR1 -->
  5. <hibernate-mapping>
  6. <class name="com.model.News" table="news" catalog="zhenzai" optimistic-lock="version">
  7. <id name="ids" type="java.lang.Integer">
  8. <column name="ids" />
  9. <generator class="identity" />
  10. </id>
  11. <property name="title" type="string">
  12. <column name="title" />
  13. </property>
  14. <property name="time" type="timestamp">
  15. <column name="time" length="19" />
  16. </property>
  17. <property name="content" type="string">
  18. <column name="content" />
  19. </property>
  20. </class>
  21. </hibernate-mapping>

com.dao.HibernateUtil.java配置:

  1. package com.dao;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.cfg.Configuration;
  6.  
  7. import javafx.util.BuilderFactory;
  8.  
  9. public class HibernateUtil {
  10. private static final SessionFactory factory = BuilderFactory();
  11. private static final ThreadLocal<Session> lock = new ThreadLocal<Session>();
  12. private static SessionFactory BuilderFactory() {
  13. Configuration config = new Configuration().configure();
  14. return config.buildSessionFactory();
  15. }
  16. public static Session getsession(){
  17. Session session=lock.get();
  18. if(session==null){
  19. session=factory.openSession();
  20. lock.set(session);
  21. }
  22. return session;
  23. }
  24. public static void closeSession(){
  25. Session session=lock.get();
  26. if(session !=null){
  27. session.close();
  28. lock.set(null);
  29. }
  30. }
  31. }

com.dao.NewsDao.java配置:

  1. package com.dao;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.hibernate.Session;
  7.  
  8. import com.model.News;
  9.  
  10. public class NewsDao {
  11. private Session session=null;
  12. public NewsDao(){
  13. session=HibernateUtil.getsession();
  14. }
  15. public List<News> select(){
  16. List<News> list = new ArrayList<News>();
  17. try{
  18. list = session.createQuery("from News").getResultList();
  19.  
  20. }catch(Exception e){
  21. e.printStackTrace();
  22. }
  23. finally{
  24. HibernateUtil.closeSession();
  25. }
  26. return list;
  27. }
  28. public News select(int ids){
  29. News list = new News();
  30. try{
  31. list = (News)session.createQuery("from News where ids=?")
  32. .setParameter(0,ids)
  33. .getSingleResult();
  34.  
  35. }catch(Exception e){
  36. e.printStackTrace();
  37. }
  38. finally{
  39. HibernateUtil.closeSession();
  40. }
  41. return list;
  42. }
  43. public void insert(News news){
  44.  
  45. try{
  46. session.beginTransaction();
  47. session.save(news);
  48. session.getTransaction().commit();
  49.  
  50. }catch(Exception e){
  51. e.printStackTrace();
  52. session.getTransaction().rollback();
  53. }
  54. finally{
  55. HibernateUtil.closeSession();
  56. }
  57. }
  58. public void update(News news){
  59. try{
  60. session.beginTransaction();
  61. News n=session.get(News.class, news.getIds());
  62. n.setTitle(news.getTitle());
  63. n.setTime(news.getTime());
  64. n.setContent(news.getContent());
  65. session.update(n);
  66. session.getTransaction().commit();
  67.  
  68. }catch(Exception e){
  69. e.printStackTrace();
  70. session.getTransaction().rollback();
  71. }
  72. finally{
  73. HibernateUtil.closeSession();
  74. }
  75. }
  76. public void delete(int ids){
  77. try{
  78. session.beginTransaction();
  79. News n = session.get(News.class, ids);
  80. session.delete(n);
  81. session.getTransaction().commit();
  82.  
  83. }catch(Exception e){
  84. e.printStackTrace();
  85. session.getTransaction().rollback();
  86. }
  87. finally{
  88. HibernateUtil.closeSession();
  89. }
  90. }
  91.  
  92. }

接下来就是配置struts的内容:

web.xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  5.  
  6. <display-name>Struts Blank</display-name>
  7.  
  8. <filter>
  9. <filter-name>struts2</filter-name>
  10. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  11. </filter>
  12.  
  13. <filter-mapping>
  14. <filter-name>struts2</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>
  17.  
  18. <welcome-file-list>
  19. <welcome-file>index.html</welcome-file>
  20. </welcome-file-list>
  21.  
  22. </web-app>

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.  
  6. <struts>
  7.  
  8. <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  9. <constant name="struts.devMode" value="true" />
  10.  
  11. <package name="default" namespace="/" extends="struts-default">
  12. <action name="*_*" class="com.controller.{1}Action" method="{2}">
  13. <result>
  14. {1}_{2}.jsp
  15. </result>
  16. </action>
  17. </package>
  18.  
  19. </struts>

com.controller.NewsAction.java配置:

  1. package com.controller;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.servlet.http.HttpServletRequest;
  6.  
  7. import org.apache.struts2.ServletActionContext;
  8.  
  9. import com.dao.NewsDao;
  10. import com.model.News;
  11. import com.opensymphony.xwork2.ActionSupport;
  12.  
  13. public class NewsAction extends ActionSupport {
  14. private int ids;//创建一个ids字段,供前后端调用时使用
  15. private News news;//创建一个news对象,供前端调用时直接使用news.xxxx
  16. public int getIds() {
  17. return ids;
  18. }
  19. public void setIds(int ids) {
  20. this.ids = ids;
  21. }
  22. public News getNews() {
  23. return news;
  24. }
  25. public void setNews(News news) {
  26. this.news = news;
  27. }
  28. public String get(){//对应News_get.jsp界面
  29. news = new NewsDao().select(ids);
  30. return SUCCESS;
  31.  
  32. }
  33. public String getAll(){//对应News_getAll.jsp界面
  34. List<News> list = new NewsDao().select();
  35. HttpServletRequest req = ServletActionContext.getRequest();//后端直接使用request搭建前后端连接
  36. req.setAttribute("list", list);
  37. return SUCCESS;
  38.  
  39. }
  40. public String add(){//对应News_add.jsp界面
  41.  
  42. return SUCCESS;
  43. }
  44. public String insert(){//对应News_insert.jsp界面
  45. new NewsDao().insert(news);
  46. return SUCCESS;
  47. }
  48. public String edit(){//对应News_edit.jsp界面
  49. news = new NewsDao().select(ids);
  50. return SUCCESS;
  51. }
  52. public String update(){//对应News_update.jsp界面
  53. new NewsDao().update(news);
  54. return SUCCESS;
  55. }
  56. public String delete(){//对应News_delete.jsp界面
  57. new NewsDao().delete(ids);
  58. return SUCCESS;
  59. }
  60. }

News_getAll.jsp配置:

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <h1>新闻显示</h1>
  12. <div>
  13. <c:forEach var="l" items="${list }">//获取getAll方法中request提供的list
  14. <div>
  15. <a href="News_get?ids=${l.ids }">${l.ids }&nbsp;&nbsp;${l.title }</a>&nbsp;&nbsp;<a href="News_edit?ids=${l.ids }">修改</a><a href="News_delete?ids=${l.ids }">删除</a>
  16. </div>
  17. </c:forEach>
  18. </div>
  19. <div>
  20. <a href="News_add">添加</a>
  21. </div>
  22. </body>
  23. </html>

News_get.jsp配置:

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. ${news.title }<br><br>//使用的是NewsAction方法中的private News news;
  11. ${news.time }<br><br>
  12. ${news.content }<br><br>
  13. </body>
  14. </html>

News_add.jsp配置:

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h1>新闻添加</h1>
  11. <form action="News_insert" method="post">
  12. 标题:<input type="text" name="news.title"><br>//使用的是NewsAction方法中的private News news;
  13. 时间:<input type="text" name="news.time"><br>
  14. 内容:<textarea rows="10" cols="12" name="news.content"></textarea>
  15. <input type="submit" value="添加">
  16. </form>
  17. </body>
  18. </html>

News_insert.jsp配置:

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 添加成功
  11. </body>
  12. </html>

News_edit.jsp配置:

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form method="post" action="News_update">
  11. <input type="hidden" name="news.ids" value="${news.ids }"><br>//使用的是NewsAction方法中的private News news;
  12. 标题:<input type="text" name="news.title" value="${news.title }"><br>
  13. 时间:<input type="text" name="news.time" value="${news.time }"><br>
  14. 内容:<textarea rows="10" cols="10" name="news.content" >${news.content }</textarea>
  15. <input type="submit" value="修改">
  16. </form>
  17. </body>
  18. </html>

News_update.jsp配置:

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 修改成功
  11. </body>
  12. </html>

News_delete.jsp配置:

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 删除成功
  11. </body>
  12. </html>

hibernate与struts框架实现增删改查的更多相关文章

  1. tp框架的增删改查

    首先,我们来看一下tp框架里面的查询方法: 查询有很多种,代码如下: <?php namespace Admin\Controller; use Think\Controller; class ...

  2. Yii2.0高级框架数据库增删改查的一些操作(转)

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

  3. Yii2.0高级框架数据库增删改查的一些操作

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

  4. Entity - 使用EF框架进行增删改查 - 模型先行

    模型先行:先创建数据库实体模型,然后再进行数据库的增删改查. 基本步骤是不变的,可参照 <Entity - 使用EF框架进行增删改查 - 数据库先行> 其中的不同是,在创建数据库实体模型的 ...

  5. Hibernate通过createSQLQuery( )方法实现增删改查

    一.项目结构 二.hibernate核心配置文件:   hibernate.cfg.xm <?xml version="1.0" encoding="UTF-8&q ...

  6. ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)

    ssm 框架实现增删改查 SpringBoot 项目整合 一.项目准备 1.1 ssm 框架环境搭建 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二.项目实现 1. Emplo ...

  7. 2、hibernate七步走完成增删改查

    一.hibernate框架介绍如下 1.框架=模板 2.Hibernate是对象模型与关系数据库模型之间的桥梁 3.hibernate持久化概念 什么是ORM ORM是对象关系映射,是一种数据持久化操 ...

  8. Hibernate之API初识及增删改查实现

    声明:关于hibernate的学习.非常大一部分东西都是概念性的. 大家最好手里都有一份学习资料,在我的博文中.我不会把书本上的概念一类的东西搬过来.那没有不论什么意义.关于hibernate的学习, ...

  9. hibernate基本配置与简单增删改查

    ORM(Object Relation Mapping)是对象关系映射,是一个思想,它的作用是在关系数据库与对象之间做一个自动映射,将数据库中的表格映射到一个类,也就是持久化类,数据表中每行映射为对象 ...

随机推荐

  1. C# 向服务器上传文件(客服端winform、服务端web)

    转载 首先写客服端,winform模拟一个post提交: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summa ...

  2. mininet安装,使用

    http://mininet.org/download/ http://sdnhub.cn/index.php/mininet-walkthrough-chinese/ --------------- ...

  3. Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟

    Input Input data to the program specifies the order in which cards are dealt from the pack. The inpu ...

  4. python 函数内使用全局变量

    x = def change_global(): global x x = x + change_global() print(x) result: 2

  5. 使用jquery.ajax实现省市的二级联动(SSH架构)

    首先实现jquery ajax的二级联动 要下载个jquery.js 我在这里就不准备了 自行百度下载 背景介绍:通过部门的ID来查找部门下的所有班级 我实现二级联动的思路是:先查询所有部门 显示在页 ...

  6. C#数组添加元素

    一.向数组添加元素 在C#中,只能在动态数组ArrayList类中向数组添加元素.因为动态数组是一个可以改变数组长度和元素个数的数据类型. 示例: using System;using System. ...

  7. stataic 变量

    static 是静态变量的的类型说明符 静态变量属于静态存储方式,(外部变量也是静态存储方式) 静态的局部变量 静态局部变量属于静态存储方式,它具有以下特点: (1)静态局部变量在函数内定义 它的生存 ...

  8. python入门:数字型和字符串换行要同类型 注意连接符

    #!/usr/bin/env python # -*- coding: utf-8 -*- #数字型和字符串换行要同类型 注意连接符 a = 1 b = 2 print(str(a) + " ...

  9. ubuntu 16.04下如何打造 sublime python编程环境

    一.安装python3     ubuntu自身是安装python2的,例如在ubuntu 16.04中安装的就是python2.7.但我想在python3的环境下进行开发所以就要安装python3. ...

  10. Developing for nRF52810(转载)

    Table of Contents Introduction Hardware emulation of nRF52810 Limitations Software emulation of nRF5 ...