(一)利用hibernate影射文件在oracle中生成对应的数据库和表
我用的是hibernate和Spring配置生成的,在Spring的配置文件配置如下:
<!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="mappingDirectoryLocations"> <list> <value>classpath:/standing/common/pojo/hbm/</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.OracleDialect </prop> <prop key="hibernate.jdbc.batch_size">25</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">validate</prop> </props> </property> </bean>
启动项目时即可在oracle中生成数据库。
(二)利用hibernate影射文件在mysql中生成对应的数据库和表
①同样利用hibernate和Spring配置生成,在session-factory中的配置如下:
<session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.url"> jdbc:mysql://localhost:3306/xdatabasename </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"> myDriver </property> <property name="jdbc.use_scrollable_resultset">false</property> <property name="hibernate.connection.pool.size">20</property> <property name="hibernate.show_sql">true</property> <property name="jdbc.fetch_size">50</property> <property name="jdbc.batch_size">25</property> <property name="format_sql">true</property> <property name="show_sql">true</property> <mapping resource="XX/po/X.hbm.xml" /> </session-factory>
②需要写一个main方法,代码如下:
package xX.test; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class CreateDatabase { public static void main(String[] args) { Configuration cfg = new Configuration().configure(); SchemaExport schemaExport= new SchemaExport(cfg); schemaExport.create(false, true); } }
注意:需要先在数据库中建一个名字是xdatabasename的空数据库。