紧接上一篇,这里Himi直接copy上一篇创建的HelloHibernate项目,然后改名为:HelloAnnonation,Ok;
OK,准备工作:
首先第一步,继续再我们自定义的user libraries 中添加Annotation所需的包,如下3个jar包(共11个包):
/hibernate-annotations-3.4.0.GA/hibernate-annotations.jar (核心包)
/hibernate-annotations-3.4.0.GA/lib/ejb3-persistence.jar (jpa)
/hibernate-annotations-3.4.0.GA/lib/hibernate-commons-annotations.jar (反射所需的包)
如下图 ;
然后我们新建一个People类,如下:
- package com.himi;
- public class People {
- private int id;
- private String name;
- private int age;
- private String title;
- private String birthday;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getBirthday() {
- return birthday;
- }
- public void setBirthday(String birthday) {
- this.birthday = birthday;
- }
- }
最后准备工作在我们数据库中新建一个对应映射people实体类的表:(不赘述,直接看操作过程)
- mysql> use hibernate;
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- mysql> create table people(id int primary key, name varchar(20),age int ,title varchar(20), birthday varchar(20) );
- Query OK, 0 rows affected (0.06 sec)
- mysql> describe people;
- +----------+-------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+-------------+------+-----+---------+-------+
- | id | int(11) | NO | PRI | NULL | |
- | name | varchar(20) | YES | | NULL | |
- | age | int(11) | YES | | NULL | |
- | title | varchar(20) | YES | | NULL | |
- | birthday | varchar(20) | YES | | NULL | |
- +----------+-------------+------+-----+---------+-------+
- 5 rows in set (0.01 sec)
准备工作完成之后,那么如果通常我们会建立People.hbm.xml来对应数据库的组件和属性,然后将People在hibernate.cfg.xml配置文件中使用mapping resource声明我们有一个被加了映射,People是实体类;
但是本篇我们使用Annotation将不再创建对应的实体类对应数据库的xml,而是直接在People类中声明实体类就可以啦,修改People.java文件如下:
- package com.himi;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- @Entity
- public class People {
- private int id;
- private String name;
- private int age;
- private String title;
- private String birthday;
- @Id
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getBirthday() {
- return birthday;
- }
- public void setBirthday(String birthday) {
- this.birthday = birthday;
- }
- }
仔细观察,发现只有两处有改动,就是添加了一个 @Entity 和 @Id
@Entity 表示本类是个实体类,是javax.persistence.Entity
@Id 在组件getId ()函数上约定俗成加入注解 @Id
接着我们继续在hibernate.cfg.xml配置文件中声明我们的People是个映射实体类:
- <mapping resource="com/himi/Teacher.hbm.xml"/> <!-- 这里是将需要mapping的文件进行再次声明 -->
- <mapping class="com.himi.People"/> <!-- 这里是声明我们的People是个映射实体类-->
上面的Teacher是上一篇介绍的,People是本篇中新建的实体类, 务必注意,两种方式的区别:
一个是 mapping resource , 一个是 mapping class ;
一个路径是 com/himi/Teacher.hbm.xml 一个路径的 com.himi.People ;
最后一步,新建一个测试People的man类,名字为MainTestPeople.java:
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.AnnotationConfiguration;
- import org.hibernate.cfg.Configuration;
- import com.himi.People;
- public class MainTestPeople {
- /**
- * @param args
- */
- public static void main(String[] args) {
- People people = new People();// 新建我们需要存储的类对象,并且设置其对象的一些属性
- people.setId(2);
- people.setName("Himi");
- people.setAge(22);
- people.setTitle("CTO");
- people.setBirthday("1990-01-01");
- // Configuration主要用以读取配置文件
- //AnnotationConfiguration 专用于读取与Annotation相关的配置
- Configuration cfg = new AnnotationConfiguration();
- SessionFactory sf = cfg.configure().buildSessionFactory();
- // 这里注意了,cfg.configure()读取配置文件的时候,如果你的hibernate的文件名不采用默认的“hibernate.cfg.xml”的话,那么这里传入你定义的配置文件路径
- // buildSessionFactory();得到一个创建Session的工场
- Session ss = sf.openSession();// 这里的Session导入import
- // org.hibernate.Session;不要使用class经典的,因为可能会过时
- ss.beginTransaction();// OK,将操作放入事务中
- ss.save(people);// 保存你的对象
- ss.getTransaction().commit();// 得到事务并提交
- ss.close();// Session关闭
- sf.close();// 工厂关闭
- }
- }
仔细以上观看代码,与上一篇测试我们的Teacher 一致,唯一要注意的是 Configuration的实例:
不使用Annotation实例配置对象,我们采用如下获取Configuration:
Configuration cfg = new Configuration();
使用Annotation时,我们采用如下获取Configuration:
Configuration cfg = new AnnotationConfiguration();
OK, 右键MainTestPeople, run as -> Java application;
运行结果:
- Hibernate: insert into People (age, birthday, name, title, id) values (?, ?, ?, ?, ?)
ok,监测一下数据库的People表内是否已经成功插入了信息:
- mysql> select *from people;
- +----+------+------+-------+------------+
- | id | name | age | title | birthday |
- +----+------+------+-------+------------+
- | 1 | Himi | 23 | CTO | 1990-01-01 |
- | 2 | Himi | 22 | CTO | 1990-01-01 |
- +----+------+------+-------+------------+
- 2 rows in set (0.00 sec)
没问题。本篇介绍Annotation第一个项目就到这里,比较eazy,而且Annotation使用起来相当方便!