hibernate需要配置的xml – JAVA編程語言程序開發技術文章

001
<pre class="brush:xml; toolbar: true; auto-links: false;">
002
  
003
  
004
  
005
  <blockquote>
006
   
007
   
008
   <h3>    <b>Customer.hbm.xml</b></h3>
009
  
010
  
011
  
012
  </blockquote>
013
</pre><pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="UTF-8"?><br/></pre><pre class="brush:xml; toolbar: true; auto-links: false;"><!DOCTYPE hibernate-mapping PUBLIC
014
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
015
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
016
<!– 建立持久化對象與數據庫表的關聯關系 –>
017
<hibernate-mapping>
018
    <!–
019
    class:指定類的路徑和表的名稱的關聯
020
       * name:持久化類的名稱對應的全路徑
021
       * table:表示對應數據庫表的名稱
022
     –>
023
    <class name="cn.aypak.a_primer.Customer" table="a_customer">
024
        <!–
025
            id:主鍵(OID)
026
              * name:持久化類中屬性
027
              * type:表示hibernate的數據類型,表示java的數據類型在連接sql的數據類型的一個橋梁
028
              * column:數據庫對應的字段列
029
                   * name:數據庫表中字段名稱
030
              * generator:主鍵的生成策略
031
                   * class:指定主鍵按照哪種生成策略執行(本例:increment表示自增長)
032
         –>
033
        <id name="id" type="integer">
034
            <column name="id"></column>
035
            <generator class="increment"></generator>
036
        </id>
037
        <!–
038
            property:持久化對象中的屬性與表中的字段進行關聯
039
               * name:持久化類中屬性
040
               * type:表示hibernate的數據類型,表示java的數據類型在連接sql的數據類型的一個橋梁
041
               * column:數據庫對應的字段列
042
                   * name:數據庫表中字段名稱
043
         –>
044
        <property name="name" type="string">
045
            <column name="name"></column>
046
        </property>
047
        <property name="age" type="integer">
048
            <column name="age"></column>
049
        </property>
050
        <property name="des" type="string">
051
            <column name="des"></column>
052
        </property>
053
    </class>
054
</hibernate-mapping></pre><p></p>
055
 
056
 
057
 
058
 <blockquote>
059
  
060
  
061
  <h3>   hibernate.cfg.xml</h3>
062
 
063
 
064
 
065
 </blockquote>
066
<br>
067
<p></p>
068
<p></p>
069
<pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="UTF-8"?>
070
<!DOCTYPE hibernate-configuration PUBLIC
071
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
072
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
073
 
074
<hibernate-configuration>
075
    <session-factory>
076
        <!– 操作連接mysql的數據信息 –>
077
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
078
        <property name="hibernate.connection.url">jdbc:mysql://192.168.137.244:3306/aypak?useUnicode=true&amp;characterEncoding=utf8</property>
079
        <property name="hibernate.connection.username">root</property>
080
        <property name="hibernate.connection.password">root</property>
081
        <!– 配置hibernate的方言,通知hibernate要連接哪種數據庫(mysql),將該數據庫類型識別成hibernate能夠識別的類型 –>
082
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
083
        <!– 是否通過配置文件(hbm.xml)的配置生成數據庫的表
084
          update:當表不存在的時候,自動創建表,並往表中追加數據
085
                 當表存在的時候,不會刪除表再創建表,而是直接向表中追加數據
086
          create:每次在操作數據庫(CRUD)的時候,都會先刪除表,在創建表,項目不能使用,這樣歷史數據將不被保存
087
          none:不能自動創建表,每次操作數據庫的時候,直接將數據保存到數據庫,歷史數據不丟失
088
         –>
089
        <property name="hibernate.hbm2ddl.auto">update</property>
090
        <!– 是否在開發的過程中顯示sql語句,默認是false –>
091
        <property name="hibernate.show_sql">true</property>
092
        <!– 是否對顯示的sql語句進行格式化,默認是false –>
093
        <property name="hibernate.format_sql">false</property>
094
        
095
        <!– 等價於configuration.addClass(Customer.class); –>
096
        <!– <mapping resource="cn/aypak/a_primer/Customer.hbm.xml"/>–>
097
    </session-factory>
098
</hibernate-configuration></pre><p> </p>
099
 
100
 
101
 
102
 <blockquote>
103
  
104
  
105
  <h3>  實例:CRUD</h3>
106
 
107
 
108
 
109
 </blockquote>
110
<p></p>
111
<p></p>
112
<pre class="brush:java; toolbar: true; auto-links: false;">package cn.aypak.a_primer;
113
 
114
import java.util.List;
115
 
116
import org.hibernate.Query;
117
import org.hibernate.Session;
118
import org.hibernate.SessionFactory;
119
import org.hibernate.Transaction;
120
import org.hibernate.cfg.Configuration;
121
import org.junit.Test;
122
 
123
public class App {
124
    
125
    private static SessionFactory sf = null;
126
    
127
    static{
128
        Configuration configuration = new Configuration();
129
        configuration.configure("cn/aypak/a_primer/hibernate.cfg.xml");
130
        configuration.addClass(Customer.class);
131
        sf = configuration.buildSessionFactory();
132
    }
133
    
134
    /**新增*/
135
    <a href="http://my.oschina.net/test45" target="_blank" rel="nofollow">@Test</a>
136
    public void testInsertCustomer(){
137
        Session s = sf.openSession();
138
        Transaction tr = s.beginTransaction();
139
        
140
        Customer c = new Customer();
141
        c.setName("小三");
142
        c.setAge(18);
143
        c.setDes("專業的");
144
        s.save(c);
145
        
146
        tr.commit();
147
        s.close();
148
    }
149
    
150
    /**通過id進行修改*/
151
    <a href="http://my.oschina.net/test45" target="_blank" rel="nofollow">@Test</a>
152
    public void testUpdateCustomer(){
153
        Session s = sf.openSession();
154
        Transaction tr = s.beginTransaction();
155
        
156
        Customer c = new Customer();
157
        c.setId(3);
158
        c.setName("小四");
159
        s.update(c);
160
        
161
        tr.commit();
162
        s.close();
163
    }
164
    
165
    /**通過id刪除客戶信息*/
166
    <a href="http://my.oschina.net/test45" target="_blank" rel="nofollow">@Test</a>
167
    public void testDeleteCustomer(){
168
        Session s = sf.openSession();
169
        Transaction tr = s.beginTransaction();
170
        
171
        Customer c = new Customer();
172
        c.setId(2);
173
        s.delete(c);
174
        
175
        tr.commit();
176
        s.close();
177
    }
178
    
179
    /**通過id查詢客戶信息*/
180
    <a href="http://my.oschina.net/test45" target="_blank" rel="nofollow">@Test</a>
181
    public void testQueryCustomer(){
182
        Session s = sf.openSession();
183
        Transaction tr = s.beginTransaction();
184
        
185
        Customer c = (Customer) s.get(Customer.class, 1);
186
//      Customer c = (Customer) s.load(Customer.class, 1);
187
        System.out.println(c.getName()+"    "+c.getAge()+"     "+c.getDes());
188
        
189
        tr.commit();
190
        s.close();
191
    }
192
    /**查詢所有的客戶信息*/
193
    <a href="http://my.oschina.net/test45" target="_blank" rel="nofollow">@Test</a>
194
    public void testQueryAllCustomer(){
195
        Session s = sf.openSession();
196
        Transaction tr = s.beginTransaction();
197
        
198
        /**
199
         * s.createQuery("HQL語句"):使用HQL語句查詢數據庫,返回Query對象
200
         *      SQL語句:針對數據庫、數據庫表、數據庫字段
201
         *      HQL語句:針對持久化對象、持久化對象的屬性
202
         */
203
        Query query = s.createQuery("from Customer");
204
        List<Customer> list = query.list();
205
        for(Customer c:list){
206
            System.out.println(c.getName()+"    "+c.getAge()+"     "+c.getDes());
207
        }
208
        
209
        tr.commit();
210
        s.close();
211
    }
212
}</pre><p> </p>
213
<p></p>
214
<p></p>
215
<br>
作者:Aypak

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *