前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
1. 在 hibernate.cfg.xml 添加這句話,可以自動生成數據表 :
<property name="hibernate.hbm2ddl.auto">update</property>
update:表示自動根據 model 對象來更新表結構,啟動 hibernate 時會自動檢查數據庫。如果缺少表,則自動建表;如果表里缺少列,則自動添加列 。?
其他參數:?
create:啟動 hibernate 時,自動刪除原來的表。新建所有的表,所以每次啟動后的以前數據都會丟失。?
create-drop:啟動 hibernate 時,自動創建表。程序關閉時,自動把相應的表都刪除。所以程序結束時,表和數據也不會再存在。?
PS:數據庫要預先建立好,因為?hibernate 只會建表,不會建庫。
表結構和數據總是在程序執行的時候無端的修改要配置:? ? ?
<property name="hibernate.hbm2ddl.auto" value="update" />
2. hibernate.hbm2ddl.auto Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. eg. validate | update | create | create-drop
其實這個參數的作用主要用于:自動創建|更新|驗證數據庫表結構。
如果沒有此方面的需求建議 set value="none".
其它參數 :
validate 加載hibernate時,驗證創建數據庫表結構
create 每次加載hibernate,重新創建數據庫表結構
create-drop 加載hibernate時創建,退出是刪除表結構
update 加載hibernate自動更新數據庫結構
?
ps : 如果發現數據庫表丟失或新增,請檢查 hibernate.hbm2ddl.auto 的配置?
可設置?<property?name="hibernate.hbm2ddl.auto"?value="none"?/>
轉自:https://blog.csdn.net/zwhfyy/article/details/4514966