hibernate最重要的就是.hbm.xml對應(yīng)文件,,實體之間的一對一,、一對多和多對多關(guān)系都體現(xiàn)在這個配置文件中,。
struts整合hibernate,先要整合struts,,就是添加struts的JAR包,,然后配置web.xml配置文件,配置struts過濾器,,然后在添加hibernate的JAR包,,添加hibernate主配置文件hibernate.cfg.xml,然后根據(jù)類與表的對應(yīng)關(guān)系配置相應(yīng)的hbm.xml文件,。
hibernate提供了一種HQL查詢語言——Hibernate Query Language,,HQL面向的是對象而不是數(shù)據(jù)庫中的表,這是與SQL(Structured Query Language)之間的差別
使用struts,、hibernate開發(fā)一個注冊信息的小程序:一個注冊頁面,,提交后顯示所有用戶信息,單擊其中一個用戶名,,顯示單用戶詳細信息,,每行后面有update和delete鏈接,單擊update,,顯示單用戶信息頁,,同時可以修改密碼,年齡,,單擊delete,,刪除此條記錄,如下:
是對上一個程序的完善:
程序的開發(fā)包結(jié)構(gòu):
注冊頁面調(diào)用action,,action調(diào)用service,,service調(diào)用DAO
1、頁面部分
注冊頁面register.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 'register.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="savePerson.action">
- username:<input type="text" name="username" size="20"/><br/>
- password:<input type="password" name="password" size="20"/><br/>
- age:<input type="text" name="age" size="20"/><br/>
-
- <input type="submit" value="submit"/>
- </form>
- </body>
- </html>
提交后顯示listAll.jsp:
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- 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 'listAll.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>
- <table width="80%" align="center" border="1">
- <tr>
- <th>username</th>
- <th>password</th>
- <th>age</th>
- <th>registerDate</th>
- <th>update</th>
- <th>delete</th>
- </tr>
- <s:iterator value="#request.list" id="person">
- <tr>
- <td>
- <s:a href="getsinglePerson.action?id=%{#person.id}"><s:property value="username" /></s:a>
- </td>
- <td>
- <s:property value="password" />
- </td>
- <td>
- <s:property value="age" />
- </td>
- <td>
- <s:property value="registerDate" />
- </td>
-
- <td>
- <s:a href="updatePPerson.action?id=%{#person.id}">update</s:a>
- </td>
- <td>
- <s:a href="deletePerson.action?id=%{#person.id}">delete</s:a>
- </td>
- </tr>
-
- </s:iterator>
-
-
- </table>
- </body>
- </html>
點擊用戶名顯示單戶詳細信息頁面getsinglePerson.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- 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 'getsinglePerson.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>
- username:<s:property value="#request.person.username"/><br/>
- password:<s:property value="#request.person.password"/><br/>
- age:<s:property value="#request.person.age"/><br/>
- registerDate:<s:property value="#request.person.registerDate"/><br/>
- </body>
- </html>
點擊update進入單戶信息修改頁面updatePerson.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <%
- 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 'updatePerson.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="updatePerson.action">
-
- username:<s:textfield name="username" value="%{#request.person.username}" readonly="true"></s:textfield><br/>
- password:<s:password name="password" value="%{#request.person.password}"></s:password><br/>
- age:<s:textfield name="age" value="%{#request.person.age}"></s:textfield><br/>
- registerDate:<s:textfield name="registerDate" value="%{#request.person.registerDate}" readonly="true"></s:textfield><br/>
- <s:hidden name="id" value="%{#request.person.id}"></s:hidden>
- <input type="submit" value="submit"/>
- </form>
- </body>
- </html>
2,、struts的action部分,,這里只是用了一個action:PersonAction.java
3、struts的配置文件:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts./dtds/struts-2.3.dtd">
-
- <struts>
-
- <package name="hibernate" extends="struts-default">
-
- <action name="savePerson" class="com.cdtax.action.PersonAction" method="savePerson">
- <result name="success">/listAll.jsp</result>
- </action>
- <action name="deletePerson" class="com.cdtax.action.PersonAction" method="deletePerson">
- <result name="success">/listAll.jsp</result>
- </action>
- <action name="getsinglePerson" class="com.cdtax.action.PersonAction" method="getsinglePerson">
- <result name="success">/getsinglePerson.jsp</result>
- </action>
- <action name="updatePPerson" class="com.cdtax.action.PersonAction" method="getsinglePerson">
- <result name="success">/updatePerson.jsp</result>
- </action>
- <action name="updatePerson" class="com.cdtax.action.PersonAction" method="updatePerson">
- <result name="success">/listAll.jsp</result>
- </action>
- </package>
-
-
- </struts>
4,、service部分,,包括接口和實現(xiàn)
- import java.util.List;
-
- import com.cdtax.model.Person;
-
- public interface PersonService
- {
- public void savePerson(Person person);
-
- public List<Person> listAllPersons();
-
- public void removePerson(Integer id);
-
- public Person getsinglePerson(Integer id);
-
- public void updatePerson(Person person);
- }
- import java.util.List;
-
- import com.cdtax.dao.PersonDAO;
- import com.cdtax.dao.impl.PersonDAOImpl;
- import com.cdtax.model.Person;
- import com.cdtax.service.PersonService;
-
- public class PersonServiceImpl implements PersonService
- {
-
- public void savePerson(Person person)
- {
- PersonDAO personDAO = new PersonDAOImpl();
- personDAO.savePerson(person);
- }
-
- public List<Person> listAllPersons()
- {
- PersonDAO personDAO = new PersonDAOImpl();
-
- return personDAO.listAllPersons();
- }
-
- public void removePerson(Integer id)
- {
- PersonDAO personDAO = new PersonDAOImpl();
-
- personDAO.removePerson(id);
- }
-
- public Person getsinglePerson(Integer id)
- {
- PersonDAO personDAO = new PersonDAOImpl();
-
- return personDAO.getsinglePerson(id);
- }
-
- public void updatePerson(Person person)
- {
- PersonDAO personDAO = new PersonDAOImpl();
-
- personDAO.updatePerson(person);
- }
-
- }
5、DAO部分,,包括接口和實現(xiàn)
- import java.util.List;
-
- import com.cdtax.model.Person;
-
- public interface PersonDAO
- {
- public void savePerson(Person person);
-
- public List<Person> listAllPersons();
-
- public void removePerson(Integer id);
-
- public Person getsinglePerson(Integer id);
-
- public void updatePerson(Person person);
- }
6、輔助部分:
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
-
- public class HibernateUtil
- {
- private static SessionFactory sessionFactory;
-
- static
- {
- try
- {
- sessionFactory = new Configuration().configure().buildSessionFactory();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
-
- public static Session openSession()
- {
- Session session = sessionFactory.openSession();
-
- return session;
- }
-
- public static void close(Session session)
- {
- if(null !=session)
- {
- session.close();
- }
- }
- }
7,、關(guān)于session的get與load方法都可以獲取相應(yīng)的持久化對象,,如果該對象存在,,那么這兩個方法的行為是一樣的;如果該對象不存在,,那么get方法返回null而load方法則拋出異常,。
關(guān)于頁面中使用了OGNL表達式,,主要應(yīng)注意#以及%{#}的用法,,什么時候用#,什么時候用%{}
hibernate持久化主要體現(xiàn)在session的save(),,get(),,delete(),update(),,createQuery()上,。
Transaction tx = session.beginTransaction();主要作用就是執(zhí)行conn.setAutoCommit(false);對于使用jdbc直接進行數(shù)據(jù)庫操作,默認一個語句執(zhí)行是自動提交的,,如果我們有一組sql語句要作為一個整體執(zhí)行,,就需要取消自動提交,如有:
sql1
sql2
sql3
前面加上Transaction tx = session.beginTransaction();,,就相當(dāng)于:
conn.setAutoCommit(false),;
sql1;
sql2,;
sql3,;
conn.commit();
|