Fla*_*vio 1 java spring hibernate h2
我想将H2内存数据库添加到我的Spring MVC应用程序中。对于ORM,我正在使用Hibernate。这是我的配置:
[hibernate.cfg.xml]
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:~/myDB</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">qwerty</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="de.alf.h2.entity.Student"/>
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
[data.xml]
<bean:beans xmlns:bean="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<ctx:component-scan base-package="de.alf.h2.repository"/>
<bean:bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<bean:property name="sessionFactory" ref="sessionFactory"/>
</bean:bean>
<bean:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<bean:property name="configLocation" value="classpath:/hibernate/hibernate.cfg.xml"/>
</bean:bean>
<bean:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<bean:property name="sessionFactory" ref="sessionFactory"/>
</bean:bean>
</bean:beans>
Run Code Online (Sandbox Code Playgroud)
[web.xml]
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/data.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Run Code Online (Sandbox Code Playgroud)
我需要从文件(resource / db / data.sql)创建数据库。
CREATE TABLE student {
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR (250)
}
Run Code Online (Sandbox Code Playgroud)
如何在我的代码中执行此操作?
如果hbm2ddl.auto设置为create,Hibernate则将在SessionFactory创建过程中从映射的类生成DDL 。您还可以添加一个名为import.sql您的类路径的文件以导入一些数据。
如果您正在使用M2进行测试,并且想要自己进行测试,则还可以使用“ H2 在连接上执行SQL”功能:
jdbc:h2:~/myDB;INIT=runscript from '~/resource/db/data.sql'
Run Code Online (Sandbox Code Playgroud)
**请注意,通向的路径data.sql可能有所不同。
| 归档时间: |
|
| 查看次数: |
8640 次 |
| 最近记录: |