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 ...
随机推荐
- LR常用函数以及调用自定义函数
2.LR常用函数以及调用自定义函数 2.1.LR常用函数以及对信息的判断 2.1.1. LR内部自定义函数 在LR脚本中定义变量和编写自定义函数,需将变量的声明放在脚本其他内容的上方,否则会提示[il ...
- js浏览器对象的属性和方法
1.window对象 /*1.计算浏览器窗口大小*/ //不算滚动条: var width = window.innerWidth; var height = window.innerHeight; ...
- python--day2--基础数据类型与变量
笔者:QQ: 360212316 python控制语句 for循环语句示例: for i in range(10): print(i) for循环语句示例1: for i in range(10) ...
- 深入浅出讲解:php的socket通信
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问:1. 什么是TCP/IP.UDP?2. Socke ...
- Opencl 并行求和
上周尝试用opencl求极大值,在网上查到大多是求和,所谓的reduction算法.不过思路是一样的. CPP: ; unsigned ; ; ; int nGroup = nGroupSize / ...
- UML大战需求分析阅读笔记3
UML各种图的中文译名,因为翻译的原因可能会有所不一样,如:Sequence Diagram和Timing Diagram有时候都会被译成"时序图",这是最让人困扰的地方!Sequ ...
- 在做基于LBS应用的一些随笔
公司做了一个基于LBS的APP,在做服务端的时候出现了一些注意事项,还是记录下把. 首先是关于坐标: 弧长公式:L=nπr/180°或l=|α|r.地球半径大致是6400千米.以纬度0.000001为 ...
- [转]Direct3D 11 Tessellation Tutorial
The new hardware tessellation feature available on Direct3D 11 video cards has great potential, but ...
- 初学c# -- 学习笔记(五) winfrom无边框四周阴影
刚用到这个功能,网上扯淡的东西太多了,都是2边阴影,还什么窗口叠加.ps作图啥的,什么玩意.还是老外实在,google找的,无边框窗体,四边透明阴影. public partial class For ...
- svn:ignore eclipse开发一般忽略文件
target.project.classpath.settings