博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate生成数据库表
阅读量:6884 次
发布时间:2019-06-27

本文共 1803 字,大约阅读时间需要 6 分钟。

hot3.png

hibernate生成数据库表

注:使用前需把数据库建好,运行完成后,原有数据会被清空

hibernate.cfg.xml

com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc:sqlserver://192.168.56.101:1433;databasename=TestDB
sa
admin
org.hibernate.dialect.SQLServerDialect
true
org.hibernate.cache.HashtableCacheProvider
true
true
true
2
5000
100
3000
2
false

Java 代码

import java.io.File;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;/** * 通过hibernate配置文件生成数据库表 *  * @author Albert Smith * */public class InitDB {	static Session session;	public static void main(String[] args) {		Configuration config = null;		Transaction tx = null;		try {			config = new Configuration().configure(new File("config/hibernate.cfg.xml"));   //读取hibernate配置文件			System.out.println("开始创表");			SchemaExport schemaExport = new SchemaExport(config);			schemaExport.create(true, true);			System.out.println("创建结束");			SessionFactory sessionFactory = config.buildSessionFactory();			session = sessionFactory.openSession();			tx = session.beginTransaction();			tx.commit();		} catch (HibernateException e) {			e.printStackTrace();			try {				tx.rollback();			} catch (HibernateException e1) {				e1.printStackTrace();			}		} finally {		}	}}

每次修改hibernate配置文件后,运行一下,就可以自动更新表结构。

转载于:https://my.oschina.net/albert2011/blog/691219

你可能感兴趣的文章
关于OOM-killer
查看>>
Wireshark网络抓包(一)——数据包、着色规则和提示
查看>>
GOP/ 码流 /码率 / 比特率 / 帧速率 / 分辨率
查看>>
学习一门编程语言的各种矛盾
查看>>
sqlmap简单使用笔记
查看>>
Eclipse ME 安装详解(Windows XP)
查看>>
IE8及以下不支持trim()的处理方法
查看>>
Alpha 冲刺 (5/10)
查看>>
类的静态字段和构造函数
查看>>
TLE之前,没有一个节点叫失败!!!
查看>>
机器学习入门之二:一个故事说明什么是机器学习(转载)
查看>>
利用MySQL存储过程分割字符串
查看>>
Webkit statistics of Android
查看>>
哥德巴赫猜想
查看>>
动态规划3--Help Jimmy
查看>>
Java常用英语汇总(面试必备)
查看>>
负载均衡的认识
查看>>
Swing-选项卡面板JTabbedPane-入门
查看>>
(21/24) webpack实战技巧:webpack对三方类库的优化操作
查看>>
复利完结观看评价
查看>>