久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

3,、struts整合hibernate

 昵稱10087950 2016-08-17

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,,刪除此條記錄,如下:

username password age registerDate update delete
zdy qwwqwq 30 13-8-27 update delete
rte qwer 12 13-8-28 update delete
tty qw 21 13-8-28 update delete
ertty w33 32 13-8-28 update delete
ertty 222222 3211 13-8-28 update delete

 

是對上一個程序的完善:

程序的開發(fā)包結(jié)構(gòu):

注冊頁面調(diào)用action,,action調(diào)用service,,service調(diào)用DAO

1、頁面部分

注冊頁面register.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 'register.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="savePerson.action">  
  27.     username:<input type="text" name="username" size="20"/><br/>  
  28.     password:<input type="password" name="password" size="20"/><br/>  
  29.     age:<input type="text" name="age" size="20"/><br/>  
  30.       
  31.     <input type="submit" value="submit"/>  
  32.     </form>  
  33.   </body>  
  34. </html>  


提交后顯示listAll.jsp:

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>My JSP 'listAll.jsp' starting page</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.     <!-- 
  21.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  22.     -->  
  23.   
  24.   </head>  
  25.     
  26.   <body>  
  27.     <table width="80%" align="center" border="1">  
  28.     <tr>  
  29.     <th>username</th>  
  30.     <th>password</th>  
  31.     <th>age</th>  
  32.     <th>registerDate</th>  
  33.     <th>update</th>  
  34.     <th>delete</th>  
  35.     </tr>  
  36.     <s:iterator value="#request.list" id="person">  
  37.     <tr>  
  38.     <td>  
  39.         <s:a href="getsinglePerson.action?id=%{#person.id}"><s:property value="username" /></s:a>  
  40.     </td>  
  41.     <td>  
  42.         <s:property value="password" />  
  43.     </td>  
  44.     <td>  
  45.         <s:property value="age" />  
  46.     </td>  
  47.     <td>  
  48.         <s:property value="registerDate" />  
  49.     </td>  
  50.       
  51.     <td>  
  52.         <s:a href="updatePPerson.action?id=%{#person.id}">update</s:a>  
  53.     </td>  
  54.     <td>  
  55.         <s:a href="deletePerson.action?id=%{#person.id}">delete</s:a>  
  56.     </td>  
  57.     </tr>  
  58.       
  59.     </s:iterator>  
  60.       
  61.       
  62.     </table>  
  63.   </body>  
  64. </html>  


點擊用戶名顯示單戶詳細信息頁面getsinglePerson.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>My JSP 'getsinglePerson.jsp' starting page</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.     <!-- 
  21.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  22.     -->  
  23.   
  24.   </head>  
  25.     
  26.   <body>  
  27.     username:<s:property value="#request.person.username"/><br/>  
  28.     password:<s:property value="#request.person.password"/><br/>  
  29.     age:<s:property value="#request.person.age"/><br/>  
  30.     registerDate:<s:property value="#request.person.registerDate"/><br/>  
  31.   </body>  
  32. </html>  


點擊update進入單戶信息修改頁面updatePerson.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>My JSP 'updatePerson.jsp' starting page</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.     <!-- 
  21.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  22.     -->  
  23.   
  24.   </head>  
  25.     
  26.   <body>  
  27.      <form action="updatePerson.action">  
  28.        
  29.      username:<s:textfield name="username" value="%{#request.person.username}" readonly="true"></s:textfield><br/>  
  30.      password:<s:password name="password" value="%{#request.person.password}"></s:password><br/>  
  31.      age:<s:textfield name="age" value="%{#request.person.age}"></s:textfield><br/>  
  32.      registerDate:<s:textfield name="registerDate" value="%{#request.person.registerDate}" readonly="true"></s:textfield><br/>  
  33.      <s:hidden name="id" value="%{#request.person.id}"></s:hidden>  
  34.      <input type="submit" value="submit"/>  
  35.      </form>  
  36.   </body>  
  37. </html>  

2,、struts的action部分,,這里只是用了一個action:PersonAction.java

  1. import java.util.List;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5. import org.apache.struts2.ServletActionContext;  
  6. import org.hibernate.Session;  
  7.   
  8. import com.cdtax.model.Person;  
  9. import com.cdtax.service.PersonService;  
  10. import com.cdtax.service.impl.PersonServiceImpl;  
  11. import com.cdtax.util.HibernateUtil;  
  12. import com.opensymphony.xwork2.ActionSupport;  
  13.   
  14. public class PersonAction extends ActionSupport  
  15. {  
  16.     private int id;  
  17.       
  18.     private String username;  
  19.       
  20.     private String password;  
  21.       
  22.     private int age;  
  23.       
  24.       
  25.   
  26.     public int getId()  
  27.     {  
  28.         return id;  
  29.     }  
  30.   
  31.     public void setId(int id)  
  32.     {  
  33.         this.id = id;  
  34.     }  
  35.   
  36.     public String getUsername()  
  37.     {  
  38.         return username;  
  39.     }  
  40.   
  41.     public void setUsername(String username)  
  42.     {  
  43.         this.username = username;  
  44.     }  
  45.   
  46.     public String getPassword()  
  47.     {  
  48.         return password;  
  49.     }  
  50.   
  51.     public void setPassword(String password)  
  52.     {  
  53.         this.password = password;  
  54.     }  
  55.   
  56.     public int getAge()  
  57.     {  
  58.         return age;  
  59.     }  
  60.   
  61.     public void setAge(int age)  
  62.     {  
  63.         this.age = age;  
  64.     }  
  65.       
  66.     public String savePerson() throws Exception  
  67.     {  
  68.         Person person = new Person();  
  69.           
  70.         person.setUsername(username);  
  71.         person.setPassword(password);  
  72.         person.setAge(age);  
  73.           
  74.         java.sql.Date registerDate = new java.sql.Date(new java.util.Date().getTime());  
  75.           
  76.         person.setRegisterDate(registerDate);  
  77.           
  78.         PersonService personService = new PersonServiceImpl();  
  79.           
  80.         personService.savePerson(person);  
  81.           
  82.         List<Person> list = personService.listAllPersons();  
  83.           
  84.         HttpServletRequest request = ServletActionContext.getRequest();  
  85.           
  86.         request.setAttribute("list", list);  
  87.           
  88.         return SUCCESS;  
  89.     }  
  90.       
  91.     public String deletePerson() throws Exception  
  92.     {  
  93.           
  94.         PersonService personService = new PersonServiceImpl();  
  95.           
  96.         personService.removePerson(id);  
  97.           
  98.         List<Person> list = personService.listAllPersons();  
  99.           
  100.         HttpServletRequest request = ServletActionContext.getRequest();  
  101.           
  102.         request.setAttribute("list", list);  
  103.           
  104.         return SUCCESS;  
  105.     }  
  106.       
  107.     public String getsinglePerson() throws Exception  
  108.     {  
  109.         PersonService personService = new PersonServiceImpl();  
  110.           
  111.         Person person = personService.getsinglePerson(id);  
  112.           
  113.         HttpServletRequest request = ServletActionContext.getRequest();  
  114.           
  115.         request.setAttribute("person", person);  
  116.           
  117.         return SUCCESS;  
  118.     }  
  119.       
  120.     public String updatePerson() throws Exception  
  121.     {  
  122.         PersonService personService = new PersonServiceImpl();  
  123.           
  124.         Person person = personService.getsinglePerson(id);  
  125.           
  126.         person.setPassword(password);  
  127.           
  128.         person.setAge(age);  
  129.           
  130.         personService.updatePerson(person);  
  131.           
  132.         List<Person> list = personService.listAllPersons();  
  133.           
  134.         HttpServletRequest request = ServletActionContext.getRequest();  
  135.           
  136.         request.setAttribute("list", list);  
  137.           
  138.         return SUCCESS;  
  139.     }  
  140. }  

 

3、struts的配置文件:

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts./dtds/struts-2.3.dtd">  
  5.   
  6. <struts>  
  7.       
  8.     <package name="hibernate" extends="struts-default">  
  9.           
  10.         <action name="savePerson" class="com.cdtax.action.PersonAction" method="savePerson">  
  11.             <result name="success">/listAll.jsp</result>  
  12.         </action>  
  13.         <action name="deletePerson" class="com.cdtax.action.PersonAction" method="deletePerson">  
  14.             <result name="success">/listAll.jsp</result>  
  15.         </action>  
  16.         <action name="getsinglePerson" class="com.cdtax.action.PersonAction" method="getsinglePerson">  
  17.             <result name="success">/getsinglePerson.jsp</result>  
  18.         </action>  
  19.         <action name="updatePPerson" class="com.cdtax.action.PersonAction" method="getsinglePerson">  
  20.             <result name="success">/updatePerson.jsp</result>  
  21.         </action>  
  22.         <action name="updatePerson" class="com.cdtax.action.PersonAction" method="updatePerson">  
  23.             <result name="success">/listAll.jsp</result>  
  24.         </action>  
  25.     </package>  
  26.       
  27.       
  28. </struts>  


4,、service部分,,包括接口和實現(xiàn)

  1. import java.util.List;  
  2.   
  3. import com.cdtax.model.Person;  
  4.   
  5. public interface PersonService  
  6. {  
  7.     public void savePerson(Person person);  
  8.       
  9.     public List<Person> listAllPersons();  
  10.       
  11.     public void removePerson(Integer id);  
  12.       
  13.     public Person getsinglePerson(Integer id);  
  14.       
  15.     public void updatePerson(Person person);  
  16. }  


 

  1. import java.util.List;  
  2.   
  3. import com.cdtax.dao.PersonDAO;  
  4. import com.cdtax.dao.impl.PersonDAOImpl;  
  5. import com.cdtax.model.Person;  
  6. import com.cdtax.service.PersonService;  
  7.   
  8. public class PersonServiceImpl implements PersonService  
  9. {  
  10.   
  11.     public void savePerson(Person person)  
  12.     {  
  13.         PersonDAO personDAO = new PersonDAOImpl();  
  14.         personDAO.savePerson(person);  
  15.     }  
  16.       
  17.     public List<Person> listAllPersons()  
  18.     {  
  19.         PersonDAO personDAO = new PersonDAOImpl();  
  20.           
  21.         return personDAO.listAllPersons();  
  22.     }  
  23.       
  24.     public void removePerson(Integer id)  
  25.     {  
  26.         PersonDAO personDAO = new PersonDAOImpl();  
  27.           
  28.         personDAO.removePerson(id);  
  29.     }  
  30.       
  31.     public Person getsinglePerson(Integer id)  
  32.     {  
  33.         PersonDAO personDAO = new PersonDAOImpl();  
  34.           
  35.         return personDAO.getsinglePerson(id);  
  36.     }  
  37.       
  38.     public void updatePerson(Person person)  
  39.     {  
  40.         PersonDAO personDAO = new PersonDAOImpl();  
  41.           
  42.         personDAO.updatePerson(person);  
  43.     }  
  44.   
  45. }  


5、DAO部分,,包括接口和實現(xiàn)

  1. import java.util.List;  
  2.   
  3. import com.cdtax.model.Person;  
  4.   
  5. public interface PersonDAO  
  6. {  
  7.     public void savePerson(Person person);  
  8.       
  9.     public List<Person> listAllPersons();  
  10.       
  11.     public void removePerson(Integer id);  
  12.       
  13.     public Person getsinglePerson(Integer id);  
  14.       
  15.     public void updatePerson(Person person);  
  16. }  


 

  1. import java.util.List;  
  2.   
  3. import org.hibernate.Query;  
  4. import org.hibernate.Session;  
  5. import org.hibernate.Transaction;  
  6.   
  7. import com.cdtax.dao.PersonDAO;  
  8. import com.cdtax.model.Person;  
  9. import com.cdtax.util.HibernateUtil;  
  10.   
  11. public class PersonDAOImpl implements PersonDAO  
  12. {  
  13.   
  14.     public void savePerson(Person person)  
  15.     {  
  16.         Session session = HibernateUtil.openSession();  
  17.           
  18.         Transaction tx = session.beginTransaction();  
  19.           
  20.         try  
  21.         {  
  22.             session.save(person);  
  23.               
  24.             tx.commit();  
  25.         }  
  26.         catch(Exception ex)  
  27.         {  
  28.             if(null != tx)  
  29.             {  
  30.                 tx.rollback();  
  31.             }  
  32.             ex.printStackTrace();  
  33.         }  
  34.         finally  
  35.         {  
  36.             HibernateUtil.close(session);  
  37.         }  
  38.     }  
  39.       
  40.     public List<Person> listAllPersons()  
  41.     {  
  42.         Session session = HibernateUtil.openSession();  
  43.         Transaction tx = session.beginTransaction();  
  44.           
  45.         List<Person> list = null;  
  46.           
  47.         try  
  48.         {  
  49.             Query query = session.createQuery("from Person"); //Person是類的名字而不是表的名字,,需要嚴格區(qū)分大小寫  
  50.               
  51.             list = (List<Person>)query.list();  
  52.               
  53.             tx.commit();  
  54.         }  
  55.         catch(Exception ex)  
  56.         {  
  57.             if(null != tx)  
  58.             {  
  59.                 tx.rollback();  
  60.             }  
  61.         }  
  62.         finally  
  63.         {  
  64.             HibernateUtil.close(session);  
  65.         }  
  66.         return list;  
  67.     }  
  68.       
  69.     public void removePerson(Integer id)  
  70.     {  
  71.         Session session = HibernateUtil.openSession();  
  72.         Transaction tx = session.beginTransaction();  
  73.           
  74.         try  
  75.         {  
  76.             Person person = (Person)session.get(Person.class,id);  
  77.               
  78.             session.delete(person);  
  79.               
  80.             tx.commit();  
  81.         }  
  82.         catch(Exception ex)  
  83.         {  
  84.   
  85.             if(null != tx)  
  86.             {  
  87.                 tx.rollback();  
  88.             }  
  89.         }  
  90.         finally  
  91.         {  
  92.             HibernateUtil.close(session);  
  93.         }  
  94.     }  
  95.     public Person getsinglePerson(Integer id)  
  96.     {  
  97.         Session session = HibernateUtil.openSession();  
  98.         Transaction tx = session.beginTransaction();  
  99.         Person person = null;  
  100.         try  
  101.         {  
  102.             person = (Person)session.get(Person.class,id);  
  103.               
  104.             tx.commit();  
  105.         }  
  106.         catch(Exception ex)  
  107.         {  
  108.   
  109.             if(null != tx)  
  110.             {  
  111.                 tx.rollback();  
  112.             }  
  113.         }  
  114.         finally  
  115.         {  
  116.             HibernateUtil.close(session);  
  117.         }  
  118.         return person;  
  119.     }  
  120.   
  121.     public void updatePerson(Person person)  
  122.     {  
  123.         Session session = HibernateUtil.openSession();  
  124.         Transaction tx = session.beginTransaction();  
  125.           
  126.         try  
  127.         {  
  128.             session.update(person);  
  129.               
  130.             tx.commit();  
  131.         }  
  132.         catch(Exception ex)  
  133.         {  
  134.   
  135.             if(null != tx)  
  136.             {  
  137.                 tx.rollback();  
  138.             }  
  139.         }  
  140.         finally  
  141.         {  
  142.             HibernateUtil.close(session);  
  143.         }  
  144.     }  
  145. }  


6、輔助部分:

  1. import org.hibernate.Session;  
  2. import org.hibernate.SessionFactory;  
  3. import org.hibernate.cfg.Configuration;  
  4.   
  5. public class HibernateUtil  
  6. {  
  7.     private static SessionFactory sessionFactory;  
  8.       
  9.     static  
  10.     {  
  11.         try  
  12.         {  
  13.             sessionFactory = new Configuration().configure().buildSessionFactory();  
  14.         }  
  15.         catch(Exception ex)  
  16.         {  
  17.             ex.printStackTrace();  
  18.         }  
  19.     }  
  20.       
  21.     public static Session openSession()  
  22.     {  
  23.         Session session = sessionFactory.openSession();  
  24.           
  25.         return session;  
  26.     }  
  27.       
  28.     public static void close(Session session)  
  29.     {  
  30.         if(null !=session)  
  31.         {  
  32.             session.close();  
  33.         }  
  34.     }  
  35. }  


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();

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多