使用hibernate自动生成数据库表
使用hibernate自動生成數(shù)據(jù)庫表??
使用hibernate自動生成數(shù)據(jù)庫表在hibernate3.2以后有兩種方式:
1,使用hbm.xml形式;2,使用注解的形式。
本文介紹第一種方式:
IDE使用的是MyEclipse8.0,數(shù)據(jù)庫使得是mysql5.1,hibernate3.2
1,新建web項目,導(dǎo)入所需的jar包,(此為最關(guān)鍵一步,導(dǎo)入的jar包一定要匹配)。
2,新建實體類User.java
package entity;
public class User {
private int id;
private String name;
private String password;
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 String getPassword() {
?return password;
}
public void setPassword(String password) {
?this.password = password;
}
public User() {
?super();
}
}
3,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">
<!--
??? Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
??? <class name="entity.User" table="user" catalog="mysong">
??????? <id name="id" type="java.lang.Integer">
??????????
??????????? <generator class="native" />
??????? </id>
??????? <property name="name" type="java.lang.String">
??????????
??????? </property>
??????
??????? <property name="password" type="java.lang.String">
??????????
??????? </property>
??? </class>
</hibernate-mapping>
4,hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
????????? "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
????????? "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.?????????????????? -->
<hibernate-configuration>
??? <session-factory>
??? ?<property name="show_sql">true</property>
??? ?
??????? <property name="hbm2ddl.auto">update</property>
??????? <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
??????? <property name="connection.url">jdbc:mysql://localhost:3306/mysong</property>
??????? <property name="connection.username">root</property>
??????? <property name="connection.password">root</property>
??????? <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
??????? <property name="myeclipse.connection.profile">com.mysql.jdbc.Driver</property>
????????
?????????????? <mapping resource="entity/User.hbm.xml" />
??????????????????
????????????? </session-factory>
</hibernate-configuration>
5,部署項目,啟動tomcat,并打開項目的任何一個頁面,此時數(shù)據(jù)庫表已經(jīng)生成了,趕快查看數(shù)據(jù)庫吧!
Hibernate支持自動建表,在開發(fā)階段很方便,可以保證hbm與數(shù)據(jù)庫表結(jié)構(gòu)的自動同步。
如何使用呢?很簡單,只要在hibernate.cfg.xml里加上如下代碼
Xml代碼<property?name="hbm2ddl.auto">update</property>???
update:表示自動根據(jù)model對象來更新表結(jié)構(gòu),啟動hibernate時會自動檢查數(shù)據(jù)庫,如果缺少表,則自動建表;如果表里缺少列,則自動添加列。
還有其他的參數(shù):?
create:啟動hibernate時,自動刪除原來的表,新建所有的表,所以每次啟動后的以前數(shù)據(jù)都會丟失。
create-drop:啟動hibernate時,自動創(chuàng)建表,程序關(guān)閉時,自動把相應(yīng)的表都刪除。所以程序結(jié)束時,表和數(shù)據(jù)也不會再存在。
PS:數(shù)據(jù)庫要預(yù)先建立好,因為hibernate只會建表,不會建庫
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的使用hibernate自动生成数据库表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring整合Hibernate中自动
- 下一篇: Hibernate中自动生成数据库表的两