码迷,mamicode.com
首页 > 其他好文 > 详细

Annotation版本的HelloWorld

时间:2017-04-11 11:18:57      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:each   map   turn   static   gif   integer   one   char   情况   

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 - 加上@

Annotation版本的HelloWorld

标签:each   map   turn   static   gif   integer   one   char   情况   

原文地址:http://www.cnblogs.com/ShawnYang/p/6692523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!