• 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

  • 相关阅读:
    解决git推不上去1
    django中CBV源码分析
    Form和ModelForm组件
    jquery操作cookie
    django中的中间件
    django中ORM中锁和事务
    django_ajax
    docker安装jenkins 容器,集成python环境
    支付宝第三方支付
    redis基本使用
  • 原文地址:https://www.cnblogs.com/ShawnYang/p/6692523.html
Copyright © 2020-2023  润新知