天道酬勤 tiān dào chóu qín

天将降大任于斯人也,必将苦其心志,劳其筋骨 宠辱不惊,看庭上花开花落;去留无意,望天上云卷云舒
随笔 - 1, 文章 - 20, 评论 - 1, 引用 - 0
数据加载中……

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

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

hibernate3-maven-plugin,有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 

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

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中添加下面的代码 

Xml代码 
  1. <build>  
  2.   
  3.   <plugins>  
  4.   
  5.    
  6.   
  7.      <plugin>  
  8.         <groupId>org.codehaus.mojo</groupId>  
  9.         <artifactId>hibernate3-maven-plugin</artifactId>  
  10.         <version>2.0-alpha-1</version>  
  11.         <dependencies>  
  12.            <dependency>  
  13.              <groupId>mysql</groupId>  
  14.              <artifactId>mysql-connector-java</artifactId>  
  15.              <version>5.0.4</version>  
  16.           </dependency>  
  17.         </dependencies>  
  18.         <configuration>  
  19.           <components>  
  20.             <component>  
  21.               <name>hbm2ddl</name>  
  22.               <implementation>  
  23.                  annotationconfiguration  
  24.               </implementation>  
  25.             </component>  
  26.           </components>  
  27.           <componentProperties>  
  28.             <drop>true</drop>  
  29.             <jdk5>true</jdk5>  
  30.             <propertyfile>  
  31.                /src/main/resources/jdbc.properties  
  32.             </propertyfile>  
  33.             <configurationfile>  
  34.               /src/main/resources/hibernate.cfg.xml  
  35.             </configurationfile>  
  36.           </componentProperties>  
  37.         </configuration>  
  38.         <executions>  
  39.           <execution>  
  40.             <phase>process-test-resources</phase>  
  41.             <goals>  
  42.                <goal>hbm2ddl</goal>  
  43.             </goals>  
  44.          </execution>  
  45.        </executions>  
  46.    </plugin>  
  47.    
  48.     <plugin>  
  49.        <groupId>org.codehaus.mojo</groupId>  
  50.        <artifactId>dbunit-maven-plugin</artifactId>  
  51.        <dependencies>  
  52.           <dependency>  
  53.             <groupId>mysql</groupId>  
  54.             <artifactId>mysql-connector-java</artifactId>  
  55.             <version>5.0.4</version>  
  56.           </dependency>  
  57.         </dependencies>  
  58.         <configuration>  
  59.           <dataTypeFactoryName>  
  60.               org.dbunit.dataset.datatype.DefaultDataTypeFactory  
  61.           </dataTypeFactoryName>  
  62.           <driver>com.mysql.jdbc.Driver</driver>  
  63.           <url>  
  64.              jdbc:mysql://localhost/projectStencil?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&sessionVariables=FOREIGN_KEY_CHECKS=0  
  65.          </url>  
  66.          <username>root</username>  
  67.          <password></password>  
  68.          <src>src/test/resources/sample-data.xml</src>  
  69.          <type>CLEAN_INSERT</type>  
  70.        </configuration>  
  71.        <executions>  
  72.           <execution>  
  73.              <id>test-compile</id>  
  74.              <phase>test-compile</phase>  
  75.              <goals>  
  76.                <goal>operation</goal>  
  77.              </goals>  
  78.           </execution>  
  79.           <execution>  
  80.              <id>test</id>  
  81.              <phase>test</phase>  
  82.              <goals>  
  83.                 <goal>operation</goal>  
  84.             </goals>  
  85.           </execution>  
  86.        </executions>  
  87.    </plugin>  
  88.     
  89.    
  90.   
  91. </plugins>  
  92.   
  93. </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 举例如下 


Xml代码 
  1. <hibernate-configuration>  
  2.  <session-factory>  
  3.        <mapping class="com...dao.model.User" />  
  4.   
  5.         ...  
  6.   
  7.   </session-factory>  
  8. </hibernate-configuration>  



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

可以使用 mvn dbunit:export -Ddest=sample-data.xml 将数据库的数据导出来 

posted on 2009-07-09 18:23 chong 阅读(3601) 评论(0)  编辑  收藏 所属分类: maven


只有注册用户登录后才能发表评论。


网站导航: