zoukankan      html  css  js  c++  java
  • 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     @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 }
    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

    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;

       //省略了getset方法

    }

    @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;

    要点:

    1. Sql语句必须位于括号中
    2. 这里最好写完全的sql语句。表名最好使用别名
    3. 如果要引用当前对象的属性值,可以直接使用属性,如:

    u.id>id

    第二个id就是对象的值!

     

     

  • 相关阅读:
    day7 面向对象 静态方法 类方法 属性方法 类的特殊成员方法 元类 反射 异常处理
    day6 面向对象 封装 继承 多态 类与实例在内存中的关系 经典类和新式类
    day5 time datetime random os sys shutil json pickle shelve xml configparser hashlib subprocess logging re正则 python计算器
    kafka常用操作命令
    linux基础
    django学习1——初识web应用程序
    mysql数据库(三)——pymysql模块
    mysql数据库(二)——表的查询
    mysql数据库(一)
    Python常用模块——re模块
  • 原文地址:https://www.cnblogs.com/quchengfeng/p/4110698.html
Copyright © 2011-2022 走看看