Hibernate和jsp做数据库单表的增删改查

时间:2022-11-25 14:33:57

虽然很基础,但是我转牛角尖了~~~~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>