2016年7月22日 星期五

建置第一個Hibernate In Intellj

環境配置:

  • Intellj 1.4
  • JDBC4
  • Hibernate jar (少其中一個可能都會出錯)




















內容參考至:




Step1.檔案結構目錄
Step2.設置hibernate.cfg.xml
  1. <hibernate-configuration>
  2.     <session-factory>
  3.         <!-- 顯示實際操作資料庫時的SQL -->
  4.         <property name="show_sql">true</property>
  5.         <!-- 將顯示的SQL排版,方便觀看 -->
  6.         <property name="format_sql">true</property>
  7.         <!-- SQL方言,這邊設定的是SQLServer -->
  8.         <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
  9.         <!-- JDBC驅動程式 -->
  10.         <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  11.         <!-- JDBC URL -->
  12.         <property name="connection.url">jdbc:sqlserver://127.0.0.1:1433;DatabaseName=testdb</property>
  13.         <!-- 資料庫使用者 -->
  14.         <property name="connection.username">sa</property>
  15.         <!-- 資料庫密碼 -->
  16.         <property name="connection.password">578989</property>
  17.         <!-- 物件與資料庫表格映射文件 -->
  18.         <mapping resource="User.hbm.xml">
  19.     </mapping></session-factory>
  20. </hibernate-configuration>



Step3.設置User.hbm.xml
-重點注意事項
1.資料庫每個欄位都要記得設定到<property>
2.其中id是個特殊的屬性,Hibernate會使用它來作為主鍵識別,您可以定義主鍵產生的方式,這是在XML映射文件中完成
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping
  3.         PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5.  
  6. <hibernate-mapping>
  7.     <!--<class>標籤的name屬性為所映射的物件,而table為所映射的表格;-->
  8.     <class name="Hibernate.UserInformationEntity" table="user_information">
  9.         <id name="id" column="id"> <!--<id>中 column屬性指定了表格欄位,而 type屬性指定了User實例的中的id之型態。-->
  10.             <generator class="native"/> <!--<id>中主鍵的產生方式在這邊設定為"native",表示主鍵的生成方式由Hibernate根據資料庫Dialect 的定義來決定-->
  11.             <!--<generator class="identity"/>-->
  12.             <!--identity-->
  13.         </id>
  14.         <!--<property>標籤中的column與type都各自指明了表格中欄位與物件中屬性的對應。-->
  15.         <property name="account" column="account"/>
  16.         <property name="name" column="name"/>
  17.         <property name="age" column="age"/>
  18.         <property name="create_time" column="create_time"/>
  19.     </class>
  20. </hibernate-mapping>


Step4.建立實體,與表格對應
-重點注意事項
1.資料庫欄位『型態是甚麼』,實體就要設一樣,否則會出錯
ex.資料庫:age int not null  實體:int age;
  1. public class UserInformationEntity {
  2.     int id =0;
  3.     String account;
  4.     String name;
  5.     int age = 0;
  6.     Date create_time;
  7.  
  8.     public int getId() {
  9.         return id;
  10.     }
  11.     public void setId(int id) {
  12.         this.id = id;
  13.     }
  14.     public String getAccount() {
  15.         return account;
  16.     }
  17.     public void setAccount(String account) {
  18.         this.account = account;
  19.     }
  20.     public String getName() {
  21.         return name;
  22.     }
  23.     public void setName(String name) {
  24.         this.name = name;
  25.     }
  26.     public int getAge() {
  27.         return age;
  28.     }
  29.     public void setAge(int age) {
  30.         this.age = age;
  31.     }
  32.     public Date getCreate_time() {
  33.         return create_time;
  34.     }
  35.     public void setCreate_time(Date create_time) {
  36.         this.create_time = create_time;
  37.     }
  38. }
Sept5.設定SessionFactory實例,連線用 
-重點注意事項  
1.也可直接寫在main class之中,一個應用程式當中通常只需要一個SessionFactory實例, 為了方便整個應用取得同一個SessionFactory實例, 可以撰寫一個HibernateUtil類別 
2.要如何寫在main class可參考: http://blog.csdn.net/zs234/article/details/9108533
  1. package Hibernate;
  2. import org.hibernate.SessionFactory;
  3. import org.hibernate.cfg.Configuration;
  4. /**
  5.  * Created by ytchen on 2016/7/22.
  6.  */
  7. public class HibernateUtil {
  8.     private static SessionFactory sessionFactory;
  9.     static {
  10.         try {
  11.             sessionFactory = new Configuration().configure()
  12.                     .buildSessionFactory();
  13.         } catch (Throwable ex) {
  14.             throw new ExceptionInInitializerError(ex);
  15.         }
  16.     }
  17.     public static SessionFactory getSessionFactory() {
  18.         return sessionFactory;
  19.     }
  20.     public static void shutdown() {
  21.         getSessionFactory().close();
  22.     }
Sept6.馬上來試用Hibernate吧 
  1. package Hibernate;
  2.  
  3. import org.hibernate.Transaction;
  4.  
  5. import org.hibernate.Session;
  6.  
  7.  
  8.  
  9. import java.util.Date;
  10.  
  11.  
  12.  
  13. /**
  14.  
  15.  * Created by ytchen on 2016/7/22.
  16.  
  17.  */
  18.  
  19. public class HibernateDemo {
  20.  
  21.     public static void main(String[] args) {
  22.  
  23.         UserInformationEntity user = new UserInformationEntity();
  24.  
  25.         user.setAccount("Ian");
  26.  
  27.         user.setName("Ian");
  28.  
  29.         user.setAge(20);
  30.  
  31.         user.setCreate_time(new Date());
  32.  
  33.  
  34.  
  35.         // 開啟Session,相當於開啟JDBC的Connection
  36.  
  37.         Session session = HibernateUtil.getSessionFactory().openSession();
  38.  
  39.         // Transaction表示一組會話操作
  40.  
  41.         Transaction tx= session.beginTransaction();
  42.  
  43.         // 將物件映射至資料庫表格中儲存
  44.  
  45.         session.save(user);
  46.  
  47.         tx.commit();
  48.  
  49.         session.close();
  50.  
  51.  
  52.  
  53.         System.out.println("新增資料OK!請用Sqlserver觀看結果!");
  54.  
  55.  
  56.  
  57.         HibernateUtil.shutdown();
  58.  
  59.     }
  60.  
  61. }
  62.  

2016年7月21日 星期四

Hibernate 心得

1.Hibernate簡介:

Hibernate 是「物件/關係對應」(Object/Relational Mapping)的解決方案,簡寫為ORM,簡單的說就是將 Java 中的物件與物件關係,映射至關聯式資料庫中的表格與表格之間的關係, Hibernate 提供了這個過程中自動對應轉換的方案。

白話:
直接將Java設計好的物件(存放欄位跟資料的物件),直接跟資料庫中的表格做連結,省下中間很多的設定


2.甚麼是物件:

  • 我們自己建的一個Class,用來存放資料庫欄位跟資料,之後可以new這個物件出來做事情
  • 利用Get & Set 做出存取的方法


public class EipMemberBean {
    String m_account = "";
    String m_password = "";
    String m_block = "";
    String m_name ="";
    String m_email ="";
    String m_gender ="";
    String m_create_date ="";
    String m_create_time ="";
    String ls_ad ="";

    public String getM_password() {
        return m_password;
    }

    public void setM_password(String m_password) {
        this.m_password = m_password;
    }

    public String getM_account() {
        return m_account;
    }

    public void setM_account(String m_account) {
        this.m_account = m_account;
    }

    public String getM_block() {
        return m_block;
    }

    public void setM_block(String m_block) {
        this.m_block = m_block;
    }

    public String getM_name() {
        return m_name;


    }

Windos.open傳值問題

重點注意:若chrome不能執行,請用IE

方法一:單純傳值法

母:
  1. function openWindow(){
  2. window.open('upload.html','mywindow','width=400,height=200');
  3. }

子:
  1. function backToParent(){
  2. //opener(母視窗)的form1的輸入值 = 目前網頁(子視窗)form2輸入的值
  3. opener.document.form1.textInput.value = document.form2.input.value;
  4. window.close();
  5. }


方法二:呼叫母視窗的某個方法,並帶參數

母:
  1. function action4(){
  2. window.open('popupView.html','PopupView', config='height=350,width=350');
  3. //document.getElementById("myText").value = window.returnValue;
  4. }
  5.  
  6. function actionFather(Value){
  7. //alert("Value")
  8. var Value = Value;
  9. }


子:

  1. function action5(){
  2. window.opener.actionFather("haha"); //呼叫父視窗的actionFather方法,並把值傳過去給他
  3. window.close();
  4. }

2016年7月20日 星期三

jQuery-分頁顯示

資料來源: https://code.google.com/p/jquery-tablepage/


- 這是一個針對JQuery所開發的表格分頁plugin,
- 主要目的是讓使用者能夠針對現有的資料表格,不需要更改過多的程式碼,即可輕易的加入分頁的功能.

使用說明
步驟1: 引用plugin檔案(這邊用1.0版)



  1. <script src="jquery-tablepage-1.0.js"></script>

步驟2: 針對要分頁的table,與顯示頁碼的span,分別設定一個id(span放在妳想要顯示的位置)
  1. <table id='table_source'>...</table>
  2. <span id='table_page'></span>

步驟3: 執行plugin
  1. $("#table_source").tablepage($("#table_page"), 10);
第二個參數10,代表每一頁要顯示10筆資料

完成結果參考:
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" href="">
  6. <script src=""></script>
  7. <title>Main</title>
  8. <!-- // 引用 jQuery.js 和 jQuery-TablePage.js -->
  9. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
  10. <script src="jquery-tablepage-1.0.js"></script>
  11. </head>
  12. <body>
  13.  
  14.  
  15. <table id="tbl" border="1">
  16. <thead>
  17. <tr >
  18. <th align="left" >Name</th>
  19. <th align="left" >Content</th>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. <tr>
  24. <td>Name1</td>
  25. <td>Content1</td>
  26. <tr>
  27. <tr>
  28. <td>Name2</td>
  29. <td>Content2</td>
  30. <tr>
  31. <tr>
  32. <td>Name3</td>
  33. <td>Content3</td>
  34. <tr>
  35. <tr>
  36. <td>Name4</td>
  37. <td>Content4</td>
  38. <tr>
  39. <tr>
  40. <td>Name5</td>
  41. <td>Content5</td>
  42. <tr>
  43. <tr>
  44. <td>Name6</td>
  45. <td>Content6</td>
  46. <tr>
  47. </tbody>
  48. </table>
  49. <!-- // 放分頁的 HTML 物件 -->
  50. <span id='table_page'></span>
  51.  
  52.  
  53. <!-- // 使用 TablePage 功能,參數1是放分頁的 HTML 物件,例如 span,參數2是每頁幾筆 -->
  54. <script>
  55. $("#tbl").tablepage($("#table_page"), 6);
  56. </script>
  57.  
  58. </body>
  59. <html>