Hibernate 註解應用搭建
阿新 • • 發佈:2018-07-22
文件中 -- query color odin ppr bubuko int set
一、構建項目目錄結構
二、實體類
1 package gdpi.annotation; 2 3 import java.io.Serializable; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 import javax.persistence.CascadeType; 8 import javax.persistence.Column; 9 import javax.persistence.Entity; 10 import javax.persistence.GeneratedValue;11 import javax.persistence.GenerationType; 12 import javax.persistence.Id; 13 import javax.persistence.OneToMany; 14 import javax.persistence.Table; 15 import javax.persistence.Transient; 20 21 @Entity // @Entity註解 聲明這個類為一個實體類 22 @Table(name="t_grade") //@Table 指明這個實體類對應數據庫t_grade 表 23 public class Grade implements Serializable{ 24 25 @Transient // 指明屬性在數據庫中不存在 26 private static final long serialVersionUID = 1L; 27 @Id // 指定屬性為唯一標識 28 @GeneratedValue(strategy = GenerationType.IDENTITY) //主鍵生成策略 29 private Integer gid; 30 31 @Column(name="gname") // 將實體類的屬性和數據庫表的字段作一個映射 32 private String gname; 33 34 @Column(name="gdesc") 35 private String gdesc; 36 37 public Grade() { 38 super(); 39 // TODO Auto-generated constructor stub 40 } 41 42 public Grade(Integer gid, String gname, String gdesc) { 43 super(); 44 this.gid = gid; 45 this.gname = gname; 46 this.gdesc = gdesc; 47 } 48 49 50 public Integer getGid() { 51 return gid; 52 } 53 54 public void setGid(Integer gid) { 55 this.gid = gid; 56 } 57 58 public String getGname() { 59 return gname; 60 } 61 62 public void setGname(String gname) { 63 this.gname = gname; 64 } 65 66 public String getGdesc() { 67 return gdesc; 68 } 69 70 public void setGdesc(String gdesc) { 71 this.gdesc = gdesc; 72 } 73 74 @Override 75 public String toString() { 76 return "Grade [gid=" + gid + ", gname=" + gname + ", gdesc=" + gdesc + "]"; 77 } 78 }
三、核心配置文件中加入實體的引用
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 <!--1、配置數據庫連接信息 --> 8 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 9 <property name="hibernate.connection.url">jdbc:mysql:///hibernate_study</property> <--hibernate_study 為我的本地數據庫--> 10 <property name="hibernate.connection.username">root</property> <-- root 為我的本地數據庫用戶名--> 11 <property name="hibernate.connection.password">root</property> <-- root 為我的本地數據庫密碼--> 12 13 <!--2、配置hibernate的信息 --> 14 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 15 <property name="hibernate.format_sql">true</property> 16 <property name="hibernate.show_sql">true</property> 17 <property name="hibernate.hbm2ddl.auto">update</property> 18 19 <-- 引入實體 class:為實體類的類路徑--> 20 <mapping class="gdpi.annotation.Grade"/> 21 22 23 </session-factory> 24 </hibernate-configuration>
四、測試類【測試配置文件及註解是否正常使用】
1 package gdpi.annotation; 2 3 import java.util.List; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.cfg.Configuration; 8 import org.junit.Test; 9 10 import gdpi.annotation.Grade; 11 12 /** 13 * 測試類 14 * */ 15 @SuppressWarnings("deprecation") 16 public class Test { 17 private static Configuration cfg = null; 18 private static SessionFactory sf = null; 19 private static Session session = null; 20 21 static{ 22 cfg = new Configuration(); 23 cfg.configure(); 24 sf = cfg.buildSessionFactory(); 25 session = sf.openSession(); 26 } 27 28 public static Session getSession() { 29 return session; 30 } 31 32 @Test 33 public void test(){ 34 // 獲取Session 對象,類似於JDBC中的Connection對象 35 Session session = getSession(); 36 List<Grade> list = session.createQuery("from Grade").list(); // 使用hql查詢語句,查詢該表中的所有數據 37 if(list != null){ 38 for(Grade g:list){ 39 System.out.println(g); 40 } 41 } 42 } 43 }
Run:
控制臺打印:
本地MySQL數據庫
Hibernate 註解應用搭建