使用maven自动创建数据库,导入导出数据

使用maven自动创建数据库,导入导出数据需要用到两个插件hibernate3-maven-plugin,dbunit-maven-plugin

[b]hibernate3-maven-plugin[/b],有7大功能,(这里我们只使用hbm2ddl,自动生成数据库)
The Maven Hibernate3 Plugin has seven goals.

hibernate3:hbm2cfgxml: Generates hibernate.cfg.xml
hibernate3:hbm2ddl: Generates database schema.
hibernate3:hbm2doc: Generates HTML documentation for the database schema.
hibernate3:hbm2hbmxml: Generates a set of hbm.xml files
hibernate3:hbm2java: Generates Java classes from set of *.hbm.xml files
hibernate3:schema-export: Creates SQL DDL file and generates the database schema from set of *.hbm.xml files
hibernate3:schema-update: Updates the database schema based on the set of *.hbm.xml files

[b]dbunit-maven-plugin[/b],可以实现数据库中数据的导入导出

dbunit:operation: Execute a database operation using an external dataset file.
dbunit:export: Export database tables into a dataset file.
dbunit:compare: Compare a dataset with database.


具体配置,(MYSQL为例)

在pom.xml中添加下面的代码

<build>

<plugins>



<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.0-alpha-1</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency>
</dependencies>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
<implementation>
annotationconfiguration
</implementation>
</component>
</components>
<componentProperties>
<drop>true</drop>
<jdk5>true</jdk5>
<propertyfile>
/src/main/resources/jdbc.properties
</propertyfile>
<configurationfile>
/src/main/resources/hibernate.cfg.xml
</configurationfile>
</componentProperties>
</configuration>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>hbm2ddl</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dbunit-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency>
</dependencies>
<configuration>
<dataTypeFactoryName>
org.dbunit.dataset.datatype.DefaultDataTypeFactory
</dataTypeFactoryName>
<driver>com.mysql.jdbc.Driver</driver>
<url>
jdbc:mysql://localhost/projectStencil?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&sessionVariables=FOREIGN_KEY_CHECKS=0
</url>
<username>root</username>
<password></password>
<src>src/test/resources/sample-data.xml</src>
<type>CLEAN_INSERT</type>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>operation</goal>
</goals>
</execution>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>operation</goal>
</goals>
</execution>
</executions>
</plugin>



</plugins>

</build>



其中jdbc.properties 内容为

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.username=root
hibernate.connection.password=
hibernate.connection.url=jdbc:mysql://localhost/projectStencil?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8
hibernate.connection.driver_class=com.mysql.jdbc.Driver



hibernate.cfg.xml 举例如下


<hibernate-configuration>
<session-factory>
<mapping class="com...dao.model.User" />

...

</session-factory>
</hibernate-configuration>



sample-data.xml 为要导入数据库的数据

可以使用 mvn dbunit:export -Ddest=sample-data.xml 将数据库的数据导出来
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值