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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate和jsp做数据库单表的增删改查相关的知识,希望对你有一定的参考价值。

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

1.最重要的。Java文件

技术分享
 1 package com.chinasofti.hibb.struts;
 2 
 3 
 4 import org.hibernate.Session;
 5 import org.hibernate.SessionFactory;
 6 import org.hibernate.Transaction;
 7 import org.hibernate.cfg.Configuration;
 8 import com.chinasofit.hibb.entity.Test;
 9 import com.opensymphony.xwork2.ActionSupport;
10 
11 
12 
13 @SuppressWarnings("serial")
14 public class HibbTest extends ActionSupport {
15     private Test test;
16     private int id;
17 
18     public Test getTest() {
19         return test;
20     }
21     public void setTest(Test test) {
22         this.test = test;
23     }
24     public int getId() {
25         return id;
26     }
27     public void setId(int id) {
28         this.id = id;
29     }
30     public Session session(){
31     Configuration config = new Configuration().configure();
32     SessionFactory factory = config.buildSessionFactory();
33     Session session = factory.openSession();
34     return session;
35     }
36     //1.查找
37     public String testchazhao(){
38         //int id= (Integer) session.getAttribute("id");可以直接获取页面上输入的ID,不用再获取什么的;
39         Session session=session();
40         test = (Test) session.get(Test.class, id);
41         session.close();
42         return "testchazhao";
43     }
44     //2.添加
45     public String Zhuce(){
46      //   Session session1 = HibernateSessionFactory.getSession();
47         Session session=session();
48         Transaction tes = session.beginTransaction();        
49         session.save(test);
50         //id=test.getId();
51         tes.commit();  //提交事务
52     //    tes.rollback();//事务回滚
53         session.close(); //关闭Session对象
54         return "zhuce";
55     }
56    //3.更改
57     public String update(){
58         //1.查找出id
59         Session session =session();
60         Transaction tx = session.beginTransaction();
61         test = (Test)session.get(Test.class, id);
62         tx.commit();
63         session.close();
64         //2.更改
65         test.setName("王二小");
66         Session session1 = session();
67         Transaction tx1 = session1.beginTransaction();
68         session1.update(test);
69         tx1.commit();
70         session1.close();
71         return "genggai";
72     }
73     //4.删除,根据id 
74     public String delete(){
75         Session session =session();
76         Transaction tx = session.beginTransaction();
77         test = (Test)session.get(Test.class, id);
78         session.delete(test);
79         tx.commit();
80         session.close();
81         return "shanchu";
82     }
83 }
84 
85     
86     
87     
88     
89     
90     
91     
View Code

2.就是test路径

技术分享
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 3 <struts>
 4 
 5     <package name="Ttest" namespace="/" extends="struts-default">
 6         <!-- 查找 -->
 7         <action name="Hibb" class="com.chinasofti.hibb.struts.HibbTestId" >
 8             <result name="hibbtestid"   type="dispatcher">/testchaid.jsp</result>
 9         </action>            
10         <action name="HibbTest" class="com.chinasofti.hibb.struts.HibbTest" method="testchazhao">
11             <result name="testchazhao" type="dispatcher">/testchazhao.jsp</result>
12         </action>    
13         <!-- 增加 -->
14         <action name="Zhuce" class="com.chinasofti.hibb.struts.HibbTestzhuceOk" >
15             <result name="zhuceok" type="dispatcher">/testzhuce.jsp</result>
16         </action>    
17         <action name="ZhuceOk" class="com.chinasofti.hibb.struts.HibbTest" method="Zhuce">
18             <result name="zhuce" type="dispatcher">/testchazhao.jsp</result>
19         </action>
20         <!-- 修改 -->
21         <action name="Gengai" class="com.chinasofti.hibb.struts.HibbTestgengai" >
22         <result name="hibbtestgenggai" type="dispatcher">/testgenggaiid.jsp</result>
23         </action>    
24         <action name="GengaiOk" class="com.chinasofti.hibb.struts.HibbTest" method="update">
25         <result name="genggai" type="dispatcher">/testchazhao.jsp</result>
26         </action>
27         
28         <!-- 删除 -->
29         <action name="Shanchu" class="com.chinasofti.hibb.struts.HibbTestshanchu" >
30         <result name="hibbtestshanchu" type="dispatcher">/testshanchu.jsp</result>
31         </action>    
32         <action name="ShanchuOk" class="com.chinasofti.hibb.struts.HibbTest" method="delete">
33         <result name="shanchu" type="dispatcher">/testchazhao.jsp</result>
34         </action>
35         
36         
37         
38             
39    </package>
40 </struts>    
View Code

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

技术分享
1 package com.chinasofti.hibb.struts;
2 
3 public class HibbTestgengai {
4     public String execute(){
5           return "hibbtestgenggai";
6         }
7 }
View Code
技术分享
1 package com.chinasofti.hibb.struts;
2 
3 public class HibbTestzhuceOk {
4     public String execute(){
5           return "zhuceok";
6         }
7 
8 }
View Code
技术分享
1 package com.chinasofti.hibb.struts;
2 
3 public class HibbTestshanchu {
4     public String execute(){
5           return "hibbtestshanchu";
6         }
7 }
View Code
技术分享
1 package com.chinasofti.hibb.struts;
2 
3 public class HibbTestId {
4     public String execute(){
5       return "hibbtestid";
6     }
7     
8 }
View Code

4.基本上在Struts里,最后跳转的都是查找JSP界面,这样可以知道自己到底对数据库做了什么

技术分享
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP ‘testzhuce.jsp‘ starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26   <form action="ZhuceOk.action" method="post" >
27   <h2>注册界面:</h2>
28     <table border="0" align="center">
29   <tr>
30   <td>  姓名:</td>
31    <td><input type="text" name ="test.name"/></td>
32   </tr>
33     <tr>
34   <td>  密码:</td>
35   <td><input type="text" name ="test.pwd"></td>
36   </tr>
37     <tr>
38   <td>email:</td>
39   <td><input type ="text" name = "test.email"></td>
40   </tr>
41     <tr>
42   <td>   出生日期:</td>
43   <td><input type ="text" name = "test.born"></td>
44   </tr>
45     <tr>
46   <td> 性别:</td>
47   <td><input type ="text" name = "test.sex"></td>
48   </tr>
49     <tr>
50   <td><input type ="submit" value="注册"></td>
51   <td><input type ="reset" value="取消"></td>
52   </tr>
53   </table>
54   </form>
55   </body>
56 </html>
View Code
技术分享
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP ‘testshanchu.jsp‘ starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26      <h2>删除</h2> 
27     <form action="ShanchuOk.action" method="post" >
28    <table align="center">
29    <tr>
30    <td>请输入要删除的ID:</td>
31    <td><input type="text" name="id"></td>
32    </tr>
33    <tr>
34    <td><input type="submit" value="确认"></td>
35    </tr>
36    </table>
37    </form>
38   </body>
39 </html>
View Code
技术分享
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP ‘testgenggaiid.jsp‘ starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26    <h2>更改</h2> 
27     <form action="GengaiOk.action" method="post" >
28    <table align="center">
29    <tr>
30    <td>请输入要更改的ID:</td>
31    <td><input type="text" name="id"></td>
32    </tr>
33    <tr>
34    <td><input type="submit" value="确认"></td>
35    </tr>
36    </table>
37    </form>
38   </body>
39 </html>
View Code
技术分享
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP ‘test.jsp‘ starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <h2>查找:</h2>
27   <table border="0" align="center">
28       <tr>
29   <td>id:</td>
30   <td>${test.id}</td>
31   </tr>
32   
33   <tr>
34   <td>  姓名:</td>
35   <td>${test.name}</td>
36   </tr>
37     <tr>
38   <td>  密码:</td>
39   <td>${test.pwd}</td>
40   </tr>
41     <tr>
42   <td>email:</td>
43   <td>${test.email }</td>
44   </tr>
45     <tr>
46   <td>   出生日期:</td>
47   <td>${test.born }</td>
48   </tr>
49     <tr>
50   <td> 性别:</td>
51   <td>${test.sex}</td>
52   </tr>
53 
54   </table>
55  
56   </body>
57 </html>
View Code
技术分享
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP ‘testchaid.jsp‘ starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26   <form action="HibbTest.action" method="post" >
27    <table align="center">
28    <tr>
29    <td>请输入要查找的ID:</td>
30    <td><input type="text" name="id"></td>
31    </tr>
32    <tr>
33    <td><input type="submit" value="确认"></td>
34    </tr>
35    </table>
36    </form>
37   </body>
38 </html>
View Code

 

以上是关于Hibernate和jsp做数据库单表的增删改查的主要内容,如果未能解决你的问题,请参考以下文章

框架[Hibernate]利用Hibernate进行单表的增删改查-Web实例

mybatis 单表的增删改查

MYSQL基础操作之单表的增删改查

单表的增删改查实例(图书管理 系统)

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

Django之单表的增删改查