hibernate之使用Annotation注解搭建项目

王朝学院·作者佚名  2016-05-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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{

//…类体省略

}

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

要点:

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

第二个id就是对象的值!

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航