使用hibernate自动建表时需要注意的是hibernate不会去创建数据库,所以数据库的创建要手动去完成。创建数据库后就可以通过实体类由hibernate自动去创建数据库表了。
hibernate配置文件hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!--指定数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--在控制台显示执行的数据库操作语句 -->
<property name="hibernate.show_sql">true</property>
<!--在控制台显示执行的数据库操作语句(格式) -->
<property name="hibernate.format_sql">true</property>
<!--hibernate根据实体自动生成数据库表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置 CurrentSession-->
<property name="hibernate.current_session_context_class">thread</property>
<!--引入映射文件 -->
<mapping resource="demo/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
创建实体类 user.java
public class User {
private int userId;
private String username;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
创建User类的映射文件User.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="demo.entity">
<class name="User" table="user" lazy="true">
<!-- 设置主键 -->
<id name="userId" column="userId">
<!-- id生成策略 -->
<generator class="native"/>
</id>
<!-- unique="true"唯一约束 -->
<property name="username" column="username" unique="true"/>
</class>
</hibernate-mapping>
运行main方法
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args) {
// 读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
// 建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
}
}
附截图
hibernate配置文件hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!--指定数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--在控制台显示执行的数据库操作语句 -->
<property name="hibernate.show_sql">true</property>
<!--在控制台显示执行的数据库操作语句(格式) -->
<property name="hibernate.format_sql">true</property>
<!--hibernate根据实体自动生成数据库表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置 CurrentSession-->
<property name="hibernate.current_session_context_class">thread</property>
<!--引入映射文件 -->
<mapping resource="demo/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
创建实体类 user.java
public class User {
private int userId;
private String username;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
创建User类的映射文件User.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="demo.entity">
<class name="User" table="user" lazy="true">
<!-- 设置主键 -->
<id name="userId" column="userId">
<!-- id生成策略 -->
<generator class="native"/>
</id>
<!-- unique="true"唯一约束 -->
<property name="username" column="username" unique="true"/>
</class>
</hibernate-mapping>
运行main方法
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args) {
// 读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
// 建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
}
}