hibernate注解annotation入门
2017-08-16 08:45
375 查看
前面的文章示例中我们写了实体类之后,都会写一个对应的*.hbm.xml配置文件,这种方式比较复杂,今天我们就来简单了解下使用注解annotation来替代配置文件的方法。
新建一个java项目,结构如下:
Book实体类,在实体类中使用注解,代码如下:
package com.test.pojo;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
//entity表示需要持久化的实体类
@Entity
//表示实体类所对应的表
@Table(name="t_book")
public class Book {
//id主键
@Id
//指定主键生成策略
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="name")
private String name;
@Column
private double price;
private String author;
private Date pubDate;
//省略get/set
}将实体类关联到hibernate.cfg.xml配置中
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql:///hibernatetest
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping class="com.test.pojo.Book" />
</session-factory>
</hibernate-configuration>HibernateUtil类
public class HibernateUtil {
private static Configuration cfg=null;
private static SessionFactory factory=null;
private static Session session=null;
static{
cfg=new Configuration().configure();
factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
}
public static Session getSession(){
if(factory!=null)
return session=factory.openSession();
factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
return session=factory.openSession();
}
public static void closeSession(){
if(session!=null && session.isOpen())
session.close();
}测试类HibernateTest
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg=new Configuration().configure();
SchemaExport se=new SchemaExport(cfg);
se.create(true, true);
}
@Test
public void testSave(){
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
Book book=new Book();
book.setName("读者");
book.setPrice(21.5);
book.setAuthor("读者出版传媒有限公司");
book.setPubDate(new Date());
session.save(book);
tx.commit();
HibernateUtil.closeSession();
}
}
执行testCreateDB,打印信息如下:
drop table if exists t_book
create table t_book (
id integer not null auto_increment,
author varchar(255),
name varchar(255),
price double precision,
pubDate datetime,
primary key (id)
)执行testSave,打印信息如下:
Hibernate:
insert
into
t_book
(author, name, price, pubDate)
values
(?, ?, ?, ?)
数据库表信息如下:
新建一个java项目,结构如下:
Book实体类,在实体类中使用注解,代码如下:
package com.test.pojo;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
//entity表示需要持久化的实体类
@Entity
//表示实体类所对应的表
@Table(name="t_book")
public class Book {
//id主键
@Id
//指定主键生成策略
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="name")
private String name;
@Column
private double price;
private String author;
private Date pubDate;
//省略get/set
}将实体类关联到hibernate.cfg.xml配置中
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql:///hibernatetest
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping class="com.test.pojo.Book" />
</session-factory>
</hibernate-configuration>HibernateUtil类
public class HibernateUtil {
private static Configuration cfg=null;
private static SessionFactory factory=null;
private static Session session=null;
static{
cfg=new Configuration().configure();
factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
}
public static Session getSession(){
if(factory!=null)
return session=factory.openSession();
factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
return session=factory.openSession();
}
public static void closeSession(){
if(session!=null && session.isOpen())
session.close();
}测试类HibernateTest
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg=new Configuration().configure();
SchemaExport se=new SchemaExport(cfg);
se.create(true, true);
}
@Test
public void testSave(){
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
Book book=new Book();
book.setName("读者");
book.setPrice(21.5);
book.setAuthor("读者出版传媒有限公司");
book.setPubDate(new Date());
session.save(book);
tx.commit();
HibernateUtil.closeSession();
}
}
执行testCreateDB,打印信息如下:
drop table if exists t_book
create table t_book (
id integer not null auto_increment,
author varchar(255),
name varchar(255),
price double precision,
pubDate datetime,
primary key (id)
)执行testSave,打印信息如下:
Hibernate:
insert
into
t_book
(author, name, price, pubDate)
values
(?, ?, ?, ?)
数据库表信息如下:
相关文章推荐
- hibernate注解annotation入门了解
- 【Hibernate框架开发之四】Hibernate-Annotation常用的注解归总&&ID的生成策略&&联合主键
- Rhythmk 学习 Hibernate 08 - Hibernate annotation 关联关系注解
- Hibernate注解Annotation
- hibernate 注解和xml配置入门
- Annotation——Hibernate中注解的开发
- 深入理解Java:注解(Annotation)自定义注解入门
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- Hibernate -- 注解(Annotation)关系映射
- 深入理解Java:注解(Annotation)自定义注解入门
- Hibernate的Annotation注解
- 深入理解Java:注解(Annotation)自定义注解入门
- hibernate用注解(annotation)配置sequence
- 深入理解Java:注解(Annotation)自定义注解入门
- 深入理解Java:注解(Annotation)自定义注解入门
- hibernate3.4 注解 oracle简单入门例子
- Hibernate的Annotation注解
- 深入理解java注解(Annotation)以及 自定义注解入门
- 深入理解Java:注解(Annotation)自定义注解入门