之前開發都是使用xml配置來開發項目,開發起來特別繁瑣
大家會發現通過注解大大簡化了我們開發流程,使我們從繁瑣的XML配置中解放出來。
第一步:新建一個javaweb項目。並將hibernate需要的相關jar包導入。
第二步: 使用annotation需要額外增加3個jar包:
◦ hibernate-annotations.jar
◦ ejb3-persistence.jar
◦ hibernate-commons-annotations.jar
第三步:新建一個pojo並增加注解:

1 package com.qcf.pox; 2 3 import java.util.Date; 4 5 import javax.persistence.Entity; 6 import javax.persistence.GeneratedValue; 7 import javax.persistence.GenerationType; 8 import javax.persistence.Id; 9 10 @Entity 11 public class Student { 12 13 @Id 14 @GeneratedValue(strategy=GenerationType.AUTO)//代表主鍵的生成策略 15 private int stuno; 16 private String stuname; 17 private Date birthday; 18 public int getStuno() { 19 return stuno; 20 } 21 public void setStuno(int stuno) { 22 this.stuno = stuno; 23 } 24 public String getStuname() { 25 return stuname; 26 } 27 public void setStuname(String stuname) { 28 this.stuname = stuname; 29 } 30 public Date getBirthday() { 31 return birthday; 32 } 33 public void setBirthday(Date birthday) { 34 this.birthday = birthday; 35 } 36 public Student() { 37 super(); 38 } 39 public Student(int stuno, String stuname, Date birthday) { 40 super(); 41 this.stuno = stuno; 42 this.stuname = stuname; 43 this.birthday = birthday; 44 } 45 46 }
第四步:在hibernate.cfg.xml中增加:
這里我們需要注意的是 使用注解的時候是class 使用xml配置的時候使用的resource

1 <!-- 引入映射文件 --> 2 <mapping class="com.qcf.pox.Student"/>
第五步:寫測試代碼:

1 package com.qcf.test; 2 3 import java.util.Date; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.Transaction; 8 import org.hibernate.cfg.AnnotationConfiguration; 9 import org.hibernate.cfg.Configuration; 10 11 import com.qcf.pox.Student; 12 13 public class TestAnnotation { 14 public static void main(String[] args) { 15 //獲取configuration對象 16 Configuration configuration=new AnnotationConfiguration().configure(); 17 SessionFactory factory=configuration.buildSessionFactory(); 18 Session session=factory.openSession(); 19 //創建student對象 20 Student student=new Student(); 21 student.setStuname("fawfeaw"); 22 student.setBirthday(new Date()); 23 //獲取事務 24 Transaction transaction= session.beginTransaction(); 25 //將student變為持久態 26 session.save(student); 27 //提交事務 28 transaction.commit(); 29 session.close(); 30 } 31 32 }
科普知識:
常見的注解及其典型用法
注解名稱
作用
典型代碼/其他
@Entity
將一個類聲明為一個實體bean(即一個持久化POJO類)
@Entity
public class User {
//…類體省略
}
@Table
注解聲明了該實體bean映射指定的表(table),目錄(catalog)和schema的名字
@Entity
@Table(name="_user")
public class User {
//…省略類體
}
@Id
注解聲明了該實體bean的標識屬性(對應表中的主鍵)
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
private Date birthday;
//省略了get和set方法
}
@GeneratedValue
聲明了主鍵的生成策略。該注解有如下屬性
@Column
聲明了屬性到列的映射
@Column(name="_uname")
private String name;
@Transient
聲明的屬性不會跟數據庫關聯
@Transient
private Date birthday;
則數據庫中不會有birthday列
@Formula
用一個查詢語句動態的生成一個類的屬性. 表示這個屬性是一個虛擬的列,表中並沒有這個列。需要通過查詢語句計算出來。
@Formula("(select count(*) from _user u where u.id>id)")
private int countUser;
要點:
- Sql語句必須位於括號中
- 這里最好寫完全的sql語句。表名最好使用別名
- 如果要引用當前對象的屬性值,可以直接使用屬性,如:
u.id>id
第二個id就是對象的值!