hibernate的自动创建表以及注解

<property name="hbm2ddl.auto">update</property>

(1). hibernate反向工程 

(2).hibernate的自动创建

1.在hibernate.cfg.xml中增加<propertyname="hbm2ddl.auto">update</property>

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
	<property name="show_sql">true</property>
		<!-- hibernate反向工程 -->
		<property name="hbm2ddl.auto">update</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///zuo?characterEncoding=utf-8</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password"></property>
		
		<mapping class="com.oracle.pojo.Book"/>
		
	</session-factory>
</hibernate-configuration>

2在实体类中配置注解

@Table(name="")name是表的名字

@Entity声明一个实体类为hibernate的pojo

@table(name=”tablename”)声明实体类对应的数据库表

@Id声明主键

package com.oracle.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="books")
public class Book {
	@Id
	@GeneratedValue
	private int id;
	private String name;
	private int  price;
	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Book( String name, int price) {
		super();
	    this.id = id;
		this.name = name;
		this.price = price;
	}
	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 getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	
}

猜你喜欢

转载自blog.csdn.net/weixin_44793200/article/details/88897068