日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Annotation版本的HelloWorld

發布時間:2023/11/29 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Annotation版本的HelloWorld 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

hiberante 的 annotation歷史:

在hibernate3以后,開始支持Annotation;

先有hiberante再有JPA,有了JPA標準之后,hibernate寫了Annotation來支持JPA;
所以 hibernate的annotation是JPA標準之下的,一般都直接用JPA的annotation,hibernate的annotation只有在極少的情況下才使用。

1、創建teacher表,create table teacher(id int primary key, name varchar(20),title varchar(10));
2、創建 Teacher 類

3、在 hibernate lib 中加入 annotation 的 jar 包

在 hibernate 這個 User Libaray 中添加支持annotation的jar包:

  a) hibernate annotations jar


  b) ejb3 persistence jar
  c) hibernate common annotations jar
  d) 注意文檔中沒有提到 hibernate common annotations jar 文件

4、參考 Annotation 文檔建立對應的注解

為 Teacher 類添加注解

1 package com.bjsxt.hibernate.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.Id; 5 import javax.persistence.Table; 6 7 @Entity 8 @Table(name="teacher") 9 public class Teacher { 10 11 private Integer id; 12 13 private String name; 14 15 private String title; 16 17 @Id 18 public Integer getId() { 19 return id; 20 } 21 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public String getTitle() { 35 return title; 36 } 37 38 public void setTitle(String title) { 39 this.title = title; 40 } 41 42 }

注意:類屬性的注解一般都寫在該屬性的get方法上,約定俗成:如id屬性的注解寫在getId()上。

5、在 hibernate.cfg.xml 建立對應映射 <mapping class=... />

  Teacher的映射:<mapping class="com.bjsxt.hibernate.model.Teacher" />

6、參考文檔進行測試(注意文檔中缺少 configure()的小bug)

1 import org.hibernate.Session; 2 import org.hibernate.SessionFactory; 3 import org.hibernate.cfg.AnnotationConfiguration; 4 import org.hibernate.cfg.Configuration; 5 6 import com.bjsxt.hibernate.model.Teacher; 7 8 public class TeacherTest { 9 10 public static void main(String[] args) { 11 Teacher t = new Teacher(); 12 t.setId(1); 13 t.setName("t1"); 14 t.setTitle("中級"); 15 16 //改為new AnnotationConfiguration() 17 Configuration cfg = new AnnotationConfiguration(); 18 SessionFactory sf = cfg.configure().buildSessionFactory(); 19 Session session = sf.openSession(); 20 session.beginTransaction(); 21 22 session.save(t); 23 24 session.getTransaction().commit(); 25 session.close(); 26 sf.close(); 27 } 28 29 } View Code

注意 Configuration cfg = new AnnotationConfiguration();

鏈接: http://pan.baidu.com/s/1eSvHWbg 密碼: 63cy

所需jar包:鏈接: http://pan.baidu.com/s/1dEDKHln 密碼: s4bq

FAQ:@不給提示
a) content assist - activation - 加上@,具體參考:http://www.cnblogs.com/ShawnYang/p/6692735.html

轉載于:https://www.cnblogs.com/ShawnYang/p/6692523.html

總結

以上是生活随笔為你收集整理的Annotation版本的HelloWorld的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。