hibernate之使用Annotation注解搭建项目
之前开发都是使用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;
要点:
- Sql语句必须位于括号中
- 这里最好写完全的sql语句。表名最好使用别名
- 如果要引用当前对象的属性值,可以直接使用属性,如:
u.id>id
第二个id就是对象的值!
hibernate之使用Annotation注解搭建项目的更多相关文章
- Spring之使用Annotation注解开发项目
我们也可以使用Annotation来实现注入操作,提高我们写代码的灵活性和效率.spring中要使用annotation,需要在配置文件中增加: <beans xmlns="http: ...
- Java - 使用hibernate配置文件 + JPA annotation注解操作数据库
本程序运行环境:IDEA. 实际上我对hiberbate与注解的关系还不是太清晰.据我所知注解都是Java JPA的,那么我的理解是:hibernate就应该只是通过这些JPA标识及hibernate ...
- Hibernate的Annotation注解
当项目变得比较大的时候,如何还使用hbm.xml文件来配置Hibernate实体就会变得比较复杂.这里Hibernate提供了Annotation注解方式,使得Hibernate的映射文件变得很方便管 ...
- 【maven + hibernate(注解) +spring +springMVC】 使用maven搭建项目
研究,百度,查资料+好友帮助,使用MyEcplise2015工具,通过maven搭建hibernate+springMVC+spring的项目,数据库采用MySql5.5 不过使用的版本会在项目搭建过 ...
- Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)
1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...
- hibernate annotation注解方式来处理映射关系
在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ...
- Hibernate or JPA Annotation中BLOB、CLOB注解写法
BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...
- Hibernate 零配置之Annotation注解
JPA规范推荐使用Annotation来管理实体类与数据表之间的映射关系,从而避免同时维护两份文件(Java 实体类 和 XML 映射文件),将映射信息(写在Annotation中)与实体类集中在一起 ...
- SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分
SpringMVC,Spring,Hibernate,Mybatis架构开发搭建之SpringMVC部分 辞职待业青年就是有很多时间来写博客,以前在传统行业技术强度相对不大,不处理大数据,也不弄高并发 ...
随机推荐
- CodeForces 396C 树状数组 + DFS
本主题开始看到以为段树或树状数组,但是,对于一个节点的有疑问的所有子节点的加权,这一条件被视为树的根,像 然后1号是肯定在第一层中,然后建立一个单向侧倒查,然后记录下来 其中每个节点 层,终于 两个节 ...
- Android虚拟机器学习总结Dalvik虚拟机创建进程和线程分析
Dalvik调用一个成员函数时,虚拟机,假设发现,该成员函数是一个JNI办法,然后,它会直接跳转到其地址来运行.也就是说.JNI方法是直接在本地操作系统上运行的.而不是由Dalvik虚拟机解释器运行. ...
- Chapter 3 Protecting the Data(3):创建和使用数据库角色
原版的:http://blog.csdn.net/dba_huangzj/article/details/39639365.专题文件夹:http://blog.csdn.net/dba_huangzj ...
- gem 安装nokigiri
在mac上安装nokogiri的时候各种报错,终于安装成功一次,备份命令. ➜ ~ sudo gem install nokogiri -- --use-system-libraries --with ...
- VS路宏 vc++于OutDir、ProjectDir、SolutionDir不同的路径
说明 $(RemoteMachine) 设置为"调试"属性页上"远程计算机"属性的值.有关很多其它信息,请參见更改用于 C/C++ 调试配置的项目设置. $(R ...
- Lucene于Directory
MMapDirectory从继承FSDirectory,抵抗jre至今未能解决Mmap close不回收空间(直到full gc恢复之前,)的bug,lucene使用hack资料恢复(只要sun ja ...
- Git是个好工具(转)
Git是分布式版本控制系统,我们常用的版本控制工具还有SVN.这里就得区分下什么是分布式版本控制系统,什么是集中化的版本控制系统. 集中化的版本控制系统 集中化的版本控制系统( Centralized ...
- poj1083 思考题
http://poj.org/problem?id=1083 Description The famous ACM (Advanced Computer Maker) Company has rent ...
- hdu 4864 Task---2014 Multi-University Training Contest 1
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4864 Task Time Limit: 4000/2000 MS (Java/Others) M ...
- contentWindow,
a>contentWindow 兼容各个浏览器,可取得子窗口的 window 对象.b>contentDocument Firefox 支持,> ie8 的ie支持.可取得子窗口的 ...