實體bean,文件名字:Employee.java
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cn.toher.hibernate.model; import javax.persistence.Entity; import javax.persistence.Id; /** * * @author Administrator */ @Entity public class Employee { private String id; private String firstName; private String lastName; private int salary; public Employee() {} public Employee(String fname, String lname, int salary) { this.firstName = fname; this.lastName = lname; this.salary = salary; } @Id public String getId() { return id; } public void setId( String id ) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName( String first_name ) { this.firstName = first_name; } public String getLastName() { return lastName; } public void setLastName( String last_name ) { this.lastName = last_name; } public int getSalary() { return salary; } public void setSalary( int salary ) { this.salary = salary; } }
映射文件 文件名字:Employee.hbm.xm<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
//這是完整的包名,如果沒有回報找不到實體bean的錯誤
<class name="cn.toher.hibernate.model.Employee" table="EMPLOYEE"> <meta attribute="class-description"> This class contains the employee detail. </meta>
// 這里面的string的s 是小寫的,如果寫成大寫的回報錯誤
<id name="id" type="string" column="id">
//id生成策略
<generator class="uuid"/> </id> <property name="firstName" column="first_name" type="string"/> <property name="lastName" column="last_name" type="string"/> <property name="salary" column="salary" type="int"/> </class> </hibernate-mapping>
配置文件 文件名字:hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!-- DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. Copyright (c) 2008, 2016 Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. The contents of this file are subject to the terms of either the GNU General Public License Version 2 only ("GPL") or the Common Development and Distribution License("CDDL") (collectively, the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the License at http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the specific language governing permissions and limitations under the License. When distributing the software, include this License Header Notice in each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this particular file as subject to the "Classpath" exception as provided by Oracle in the GPL Version 2 section of the License file that accompanied this code. If applicable, add the following below the License Header, with the fields enclosed by brackets [] replaced by your own identifying information: "Portions Copyrighted [year] [name of copyright owner]" If you wish your version of this file to be governed by only the CDDL or only the GPL Version 2, indicate your decision by adding "[Contributor] elects to include this software in this distribution under the [CDDL or GPL Version 2] license." If you do not indicate a single choice of license, a recipient has the option to distribute your version of this file under either the CDDL, the GPL Version 2 or to extend the choice of license to its licensees as provided above. However, if you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then the option applies only if the new code is made subject to such option by the copyright holder. Contributor(s): --> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!--表明以下的配置是針對session-factory配置的,SessionFactory是Hibernate中的一個類,這個類主要負責保存HIbernate的配置信息,以及對Session的操作--> <session-factory> <!-- 1,數據庫連接信息: --> <!--配置數據庫的驅動程序,Hibernate在連接數據庫時,需要用到數據庫的驅動程序--> <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <!--設置數據庫的連接url:jdbc:mysql://localhost:3306/dbname,其中localhost表示mysql服務器名稱,此處為本機, dbname是數據庫名--> <property name="hibernate.connection.url">jdbc:sqlserver://192.168.1.131:1433;DatabaseName=Test</property> <!--連接數據庫是用戶名--> <property name="hibernate.connection.username">sa</property> <!--連接數據庫是密碼--> <property name="hibernate.connection.password">123456a</property> <!--hibernate.dialect 只是Hibernate使用的數據庫方言,就是要用Hibernate連接那種類型的數據庫服務器。--> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- 以下信息可選 --> <!--jdbc.use_scrollable_resultset是否允許Hibernate用JDBC的可滾動的結果集。對分頁的結果集。對分頁時的設置非常有幫助--> <!-- <property name="jdbc.use_scrollable_resultset">false </property> connection.useUnicode連接數據庫時是否使用Unicode編碼 <property name="Connection.useUnicode">true </property> connection.characterEncoding連接數據庫時數據的傳輸字符集編碼方式, <property name="connection.characterEncoding">utf-8 </property> --> <!-- 2,數據庫連接池信息: 注意:現在還用不上,所以先注銷掉 <property name="hibernate.connection.pool.size">20 </property> 指定連接池的最大連接個數,使用連接池需要加載所有的鏈接池的JAR文件,JAR文件在Hibernate文件夾下的“lib\optional\c3p0”中 <property name="hibernate.c3p0.max_size">30</property> <property name="hibernate.c3p0.min_size">10</property> <property name="hibernate.c3p0.timeout">5000</property> 指定連接池里連接超時時長,即最大時間 --> <!--3,數據庫一次操作時的記錄數::--> <!--jdbc.fetch_size是指Hibernate每次從數據庫中取出並放到JDBC的Statement中的記錄條數。Fetch Size設的越大,讀數據庫的次數越少,速度越快,Fetch Size越小,讀數據庫的次數越多,速度越慢--> <!-- <property name="jdbc.fetch_size">50 </property> jdbc.batch_size是指Hibernate批量插入,刪除和更新時每次操作的記錄數。Batch Size越大,批量操作的向數據庫發送Sql的次數越少,速度就越快,同樣耗用內存就越大 <property name="jdbc.batch_size">23 </property> --> <!--4,是否顯示sql:--> <!--是否在后台顯示Hibernate用到的SQL語句,開發時設置為true,便於差錯,程序運行時可以在Eclipse的控制台顯示Hibernate的執行Sql語句。項目部署后可以設置為false,提高運行效率--> <property name="hibernate.show_sql">true </property> <!-- create: 先刪表,再建表。 create-drop: 啟動時建表,退出前刪表。 update: 如果表結構不一致,就創建或更新。 validate: 啟動時驗證表結構,如果不致就拋異常。 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 5,開啟二級緩存使用: 4.X二級緩存里面缺少很多包,所以先別用--> <!-- <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> 指定緩存產品所需的類 <property name="hibernate.cache.use_query_cache">true</property> 啟用查詢緩存 --> <!--6,指定映射文件,可映射多個映射文件,映射的就是實體類,這是基於XML映射文件的--> <!--<mapping resource="cn/toher/hibernate/model/Student.hbm.xml" />--> <!--這是基於Annotation注解的--> <mapping resource="cn/toher/hibernate/model/Employee.hbm.xml" /> <!--<mapping class="cn.toher.hibernate.model.Employee" />--> <!-- <mapping class="cn.toher.hibernate.model.OutboundLog" /> <mapping class="cn.toher.hibernate.model.AfterSaleLog" /> --> </session-factory> </hibernate-configuration>