Hibernate和jsp做数据库单表的增删改查
虽然很基础,但是我转牛角尖了~~~~
这是几个文件
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做数据库单表的增删改查的更多相关文章
- hibernate对单表的增删改查
ORM: 对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping) 实现对单表的增删改查 向区域表中增加数据: 第一步: 新建一个Da ...
- Django学习笔记(10)——Book单表的增删改查页面
一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...
- python全栈开发day61-django简单的出版社网站展示,添加,删除,编辑(单表的增删改查)
day61 django内容回顾: 1. 下载: pip install django==1.11.14 pip install -i 源 django==1.11.14 pycharm 2. 创建项 ...
- MySQL数据库之表的增删改查
目录 MySQL数据库之表的增删改查 1 引言 2 创建表 3 删除表 4 修改表 5 查看表 6 复制表 MySQL数据库之表的增删改查 1 引言 1.MySQL数据库中,数据库database就是 ...
- django模型层 关于单表的增删改查
关于ORM MTV或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库, 通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员 ...
- Django学习笔记--数据库中的单表操作----增删改查
1.Django数据库中的增删改查 1.添加表和字段 # 创建的表的名字为app的名称拼接类名 class User(models.Model): # id字段 自增 是主键 id = models. ...
- Mybatis(一)实现单表的增删改查
1.1 什么是Mybatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并 ...
- 1.SSM整合_单表的增删改查
目标:增删改查 环境:Maven+Eclipse+Tomcat7+JDK7 思维导图: 表结构 目录结构 依赖 <dependencies> <dependency> < ...
- Django中对单表的增删改查
之前的简单预习,重点在后面 方式一: # create方法的返回值book_obj就是插入book表中的python葵花宝典这本书籍纪录对象 book_obj=Book.objects.creat ...
随机推荐
- windbg学习----.process
.process 命令指定要用作进程上下文的进程(Set Process Context) .process显示当前进程的EPROCESS,这里显示当前进程为test.exe kd> .proc ...
- MVC 基础和增删改、登录
一.什么是MVC?1.了解MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式: Model(模型)表示应用程序核心 ...
- 百度音乐API抓取
百度音乐API抓取 前段时间做了一个本地音乐的播放器 github地址,想实现在线播放的功能,于是到处寻找API,很遗憾,不是歌曲不全就是质量不高.在网上发现这么一个APIMRASONG博客,有“获取 ...
- Mybatis之关联查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- Delphi 中记录类型 给记录指针赋值。
PPersion=^TPersion; TPersion=packed record Name:string; Sex:string; Clasee:string; end ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
- HDU 1251统计难题
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- deep learning on object detection
回归工作一周,忙的头晕,看了两三篇文章,主要在写各种文档和走各种办事流程了-- 这次来写写object detection最近看的三篇文章吧.都不是最近的文章,但是是今年的文章,我也想借此让自己赶快熟 ...
- Spark standalone HA
配置Spark standalone HA 主机:node1,node2,node3 master: node1,node2 slave:node2,node3 修改配置文件: node1,node3 ...
- 脚本录制(JMeter)
使用JMeter录制脚本: 1.打开JMeter工具,创建一个线程组,接着创建一个http代理服务器,代理服务器设置如下: