標籤:des style blog io ar color os 使用 sp
一.hibernate.cfg.xml: hbni2ddl.auto
二.搭建日誌環境並配置顯示DDL語句
我們使用slf介面,然後使用log4j的實現。
1、 首先引入log4j的jar包(log4j-1.2.14.jar),
2、 然後再引入slf4j實現LOG4J和適配器jar包(slf4j-log4j12-1.5.8.jar)
3、 最後建立log4j的設定檔(log4j.properties),並加以修改,只要保留
log4j.logger.org.hibernate.tool.hbm2ddl=debug
三.搭建Junit環境
1、首先引入Junit 類庫 jar包 (junit-4.8.1.jar)
2、在項目名上右鍵→new→Source Folder→輸入名稱→finish
3、注意,你對哪個包進行測試,你就在測試下建立和那個包相同的包
4、建立測試類別,需要在測試的方法前面加入”@Test”
package com.bjsxt.hibernate.model;import static org.junit.Assert.*;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;public class TeacherTest { private static SessionFactory sf; @BeforeClass public static void beforeClass(){ try { sf=new AnnotationConfiguration().configure().buildSessionFactory(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void testTeacherSave() { Teacher t=new Teacher(); t.setId(3); t.setName("t1"); t.setTitle("中級"); Session session=sf.openSession(); session.beginTransaction(); session.save(t); session.getTransaction().commit(); session.close(); } @AfterClass public static void afterClass(){ sf.close(); }}
四.ehibernate.cfg.xml : show_sql
輸出所有SQL語句到控制台. 有一個另外的選擇是把org.hibernate.SQL這個log category設為debug。
取值: true | false
五,hibernate.cfg.xml :format_sql
六,表名和類名不同,對錶名進行配置
Annotation:使用 @Table(name=”tableName”) 進行註解
@Entity@Table(name="_Teacher")public class Teacher { private int id; private String name; private String title; private String money; private Date birthDay; private ZhiCheng zhiCheng;。。。。。}
七,欄位名和屬性相同
Annotation:預設為@Basic
注意:如果在成員屬性沒有加入任何註解,則預設在前面加入了@Basic
Xml中不用寫column
八,欄位名和屬性名稱不同
Annotation:使用@Column(name=”columnName”)進行註解
@Column(name="_name") public String getName() { return name; }
Xml:
<property name="name" column="_name"/>
九,不需要(持久化)psersistence的欄位
就是實體類的某個成員屬性不需要存入資料庫中
Annotation:使用@Transient 進行註解就可以了。
@Transient public String getMoney() { return money; } public void setMoney(String money) { this.money = money; }
Xml:不寫(就是不需要對這個成員屬性進行映射
十,映射日期與時間類型,指定時間精度
Annotation:使用@Temporal(value=TemporalType)來註解表示日期和時間的註解
其中TemporalType有三個值:TemporalType.TIMESTAMP 表示yyyy-MM-dd HH:mm:ss
TemporalType.DATE 表示yyyy-MM-dd
TemporalType.TIME 表示HH:mm:ss
@Temporal(TemporalType.DATE)//資料庫中只存日期,不存時間 public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; }
注意:當使用註解時,屬性為value時,則這個屬性名稱可以省略,例如:@Temporal(TemporalType)
Xml:使用type屬性指定hibernate類型
<property name="birthDate" type="date"/>
注意:hibernate日期時間類型有:date, time, timestamp,當然您也可以使用Java封裝類
十一,映射枚舉類型
Annotation:使用@Enumerated(value=EnumType)來註解表示此成員屬性為枚舉映射到資料庫
其中EnumType有二個值:①EnumType.STRING 表示直接將枚舉名稱存入資料庫
②EnumType.ORDINAL 表示將枚舉所對應的數值存入資料庫
@Enumerated(EnumType.STRING)//string 在資料庫中映射成字串,而oridinal會映射成int類型從0開始 public ZhiCheng getZhiCheng() { return zhiCheng; } public void setZhiCheng(ZhiCheng zhiCheng) { this.zhiCheng = zhiCheng; }
Xml:映射非常的麻煩,先要定義自訂類型,然後再使用這個定義的類型……
總結:
package com.bjsxt.hibernate.model;import java.util.Date;import java.util.Enumeration;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.EnumType;import javax.persistence.Enumerated;import javax.persistence.Id;import javax.persistence.Table;import javax.persistence.Temporal;import javax.persistence.TemporalType;import javax.persistence.Transient;@Entity@Table(name="_Teacher")public class Teacher { private int id; private String name; private String title; private String money; private Date birthDay; private ZhiCheng zhiCheng; @Id public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name="_name") public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Transient public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } @Temporal(TemporalType.DATE)//資料庫中只存日期,不存時間 public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } @Enumerated(EnumType.STRING)//string 在資料庫中映射成字串,而oridinal會映射成int類型從0開始 public ZhiCheng getZhiCheng() { return zhiCheng; } public void setZhiCheng(ZhiCheng zhiCheng) { this.zhiCheng = zhiCheng; }}
hibernate學習筆記_基礎配置