虽然很基础,但是我转牛角尖了~~~~这是几个文件

1.最重要的。Java文件

 package com.chinasofti.hibb.struts;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.chinasofit.hibb.entity.Test;
import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial")
public class HibbTest extends ActionSupport {
private Test test;
private int id; public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Session session(){
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
return session;
}
//1.查找
public String testchazhao(){
//int id= (Integer) session.getAttribute("id");可以直接获取页面上输入的ID,不用再获取什么的;
Session session=session();
test = (Test) session.get(Test.class, id);
session.close();
return "testchazhao";
}
//2.添加
public String Zhuce(){
// Session session1 = HibernateSessionFactory.getSession();
Session session=session();
Transaction tes = session.beginTransaction();
session.save(test);
//id=test.getId();
tes.commit(); //提交事务
// tes.rollback();//事务回滚
session.close(); //关闭Session对象
return "zhuce";
}
//3.更改
public String update(){
//1.查找出id
Session session =session();
Transaction tx = session.beginTransaction();
test = (Test)session.get(Test.class, id);
tx.commit();
session.close();
//2.更改
test.setName("王二小");
Session session1 = session();
Transaction tx1 = session1.beginTransaction();
session1.update(test);
tx1.commit();
session1.close();
return "genggai";
}
//4.删除,根据id
public String delete(){
Session session =session();
Transaction tx = session.beginTransaction();
test = (Test)session.get(Test.class, id);
session.delete(test);
tx.commit();
session.close();
return "shanchu";
}
}

2.就是test路径

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts> <package name="Ttest" namespace="/" extends="struts-default">
<!-- 查找 -->
<action name="Hibb" class="com.chinasofti.hibb.struts.HibbTestId" >
<result name="hibbtestid" type="dispatcher">/testchaid.jsp</result>
</action>
<action name="HibbTest" class="com.chinasofti.hibb.struts.HibbTest" method="testchazhao">
<result name="testchazhao" type="dispatcher">/testchazhao.jsp</result>
</action>
<!-- 增加 -->
<action name="Zhuce" class="com.chinasofti.hibb.struts.HibbTestzhuceOk" >
<result name="zhuceok" type="dispatcher">/testzhuce.jsp</result>
</action>
<action name="ZhuceOk" class="com.chinasofti.hibb.struts.HibbTest" method="Zhuce">
<result name="zhuce" type="dispatcher">/testchazhao.jsp</result>
</action>
<!-- 修改 -->
<action name="Gengai" class="com.chinasofti.hibb.struts.HibbTestgengai" >
<result name="hibbtestgenggai" type="dispatcher">/testgenggaiid.jsp</result>
</action>
<action name="GengaiOk" class="com.chinasofti.hibb.struts.HibbTest" method="update">
<result name="genggai" type="dispatcher">/testchazhao.jsp</result>
</action> <!-- 删除 -->
<action name="Shanchu" class="com.chinasofti.hibb.struts.HibbTestshanchu" >
<result name="hibbtestshanchu" type="dispatcher">/testshanchu.jsp</result>
</action>
<action name="ShanchuOk" class="com.chinasofti.hibb.struts.HibbTest" method="delete">
<result name="shanchu" type="dispatcher">/testchazhao.jsp</result>
</action> </package>
</struts>

3.增删改查访问的路径都是通过ID,所以需要几个可以跳转ID的JSP和action。

 package com.chinasofti.hibb.struts;

 public class HibbTestgengai {
public String execute(){
return "hibbtestgenggai";
}
}
 package com.chinasofti.hibb.struts;

 public class HibbTestzhuceOk {
public String execute(){
return "zhuceok";
} }
 package com.chinasofti.hibb.struts;

 public class HibbTestshanchu {
public String execute(){
return "hibbtestshanchu";
}
}
 package com.chinasofti.hibb.struts;

 public class HibbTestId {
public String execute(){
return "hibbtestid";
} }

4.基本上在Struts里,最后跳转的都是查找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 'testzhuce.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="ZhuceOk.action" method="post" >
<h2>注册界面:</h2>
<table border="0" align="center">
<tr>
<td> 姓名:</td>
<td><input type="text" name ="test.name"/></td>
</tr>
<tr>
<td> 密码:</td>
<td><input type="text" name ="test.pwd"></td>
</tr>
<tr>
<td>email:</td>
<td><input type ="text" name = "test.email"></td>
</tr>
<tr>
<td> 出生日期:</td>
<td><input type ="text" name = "test.born"></td>
</tr>
<tr>
<td> 性别:</td>
<td><input type ="text" name = "test.sex"></td>
</tr>
<tr>
<td><input type ="submit" value="注册"></td>
<td><input type ="reset" value="取消"></td>
</tr>
</table>
</form>
</body>
</html>
 <%@ 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 'testshanchu.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>
<h2>删除</h2>
<form action="ShanchuOk.action" method="post" >
<table align="center">
<tr>
<td>请输入要删除的ID:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td><input type="submit" value="确认"></td>
</tr>
</table>
</form>
</body>
</html>
 <%@ 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 'testgenggaiid.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>
<h2>更改</h2>
<form action="GengaiOk.action" method="post" >
<table align="center">
<tr>
<td>请输入要更改的ID:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td><input type="submit" value="确认"></td>
</tr>
</table>
</form>
</body>
</html>
 <%@ 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 'test.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>
<h2>查找:</h2>
<table border="0" align="center">
<tr>
<td>id:</td>
<td>${test.id}</td>
</tr> <tr>
<td> 姓名:</td>
<td>${test.name}</td>
</tr>
<tr>
<td> 密码:</td>
<td>${test.pwd}</td>
</tr>
<tr>
<td>email:</td>
<td>${test.email }</td>
</tr>
<tr>
<td> 出生日期:</td>
<td>${test.born }</td>
</tr>
<tr>
<td> 性别:</td>
<td>${test.sex}</td>
</tr> </table> </body>
</html>
 <%@ 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 'testchaid.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="HibbTest.action" method="post" >
<table align="center">
<tr>
<td>请输入要查找的ID:</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td><input type="submit" value="确认"></td>
</tr>
</table>
</form>
</body>
</html>

Hibernate和jsp做数据库单表的增删改查的更多相关文章

  1. hibernate对单表的增删改查

    ORM: 对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping) 实现对单表的增删改查 向区域表中增加数据: 第一步: 新建一个Da ...

  2. Django学习笔记(10)——Book单表的增删改查页面

    一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...

  3. python全栈开发day61-django简单的出版社网站展示,添加,删除,编辑(单表的增删改查)

    day61 django内容回顾: 1. 下载: pip install django==1.11.14 pip install -i 源 django==1.11.14 pycharm 2. 创建项 ...

  4. MySQL数据库之表的增删改查

    目录 MySQL数据库之表的增删改查 1 引言 2 创建表 3 删除表 4 修改表 5 查看表 6 复制表 MySQL数据库之表的增删改查 1 引言 1.MySQL数据库中,数据库database就是 ...

  5. django模型层 关于单表的增删改查

    关于ORM MTV或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库, 通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员 ...

  6. Django学习笔记--数据库中的单表操作----增删改查

    1.Django数据库中的增删改查 1.添加表和字段 # 创建的表的名字为app的名称拼接类名 class User(models.Model): # id字段 自增 是主键 id = models. ...

  7. Mybatis(一)实现单表的增删改查

    1.1 什么是Mybatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并 ...

  8. 1.SSM整合_单表的增删改查

    目标:增删改查 环境:Maven+Eclipse+Tomcat7+JDK7 思维导图: 表结构 目录结构 依赖 <dependencies> <dependency> < ...

  9. Django中对单表的增删改查

    之前的简单预习,重点在后面 方式一: # create方法的返回值book_obj就是插入book表中的python葵花宝典这本书籍纪录对象   book_obj=Book.objects.creat ...

随机推荐

  1. Python: 元组的基本用法

    元组和列表是近亲,列表允许并且提供了方法来改变它的值,但元组是不可改变的,即不允许你改变它的值----这也是它没有方法的部分原因. 元组的主要作用是作为参数传递给函数调用.或是从函数调用那里获得参数时 ...

  2. lnmp

    参照http://www.osyunwei.com/archives/8867.html,略有改动 一 .系统约定 软件源代码包存放位置:/usr/local/src 源码包编译安装位置:/usr/l ...

  3. 基于AQS的锁

    锁分为独占锁和共享锁,它们的主要实现都是依靠AbstractQueuedSynchronizer,这个类只提供一系列公共的方法,让子类来调用.基于我了解不深,从这个类的属性,方法,和独占锁的获取方式去 ...

  4. 【原】jQuery与CSS自动生成验证码

    模板: <button class="r receive_code">获取验证码</button> <span class="r code& ...

  5. jquery 序列化

    //生成发件人Json信息 function buildSenderInfoJson() { var sendName = $("#SendName").val(); var se ...

  6. Android笔记之——线程

    Java: package com.example.test_ui_thread01;import android.support.v7.app.AppCompatActivity;import an ...

  7. Android Runtime

    [Android Runtime] Every Android application runs in its own process, with its own instance of the Da ...

  8. scala控制结构

    #判断 scala> def min(x:Int,y:Int):Int={ var a=x if(x>y) a=y return a } scala> min(1,2)res1: I ...

  9. java 常见dos命令

    盘符: 进入指定的盘符下. dir : 列出当前目录下的文件以及文件夹 md : 创建目录 rd : 删除目录    注意:rd不能删除非空的文件夹,而且只能用于删除文件夹. cd : 进入指定目录 ...

  10. C语言中的回调函数(Callback Function)

    1 定义和使用场合 回调函数是指 使用者自己定义一个函数,实现这个函数的程序内容,然后把这个函数(入口地址)作为参数传入别人(或系统)的函数中,由别人(或系统)的函数在运行时来调用的函数.函数是你实现 ...