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 ...
随机推荐
- 使用openssl实现ECDSA签名以及验证功能(附完整测试源码)
突然找到数年前写的这段代码,当是因为对密码学几乎不怎么了解踩了一些坑,现在开源出来方便大家直接利用. ECDSA的全名是Elliptic Curve DSA,也就是椭圆曲线DSA,由于椭圆曲线的复杂性 ...
- 2016 Multi-University Training Contest 2 - 1005 Eureka
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题目大意:给定平面上的n个点,一个集合合法当且仅当集合中存在一对点u,v,对于集合中任意点w,均 ...
- WAP端 经验记录2
1. LightboxV2 插件 点击A 应该关闭弹层的效果,但是 SAMSUNG 手机上原生浏览器上,看上去不会关闭却跳转了,但当点击回退按钮的时候就会看见弹层已经消失(其实之前的关闭效果已经记录了 ...
- 【原】如何在jQuery中实现闭包
原生JS中,闭包虽好用,但是很难用好,在jQuery中一样,都有一些点需要我们注意.jQuery中使用闭包的常见情况有以下几种: 1.$(document).ready()的参数 我们在写jQuery ...
- iOS 开发 常用的正则验证表达式:电话 、邮箱等等
#pragma mark - 验证手机号 +(BOOL)checkForMobilePhoneNo:(NSString *)mobilePhone{ NSString *regEx = @" ...
- thinkphp学习简易教程(一) thinkphp创建项目
1.在本地服务器根目录下创建项目目录,如命名为app: 2.把thinkphp的压缩包解压到与项目同级目录下,如app是放在目录APP下面,即项目目录路径为'APP/app/',则thinkphp应解 ...
- 三表联查,这是我目前写过的最长的sql语句,嗯嗯,果然遇到问题才能让我更快成长,更复杂的语句也有了一些心得了
select sum(amount),sum(card_number) from sy_user inner join sy_admin on sy_user.customer_id=sy_admin ...
- postgres 批量更新内容
在程序中遇到这样的需求, 数据库表格式如下 需要把批量更新status, 如name = fox 时, status = 1, name = boa 时,status = 2 .... 类似的 pos ...
- Tesseract API在VS 2013中的配置以及调用
[Tesseract]Tesseract API在VS 2013中的配置以及调用 时间:2016-05-31 20:35:19 阅读:127 评论:0 收藏:0 ...
- [原创]AD9212采样方法
随记 最近由于工程原因用到ADC的采样,选用了ADI公司的AD9212芯片,八通道10位ADC.在进行ADC的采样时,看到的想到的几种方法,在这里做个笔记记录一下. AD9212简介 详细说明可以在A ...