日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

使用hibernate自动生成数据库表

發(fā)布時間:2025/6/15 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用hibernate自动生成数据库表 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

使用hibernate自動生成數(shù)據(jù)庫表??

使用hibernate自動生成數(shù)據(jù)庫表在hibernate3.2以后有兩種方式:

1,使用hbm.xml形式;2,使用注解的形式。

本文介紹第一種方式:

IDE使用的是MyEclipse8.0,數(shù)據(jù)庫使得是mysql5.1,hibernate3.2

1,新建web項目,導入所需的jar包,(此為最關鍵一步,導入的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ù)庫表結構的自動同步。

如何使用呢?很簡單,只要在hibernate.cfg.xml里加上如下代碼

Xml代碼<property?name="hbm2ddl.auto">update</property>??

?

update:表示自動根據(jù)model對象來更新表結構,啟動hibernate時會自動檢查數(shù)據(jù)庫,如果缺少表,則自動建表;如果表里缺少列,則自動添加列。

還有其他的參數(shù):?
create:啟動hibernate時,自動刪除原來的表,新建所有的表,所以每次啟動后的以前數(shù)據(jù)都會丟失。

create-drop:啟動hibernate時,自動創(chuàng)建表,程序關閉時,自動把相應的表都刪除。所以程序結束時,表和數(shù)據(jù)也不會再存在。

PS:數(shù)據(jù)庫要預先建立好,因為hibernate只會建表,不會建庫

《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的使用hibernate自动生成数据库表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。