Hibernate HelloWorld案例
阿新 • • 發佈:2018-11-29
搭建一個Hibernate環境,開發步驟:
1. 下載原始碼
版本:hibernate-distribution-3.6.0.Final
2. 引入jar檔案
hibernate3.jar核心 + required 必須引入的(6個) + jpa 目錄 + 資料庫驅動包
3. 寫物件以及物件的對映
Employee.java
Employee.hbm.xml 物件的對映 (對映檔案)
4. src/hibernate.cfg.xml 主配置檔案
-資料庫連線配置
載入所用的對映(*.hbm.xml)
5. App.java 測試
package loaderman.a_hello; import java.util.Date; public class Employee { private int empId; private String empName; private Date workDate; public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; }public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Date getWorkDate() { return workDate; } public void setWorkDate(Date workDate) { this.workDate = workDate; } @Override public String toString() { return "Employee [empId=" + empId + ", empName=" + empName + ", workDate=" + workDate + "]"; } }
Employee.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="loaderman.a_hello"> <class name="Employee" table="employee"> <!-- 主鍵 ,對映--> <id name="empId" column="id"> <generator class="native"/> </id> <!-- 非主鍵,對映 --> <property name="empName" column="empName"></property> <property name="workDate" column="workDate"></property> </class> </hibernate-mapping>
src/hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- 通常,一個session-factory節點代表一個數據庫 --> <session-factory> <!-- 1. 資料庫連線配置 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hib_demo</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!-- 資料庫方法配置, hibernate在執行的時候,會根據不同的方言生成符合當前資料庫語法的sql --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 2. 其他相關配置 --> <!-- 2.1 顯示hibernate在執行時候執行的sql語句 --> <property name="hibernate.show_sql">true</property> <!-- 2.2 格式化sql --> <property name="hibernate.format_sql">true</property> <!-- 2.3 自動建表 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 3. 載入所有對映 --> <mapping resource="loaderman/a_hello/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>
測試:
package loaderman.a_hello; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App { @Test public void testHello() throws Exception { // 物件 Employee emp =new Employee(); // Employee emp = new Employee(); emp.setEmpName("班長"); emp.setWorkDate(new Date()); // 獲取載入配置檔案的管理類物件 Configuration config = new Configuration(); config.configure(); // 預設載入src/hibenrate.cfg.xml檔案 // 建立session的工廠物件 SessionFactory sf = config.buildSessionFactory(); // 建立session (代表一個會話,與資料庫連線的會話) Session session = sf.openSession(); // 開啟事務 Transaction tx = session.beginTransaction(); //儲存-資料庫 session.save(emp); // 提交事務 tx.commit(); // 關閉 session.close(); sf.close(); } }
package loaderman.a_hello; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; public class App2 { private static SessionFactory sf; static { /* //1. 建立配置管理類物件 Configuration config = new Configuration(); // 載入配置檔案 (預設載入src/hibernate.cfg.xml) config.configure(); //2. 根據載入的配置管理類物件,建立SessionFactory物件 sf = config.buildSessionFactory(); */ // 建立sf物件 sf = new Configuration().configure().buildSessionFactory(); } //1. 儲存物件 @Test public void testSave() throws Exception { // 物件 Employee emp = new Employee(); emp.setEmpName("張三123"); emp.setWorkDate(new Date()); //根據session的工廠,建立session物件 Session session = sf.openSession(); // 開啟事務 Transaction tx = session.beginTransaction(); //-----執行操作----- session.save(emp); // 提交事務/ 關閉 tx.commit(); session.close(); } //更新 @Test public void testUpdate() throws Exception { // 物件 Employee emp = new Employee(); emp.setEmpId(1); emp.setEmpName("張三3"); // 建立session Session session = sf.openSession(); Transaction tx = session.beginTransaction(); //-------執行操作------- // 沒有設定主鍵,執行儲存;有設定主鍵,執行更新操作; 如果設定主鍵不存在報錯! session.saveOrUpdate(emp); tx.commit(); session.close(); } }
package loaderman.a_hello; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Restrictions; import org.junit.Test; public class App3 { private static SessionFactory sf; static { // 建立sf物件 sf = new Configuration().configure().buildSessionFactory(); } //HQL查詢 【適合有資料庫基礎的】 @Test public void testQuery() throws Exception { Session session = sf.openSession(); Transaction tx = session.beginTransaction(); // 主鍵查詢 //Employee emp = (Employee) session.get(Employee.class, 1); // HQL查詢,查詢全部 Query q = session.createQuery("from Employee where empId=1 or empId=2"); List<Employee> list = q.list(); System.out.println(list); tx.commit(); session.close(); } //QBC查詢 , query by criteria 完全面向物件的查詢 @Test public void testQBC() throws Exception { Session session = sf.openSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Employee.class); // 條件 criteria.add(Restrictions.eq("empId", 1)); // 查詢全部 List<Employee> list = criteria.list(); System.out.println(list); tx.commit(); session.close(); } //sQL @Test public void testSQL() throws Exception { Session session = sf.openSession(); Transaction tx = session.beginTransaction(); // 把每一行記錄封裝為物件陣列,再新增到list集合 // SQLQuery sqlQuery = session.createSQLQuery("select * from employee"); // 把每一行記錄封裝為 指定的物件型別 SQLQuery sqlQuery = session.createSQLQuery("select * from employee").addEntity(Employee.class); List list = sqlQuery.list(); System.out.println(list); tx.commit(); session.close(); } }