在Hibernate中的启动数据库创建mysql

时间:2014-12-30 10:26:21

标签: mysql spring hibernate

我正在写spring mvc项目。我想在我的应用程序的startUp创建mysql数据库。试图读取文件“import.sql”。 我收到了一个错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'test3'

这是我的spring-servlet.xml:

    <context:annotation-config />
<context:component-scan base-package="com.joseph" />    


<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
    <value>
        hibernate.dialect=${hibernate.dialect}
        hibernate.jdbc.batch_size=${hibernate.jdbc.batch_size}
        hibernate.show_sql=${hibernate.show_sql}
        hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
        hibernate.id.new_generator_mappings=${hibernate.id.new_generator_mappings}
        hibernate.hbm2ddl.import_files=${hibernate.hbm2ddl.import_files}

     </value>
</property>

</bean>


<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
<tx:annotation-driven />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean> 

和jdbc.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://localhost:3306/test3  
jdbc.username=root
jdbc.password=root

    #org.hibernate.dialect.PostgreSQLDialect
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
    hibernate.show_sql = true
    hibernate.id.new_generator_mappings = true
    hibernate.hbm2ddl.auto = create
    hibernate.jdbc.batch_size = 5
    hibernate.transaction.factory_class=org.transaction.JDBCTransactionFactory
    hibernate.hbm2ddl.import_files = import.sql 

import.sql

CREATE SCHEMA IF NOT EXISTS test3;

的hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>   
     <mapping class="com.joseph.model.Student" />        
    </session-factory>         
</hibernate-configuration>

1 个答案:

答案 0 :(得分:0)

在资源中添加import.sql文件,如下所示:

/*create database at first time*/ 
CREATE SCHEMA your-database-name;

'hibernate.cfg.xml'如下:

<hibernate-configuration>
    <session-factory>
       ...
       ...
       <property name="hbm2ddl.import_files">import.sql</property>
       ...
       ...
    </session-factory>
</hibernate-configuration>

另请参阅此步骤以编程方式执行this