Hibernate之HelloWorld
生活随笔
收集整理的這篇文章主要介紹了
Hibernate之HelloWorld
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 步驟
0. 導入相關Jar包。
1. 編寫Hibernate的持久化文件 (默認為hibernate.cfg.xml)。
2. 編寫持久化類。
3. 創建對象 - 關系文件(.htm.xml文件)。
4. 通過hibernate的API 編寫訪問數據庫的代碼。
2. 具體代碼實現
1. 創建hibernate的配置文件 - hibernate.cfg.xml文件
1)項目-右鍵-new-other-hibernate下-cfg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!-- 配置連接數據庫的基本信息 --> 8 <!-- 數據庫的用戶名 密碼 驅動 URL --> 9 <property name="connection.username">root</property> 10 <property name="connection.password">960105</property> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.url">jdbc:mysql:///hibernate</property> 13 <!-- 配置hibernate的基本方法 --> 14 <!-- hibernate所使用的數據庫方言 --> 15 <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 16 17 <!-- 執行操作時是否在控制臺打印 SQL --> 18 <property name="show_sql">true</property> 19 20 <!-- 是否對 SQL 進行格式化 --> 21 <property name="format_sql">true</property> 22 23 <!-- 指定自動生成數據表的策略 --> 24 <property name="hbm2ddl.auto">update</property> 25 26 <!-- 指定關聯的hbm.xml文件 --> 27 <mapping resource="com/tk/hibernate/News.hbm.xml"/> 28 </session-factory> 29 </hibernate-configuration>2.編寫持久化類
1. 必須提供一個無參數的構造器(反射)。
2. 提供一個唯一的標識屬性(主鍵)。
3. 為類的持久化字段提供訪問方法(即get/set)。
4. 重寫equal和hashCode方法(非必須)。
?
1 package com.tk.hibernate; 2 3 import java.sql.Date; 4 5 public class News { 6 private Integer id; //field 7 private String title; 8 private String author; 9 private Date date; 10 public Integer getId() { 11 return id; 12 } 13 public void setId(Integer id) { 14 this.id = id; 15 } 16 public String getTitle() { 17 return title; 18 } 19 public void setTitle(String title) { 20 this.title = title; 21 } 22 public String getAuthor() { 23 return author; 24 } 25 public void setAuthor(String author) { 26 this.author = author; 27 } 28 public Date getDate() { 29 return date; 30 } 31 public void setDate(Date date) { 32 this.date = date; 33 } 34 public News(String title, String author, Date date) { 35 super(); 36 this.title = title; 37 this.author = author; 38 this.date = date; 39 } 40 public News() { 41 // TODO Auto-generated constructor stub 42 } 43 }3. 創建關系 - 對象映射文件 -?項目-右鍵-new-other-hibernate下-hbm.xml
注:主鍵的生成方式暫為native,采用數據庫本地生成的方式。
?
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2017-4-18 20:03:06 by Hibernate Tools 3.5.0.Final --> 5 <hibernate-mapping> 6 <class name="com.tk.hibernate.News" table="NEWS"> 7 <!--name 類里面的名字 --> 8 <id name="id" type="java.lang.Integer"> 9 <!--column 對應數據表中的列名 --> 10 <column name="ID" /> 11 <!-- 指定主鍵的生成方式 ,native使用數據庫本地的生成方式--> 12 <generator class="native" /> 13 </id> 14 <property name="title" type="java.lang.String"> 15 <column name="TITLE" /> 16 </property> 17 <property name="author" type="java.lang.String"> 18 <column name="AUTHOR" /> 19 </property> 20 <property name="date" type="java.sql.Date"> 21 <column name="DATE" /> 22 </property> 23 </class> 24 </hibernate-mapping>?
4.創建測試類(Junit)
具體步驟-在代碼部分
?
1 package com.tk.hibernate; 2 3 import static org.junit.Assert.*; 4 5 import java.sql.Date; 6 7 import org.hibernate.Session; 8 import org.hibernate.SessionFactory; 9 import org.hibernate.Transaction; 10 import org.hibernate.cfg.Configuration; 11 import org.hibernate.service.ServiceRegistry; 12 import org.hibernate.service.ServiceRegistryBuilder; 13 import org.junit.Test; 14 15 public class HibernateTest { 16 @Test 17 public void test() { 18 //1. 創建sessionFactory 對象 19 SessionFactory sessionFactory=null; 20 //1) 創建一個configuration 對象:對應hibernate的基本配置信息和對象關系映射信息 21 Configuration configuration=new Configuration().configure(); 23 //4.0 之前這樣創建 25 //sessionFactory = configuration.buildSessionFactory(); 26 //2). 創建一個 ServiceRegistry 對象: hibernate 4.x 新添加的對象 28 //hibernate 的任何配置和服務都需要在該對象中注冊后才能有效. 29 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) 30 .buildServiceRegistry(); 31 sessionFactory = configuration.buildSessionFactory(serviceRegistry); 33 //2. 創建一個 Session 對象 35 Session session = sessionFactory.openSession(); 36 //3. 開啟事務 38 Transaction transaction = session.beginTransaction(); 39 //4. 執行保存操作 41 News news = new News("Java12345", "ATGUIGU", new Date(new java.util.Date().getTime())); 42 session.save(news); 43 //5. 提交事務 45 transaction.commit(); 46 //6. 關閉 Session 48 session.close(); 49 //7. 關閉 SessionFactory 對象 51 sessionFactory.close(); 52 53 } 54 55 }?
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/quinntian/p/6746906.html
總結
以上是生活随笔為你收集整理的Hibernate之HelloWorld的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实践lnmpde 的安装
- 下一篇: test18