国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

hibernate之使用Annotation注解搭建項目

2019-11-14 21:58:17
字體:
來源:轉載
供稿:網友
hibernate之使用Annotation注解搭建項目

之前開發都是使用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     @Id14     @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 }
View Code

第四步:在hibernate.cfg.xml中增加:

這里我們需要注意的是 使用注解的時候是class 使用xml配置的時候使用的resource

1         <!-- 引入映射文件 -->2         <mapping class="com.qcf.pox.Student"/>
View Code

第五步:寫測試代碼:

 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 }
View Code

科普知識:

常見的注解及其典型用法

注解名稱

作用

典型代碼/其他

@Entity

將一個類聲明為一個實體bean(即一個持久化POJO類)

@Entity

publicclassUser{

//&hellip;類體省略

}

@Table

注解聲明了該實體bean映射指定的表(table),目錄(catalog)和schema的名字

@Entity

@Table(name="_user")

publicclassUser{

//…省略類體

}

@Id

注解聲明了該實體bean的標識屬性(對應表中的主鍵)

@Entity

publicclassUser{

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

privateintid;

privateStringname;

privateDatebirthday;

//省略了get和set方法

}

@GeneratedValue

聲明了主鍵的生成策略。該注解有如下屬性

@Column

聲明了屬性到列的映射

@Column(name="_uname")

privateStringname;

@Transient

聲明的屬性不會跟數據庫關聯

@Transient

privateDatebirthday;

則數據庫中不會有birthday列

@Formula

用一個查詢語句動態的生成一個類的屬性.表示這個屬性是一個虛擬的列,表中并沒有這個列。需要通過查詢語句計算出來。

@Formula("(selectcount(*)from_useruwhereu.id>id)")

privateintcountUser;

要點:

  1. Sql語句必須位于括號中
  2. 這里最好寫完全的sql語句。表名最好使用別名
  3. 如果要引用當前對象的屬性值,可以直接使用屬性,如:

u.id>id

第二個id就是對象的值!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 垦利县| 金秀| 沐川县| 互助| 遂溪县| 绥化市| 永修县| 凭祥市| 宁安市| 那坡县| 信阳市| 贵定县| 朝阳市| 兴业县| 策勒县| 靖边县| 图片| 重庆市| 涞源县| 扶沟县| 甘德县| 秭归县| 子长县| 扎兰屯市| 连云港市| 沙洋县| 河西区| 天等县| 灌南县| 双辽市| 库尔勒市| 崇仁县| 溆浦县| 丰县| 黄大仙区| 巍山| 富平县| 海晏县| 泸西县| 云和县| 周口市|