之前开发都是使用xml配置来开发项目,开发起来特别繁琐

大家会发现通过注解大大简化了我们开发流程,使我们从繁琐的XML配置中解放出来。

第一步:新建一个javaweb项目。并将hibernate需要的相关jar包导入。

第二步: 使用annotation需要额外增加3个jar包:

◦ hibernate-annotations.jar

◦ ejb3-persistence.jar

◦ hibernate-commons-annotations.jar

第三步:新建一个pojo并增加注解:

 package com.qcf.pox;

 import java.util.Date;

 import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; @Entity
public class Student { @Id
@GeneratedValue(strategy=GenerationType.AUTO)//代表主键的生成策略
private int stuno;
private String stuname;
private Date birthday;
public int getStuno() {
return stuno;
}
public void setStuno(int stuno) {
this.stuno = stuno;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Student() {
super();
}
public Student(int stuno, String stuname, Date birthday) {
super();
this.stuno = stuno;
this.stuname = stuname;
this.birthday = birthday;
} }

第四步:在hibernate.cfg.xml中增加:

这里我们需要注意的是 使用注解的时候是class   使用xml配置的时候使用的resource

         <!-- 引入映射文件 -->
<mapping class="com.qcf.pox.Student"/>

第五步:写测试代码:

 package com.qcf.test;

 import java.util.Date;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration; import com.qcf.pox.Student; public class TestAnnotation {
public static void main(String[] args) {
//获取configuration对象
Configuration configuration=new AnnotationConfiguration().configure();
SessionFactory factory=configuration.buildSessionFactory();
Session session=factory.openSession();
//创建student对象
Student student=new Student();
student.setStuname("fawfeaw");
student.setBirthday(new Date());
//获取事务
Transaction transaction= session.beginTransaction();
//将student变为持久态
session.save(student);
//提交事务
transaction.commit();
session.close();
} }

科普知识:

常见的注解及其典型用法

注解名称

作用

典型代码/其他

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

//省略了get和set方法

}

@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就是对象的值!

最新文章

  1. lua 闭包
  2. UITextField 基本属性使用
  3. apache泛域名的配置
  4. MySQL 5.7.9的多源复制
  5. 博弈论(男人八题):POJ 1740 A New Stone Game
  6. asp.net微软认证全新考试题库及答案1
  7. C语言中的string.h中的内存字符串处理函数
  8. hibernate 插入数据时让数据库默认值生效
  9. 记录linux tty的一次软锁排查2
  10. 自动化测试(二) 单元测试junit的Test注解突然不能使用原因以及解决方案
  11. servlet什么时候被实例化?【转】
  12. 使用maven整合spring+springmvc+mybatis
  13. java设定一个日期时间,加几分钟(小时或者天)后得到新的日期
  14. 06:合法 C 标识符
  15. C# ADO.NET与面向对象
  16. 并发编程之 ConcurrentHashMap(JDK 1.8) putVal 源码分析
  17. Mysql查看连接数相关信息
  18. 在控制台远程连接mysql数据库时,出现ERROR 2049 (HY000)错误
  19. Android之Glide(非常好用的图片加载框架)
  20. Maven(八) Maven项目和testng结合应用

热门文章

  1. xsd的解释说明
  2. structs2注解+jsp+ajax实现post异步载入select
  3. Android.9图片评论(一个)
  4. HDU3549 Flow Problem 【最大流量】
  5. 什么是简单的分析SQL注入漏洞
  6. IT薪酬
  7. wordpress常见的问题
  8. 必要的软件架构师——编译原理&amp;#183;语法
  9. obj-c编程04:类的继承
  10. [置顶] 纯手工打造漂亮的瀑布流,五大插件一个都不少Bootstrap+jQuery+Masonry+imagesLoaded+Lightbox!