Hibernate 自动创建数据库 [英] Hibernate auto create database
问题描述
我有一个 Java EE Hibernate 项目,我使用 MySQL 作为数据库.
I have a Java EE Hibernate project, and I'm using MySQL as a database.
我希望当我第一次运行项目时,它会自动创建数据库.
I want that when I first time run the project, it will create the database automatically.
这是我的hibernate.cnf.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" >
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/InternetProject</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="entities.Business" />
<mapping class="entities.Coupon" />
<mapping class="entities.User" />
<mapping class="entities.LastLogin" />
</session-factory>
</hibernate-configuration>
当我第一次在另一台电脑上运行这个项目时,如何创建数据库InternetProject
?
When I first time run this project on another computer, how can I make the database InternetProject
to be created?
根据配置文件,它可能已经这样做了,我不知道.
According to the config file, it might already do it and I'm not aware to it.
提前致谢.
推荐答案
将创建表.但它不会创建数据库.更改连接 url 以生成数据库.
create
<property name="hibernate.hbm2ddl.auto">create</property>
will create tables. But it will not create database. Change the connection url to generate the database.
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.pool_size">10</property>
更新:创建数据库时的字符编码
Update : character encoding when creating database
<property name="connection.url">
jdbc:mysql://localhost/InternetProject?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8
</property>
这篇关于Hibernate 自动创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!