hibernate与struts框架实现增删改查
这里配置hibernate与struts不再过多赘述,配置搭建前文已经详细讲解,配置如下:
hibernate.hbm.xml配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/zhenzai?characterEncoding=GBK</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="show_sql">true</property>
- <mapping resource="com/model/News.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
com.model.News.java配置:
- package com.model;
- // Generated 2017-3-14 10:57:00 by Hibernate Tools 5.2.0.CR1
- import java.util.Date;
- /**
- * News generated by hbm2java
- */
- public class News implements java.io.Serializable {
- private Integer ids;
- private String title;
- private Date time;
- private String content;
- public News() {
- }
- public News(String title, Date time, String content) {
- this.title = title;
- this.time = time;
- this.content = content;
- }
- public Integer getIds() {
- return this.ids;
- }
- public void setIds(Integer ids) {
- this.ids = ids;
- }
- public String getTitle() {
- return this.title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public Date getTime() {
- return this.time;
- }
- public void setTime(Date time) {
- this.time = time;
- }
- public String getContent() {
- return this.content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- }
com.model.News..hbm.xml配置:
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <!-- Generated 2017-3-14 10:57:01 by Hibernate Tools 5.2.0.CR1 -->
- <hibernate-mapping>
- <class name="com.model.News" table="news" catalog="zhenzai" optimistic-lock="version">
- <id name="ids" type="java.lang.Integer">
- <column name="ids" />
- <generator class="identity" />
- </id>
- <property name="title" type="string">
- <column name="title" />
- </property>
- <property name="time" type="timestamp">
- <column name="time" length="19" />
- </property>
- <property name="content" type="string">
- <column name="content" />
- </property>
- </class>
- </hibernate-mapping>
com.dao.HibernateUtil.java配置:
- package com.dao;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import javafx.util.BuilderFactory;
- public class HibernateUtil {
- private static final SessionFactory factory = BuilderFactory();
- private static final ThreadLocal<Session> lock = new ThreadLocal<Session>();
- private static SessionFactory BuilderFactory() {
- Configuration config = new Configuration().configure();
- return config.buildSessionFactory();
- }
- public static Session getsession(){
- Session session=lock.get();
- if(session==null){
- session=factory.openSession();
- lock.set(session);
- }
- return session;
- }
- public static void closeSession(){
- Session session=lock.get();
- if(session !=null){
- session.close();
- lock.set(null);
- }
- }
- }
com.dao.NewsDao.java配置:
- package com.dao;
- import java.util.ArrayList;
- import java.util.List;
- import org.hibernate.Session;
- import com.model.News;
- public class NewsDao {
- private Session session=null;
- public NewsDao(){
- session=HibernateUtil.getsession();
- }
- public List<News> select(){
- List<News> list = new ArrayList<News>();
- try{
- list = session.createQuery("from News").getResultList();
- }catch(Exception e){
- e.printStackTrace();
- }
- finally{
- HibernateUtil.closeSession();
- }
- return list;
- }
- public News select(int ids){
- News list = new News();
- try{
- list = (News)session.createQuery("from News where ids=?")
- .setParameter(0,ids)
- .getSingleResult();
- }catch(Exception e){
- e.printStackTrace();
- }
- finally{
- HibernateUtil.closeSession();
- }
- return list;
- }
- public void insert(News news){
- try{
- session.beginTransaction();
- session.save(news);
- session.getTransaction().commit();
- }catch(Exception e){
- e.printStackTrace();
- session.getTransaction().rollback();
- }
- finally{
- HibernateUtil.closeSession();
- }
- }
- public void update(News news){
- try{
- session.beginTransaction();
- News n=session.get(News.class, news.getIds());
- n.setTitle(news.getTitle());
- n.setTime(news.getTime());
- n.setContent(news.getContent());
- session.update(n);
- session.getTransaction().commit();
- }catch(Exception e){
- e.printStackTrace();
- session.getTransaction().rollback();
- }
- finally{
- HibernateUtil.closeSession();
- }
- }
- public void delete(int ids){
- try{
- session.beginTransaction();
- News n = session.get(News.class, ids);
- session.delete(n);
- session.getTransaction().commit();
- }catch(Exception e){
- e.printStackTrace();
- session.getTransaction().rollback();
- }
- finally{
- HibernateUtil.closeSession();
- }
- }
- }
接下来就是配置struts的内容:
web.xml配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>Struts Blank</display-name>
- <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>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
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.enable.DynamicMethodInvocation" value="false" />
- <constant name="struts.devMode" value="true" />
- <package name="default" namespace="/" extends="struts-default">
- <action name="*_*" class="com.controller.{1}Action" method="{2}">
- <result>
- {1}_{2}.jsp
- </result>
- </action>
- </package>
- </struts>
com.controller.NewsAction.java配置:
- package com.controller;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.ServletActionContext;
- import com.dao.NewsDao;
- import com.model.News;
- import com.opensymphony.xwork2.ActionSupport;
- public class NewsAction extends ActionSupport {
- private int ids;//创建一个ids字段,供前后端调用时使用
- private News news;//创建一个news对象,供前端调用时直接使用news.xxxx
- public int getIds() {
- return ids;
- }
- public void setIds(int ids) {
- this.ids = ids;
- }
- public News getNews() {
- return news;
- }
- public void setNews(News news) {
- this.news = news;
- }
- public String get(){//对应News_get.jsp界面
- news = new NewsDao().select(ids);
- return SUCCESS;
- }
- public String getAll(){//对应News_getAll.jsp界面
- List<News> list = new NewsDao().select();
- HttpServletRequest req = ServletActionContext.getRequest();//后端直接使用request搭建前后端连接
- req.setAttribute("list", list);
- return SUCCESS;
- }
- public String add(){//对应News_add.jsp界面
- return SUCCESS;
- }
- public String insert(){//对应News_insert.jsp界面
- new NewsDao().insert(news);
- return SUCCESS;
- }
- public String edit(){//对应News_edit.jsp界面
- news = new NewsDao().select(ids);
- return SUCCESS;
- }
- public String update(){//对应News_update.jsp界面
- new NewsDao().update(news);
- return SUCCESS;
- }
- public String delete(){//对应News_delete.jsp界面
- new NewsDao().delete(ids);
- return SUCCESS;
- }
- }
News_getAll.jsp配置:
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h1>新闻显示</h1>
- <div>
- <c:forEach var="l" items="${list }">//获取getAll方法中request提供的list
- <div>
- <a href="News_get?ids=${l.ids }">${l.ids } ${l.title }</a> <a href="News_edit?ids=${l.ids }">修改</a><a href="News_delete?ids=${l.ids }">删除</a>
- </div>
- </c:forEach>
- </div>
- <div>
- <a href="News_add">添加</a>
- </div>
- </body>
- </html>
News_get.jsp配置:
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- </head>
- <body>
- ${news.title }<br><br>//使用的是NewsAction方法中的private News news;
- ${news.time }<br><br>
- ${news.content }<br><br>
- </body>
- </html>
News_add.jsp配置:
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- </head>
- <body>
- <h1>新闻添加</h1>
- <form action="News_insert" method="post">
- 标题:<input type="text" name="news.title"><br>//使用的是NewsAction方法中的private News news;
- 时间:<input type="text" name="news.time"><br>
- 内容:<textarea rows="10" cols="12" name="news.content"></textarea>
- <input type="submit" value="添加">
- </form>
- </body>
- </html>
News_insert.jsp配置:
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- </head>
- <body>
- 添加成功
- </body>
- </html>
News_edit.jsp配置:
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- </head>
- <body>
- <form method="post" action="News_update">
- <input type="hidden" name="news.ids" value="${news.ids }"><br>//使用的是NewsAction方法中的private News news;
- 标题:<input type="text" name="news.title" value="${news.title }"><br>
- 时间:<input type="text" name="news.time" value="${news.time }"><br>
- 内容:<textarea rows="10" cols="10" name="news.content" >${news.content }</textarea>
- <input type="submit" value="修改">
- </form>
- </body>
- </html>
News_update.jsp配置:
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- </head>
- <body>
- 修改成功
- </body>
- </html>
News_delete.jsp配置:
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Insert title here</title>
- </head>
- <body>
- 删除成功
- </body>
- </html>
hibernate与struts框架实现增删改查的更多相关文章
- tp框架的增删改查
首先,我们来看一下tp框架里面的查询方法: 查询有很多种,代码如下: <?php namespace Admin\Controller; use Think\Controller; class ...
- Yii2.0高级框架数据库增删改查的一些操作(转)
yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...
- Yii2.0高级框架数据库增删改查的一些操作
yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...
- Entity - 使用EF框架进行增删改查 - 模型先行
模型先行:先创建数据库实体模型,然后再进行数据库的增删改查. 基本步骤是不变的,可参照 <Entity - 使用EF框架进行增删改查 - 数据库先行> 其中的不同是,在创建数据库实体模型的 ...
- Hibernate通过createSQLQuery( )方法实现增删改查
一.项目结构 二.hibernate核心配置文件: hibernate.cfg.xm <?xml version="1.0" encoding="UTF-8&q ...
- ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)
ssm 框架实现增删改查 SpringBoot 项目整合 一.项目准备 1.1 ssm 框架环境搭建 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二.项目实现 1. Emplo ...
- 2、hibernate七步走完成增删改查
一.hibernate框架介绍如下 1.框架=模板 2.Hibernate是对象模型与关系数据库模型之间的桥梁 3.hibernate持久化概念 什么是ORM ORM是对象关系映射,是一种数据持久化操 ...
- Hibernate之API初识及增删改查实现
声明:关于hibernate的学习.非常大一部分东西都是概念性的. 大家最好手里都有一份学习资料,在我的博文中.我不会把书本上的概念一类的东西搬过来.那没有不论什么意义.关于hibernate的学习, ...
- hibernate基本配置与简单增删改查
ORM(Object Relation Mapping)是对象关系映射,是一个思想,它的作用是在关系数据库与对象之间做一个自动映射,将数据库中的表格映射到一个类,也就是持久化类,数据表中每行映射为对象 ...
随机推荐
- C# 向服务器上传文件(客服端winform、服务端web)
转载 首先写客服端,winform模拟一个post提交: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summa ...
- mininet安装,使用
http://mininet.org/download/ http://sdnhub.cn/index.php/mininet-walkthrough-chinese/ --------------- ...
- 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 ...
- python 函数内使用全局变量
x = def change_global(): global x x = x + change_global() print(x) result: 2
- 使用jquery.ajax实现省市的二级联动(SSH架构)
首先实现jquery ajax的二级联动 要下载个jquery.js 我在这里就不准备了 自行百度下载 背景介绍:通过部门的ID来查找部门下的所有班级 我实现二级联动的思路是:先查询所有部门 显示在页 ...
- C#数组添加元素
一.向数组添加元素 在C#中,只能在动态数组ArrayList类中向数组添加元素.因为动态数组是一个可以改变数组长度和元素个数的数据类型. 示例: using System;using System. ...
- stataic 变量
static 是静态变量的的类型说明符 静态变量属于静态存储方式,(外部变量也是静态存储方式) 静态的局部变量 静态局部变量属于静态存储方式,它具有以下特点: (1)静态局部变量在函数内定义 它的生存 ...
- python入门:数字型和字符串换行要同类型 注意连接符
#!/usr/bin/env python # -*- coding: utf-8 -*- #数字型和字符串换行要同类型 注意连接符 a = 1 b = 2 print(str(a) + " ...
- ubuntu 16.04下如何打造 sublime python编程环境
一.安装python3 ubuntu自身是安装python2的,例如在ubuntu 16.04中安装的就是python2.7.但我想在python3的环境下进行开发所以就要安装python3. ...
- Developing for nRF52810(转载)
Table of Contents Introduction Hardware emulation of nRF52810 Limitations Software emulation of nRF5 ...