一、写bean实体类Book.java,并注解
1. package edu.hzu.entity;
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.ManyToOne;
import javax.persistence.Table;
@Entity
@Table (name = "book")
public class Book {
private int id;
private String name;
private Author author;
private Press press;
private Date publishdate;
private int pageCount;
private float price;
private String introduction;
private String cover;
private BookCategory bookCategory;
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column (name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne (targetEntity = edu.hzu.entity.Author.class)
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
@ManyToOne (targetEntity = edu.hzu.entity.Press.class)
public Press getPress() {
return press;
}
public void setPress(Press press) {
this.press = press;
}
@Column (name = "date")
public Date getPublishdate() {
return publishdate;
}
public void setPublishdate(Date publishdate) {
this.publishdate = publishdate;
}
@Column (name = "pageCount")
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
@Column (name = "price")
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Column (name = "introduction")
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
@Column (name = "cover")
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
@ManyToOne (targetEntity = edu.hzu.entity.BookCategory.class)
public BookCategory getBookCategory() {
return bookCategory;
}
public void setBookCategory(BookCategory bookCategory) {
this.bookCategory = bookCategory;
}
}
二、在src根目录下见hibernate.cfg.xml文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="edu.hzu.entity.Book"/>
<mapping class="edu.hzu.entity.BookCategory"/>
<mapping class="edu.hzu.entity.Author"/>
<mapping class="edu.hzu.entity.AuthorCategory"/>
<mapping class="edu.hzu.entity.Site"/>
<mapping class="edu.hzu.entity.Price"/>
<mapping class="edu.hzu.entity.Press"/>
</session-factory>
</hibernate-configuration>
三、做测试类Test.java
package edu.hzu.test;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
public class Test {
public static void main(String[] args) {
AnnotationConfiguration a = new AnnotationConfiguration();
Session session = a.configure().buildSessionFactory().openSession();
}
}
通过此测试类即可在数据库中生成数据库及表(当然这只是一个例子,其实只给出了一个hibernate使用的框架,要真间数据库,还得在对应的建相关的类:Author、BookCategory、Press等,方法与Book类似)