用maven整合struts+spring+hibernate之二 数据库生成和测试数据

这一步要实现的目标是在执行mvn package时,maven为我们自动创建数据表,并将测试数据添加进去。是不是很实用?你要觉得不所谓,也要以跳过这步。 

一、加入ssh支持 
就是增加struts、spring、hibernate等的依赖包啦。。不细说了。后面详细说明。 

二、配置插件 
这一块是最复杂的,先来了解两个插件: 

1、hibernate3-maven-plugin插件可实现自动生成数据库schema 
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 

2、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. 

了解上面两个插件后,就可以用到它们了。怎么用,就是在pom.xml加入以下代码: 
Java代码   收藏代码
  1. <build>  
  2.     <finalName>sshExt</finalName>  
  3.     <!-- hibernate3-maven-plugin负责生成数据库 -->  
  4.     <plugins>  
  5.         <plugin>  
  6.         <groupId>org.codehaus.mojo</groupId>  
  7.         <artifactId>hibernate3-maven-plugin</artifactId>  
  8.         <version>2.1</version>  
  9.         <dependencies>  
  10.            <dependency>  
  11.              <groupId>mysql</groupId>  
  12.              <artifactId>mysql-connector-java</artifactId>  
  13.              <version>5.0.5</version>  
  14.           </dependency>  
  15.         </dependencies>  
  16.         <configuration>  
  17.           <components>  
  18.             <component>  
  19.               <name>hbm2ddl</name>  
  20.               <implementation>annotationconfiguration</implementation>  
  21.             </component>  
  22.           </components>  
  23.           <componentProperties>  
  24.             <drop>true</drop>  
  25.             <jdk5>true</jdk5>  
  26.             <propertyfile>target/classes/jdbc.properties</propertyfile>  
  27.                      </componentProperties>  
  28.             <skip>${maven.test.skip}</skip><!--很重要,可以控制是否要在package时重新建表-->  
  29.         </configuration>  
  30.         <executions>  
  31.           <execution>  
  32.             <phase>process-test-resources</phase>  
  33.             <goals>  
  34.                <goal>hbm2ddl</goal>  
  35.             </goals>  
  36.          </execution>  
  37.        </executions>  
  38.       </plugin>  
  39.         
  40.       <!-- dbunit-maven-plugin负责数据库的导入导出 -->  
  41.       <plugin>  
  42.         <groupId>org.codehaus.mojo</groupId>  
  43.         <artifactId>dbunit-maven-plugin</artifactId>  
  44.         <version>1.0-beta-1</version>  
  45.         <executions>  
  46.           <execution>  
  47.             <id>test-compile</id>  
  48.             <phase>test-compile</phase>  
  49.             <goals>  
  50.               <goal>operation</goal>  
  51.             </goals>  
  52.           </execution>  
  53.           <execution>  
  54.             <id>test</id>  
  55.             <phase>test</phase>  
  56.             <goals>  
  57.               <goal>operation</goal>  
  58.             </goals>  
  59.           </execution>  
  60.         </executions>  
  61.         <dependencies>  
  62.           <dependency>  
  63.             <groupId>${jdbc.groupId}</groupId>  
  64.             <artifactId>${jdbc.artifactId}</artifactId>  
  65.             <version>${jdbc.version}</version>  
  66.           </dependency>  
  67.         </dependencies>  
  68.         <configuration>  
  69.           <dataTypeFactoryName>${dbunit.dataTypeFactoryName}</dataTypeFactoryName>  
  70.           <driver>${jdbc.driverClassName}</driver>  
  71.           <username>${jdbc.username}</username>  
  72.           <password>${jdbc.password}</password>  
  73.           <url>${jdbc.url}</url>  
  74.           <src>src/test/resources/sample-data.xml</src>  
  75.           <type>${dbunit.operation.type}</type>  
  76.           <skip>${maven.test.skip}</skip><!--很重要,可以控制是否要在package时插入测试数据-->  
  77.         </configuration>  
  78.       </plugin>  
  79.     </plugins>  
  80.   </build>  


三、配置数据库连接 
除了pom.xml文件。还需一个jdbc.properties文件。内容如下: 
Java代码   收藏代码
  1. hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect  
  2. hibernate.connection.username=root  
  3. hibernate.connection.password=  
  4. hibernate.connection.url=jdbc:mysql://localhost/test?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf-8  
  5. hibernate.connection.driver_class=com.mysql.jdbc.Driver  


四、配置实体,由于我们采用hibernate框架 
hibernate.cfg.xml: 
Java代码   收藏代码
  1. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  2.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  3.   
  4. <hibernate-configuration>  
  5.     <session-factory>  
  6.         <mapping class="net.apex.framework.model.User"/>  
  7.         <!-- <mapping class="net.apex.framework.model.Role"/> -->  
  8.     </session-factory>  
  9. </hibernate-configuration>  


五、配置持久化方案 
persistence.xml: 
Java代码   收藏代码
  1. <persistence xmlns="http://java.sun.com/xml/ns/persistence"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"  
  4.     version="1.0">  
  5.     <persistence-unit name="ApplicationEntityManager" transaction-type="RESOURCE_LOCAL">  
  6.         <provider>org.hibernate.ejb.HibernatePersistence</provider>  
  7.         <!--   
  8.         <properties>  
  9.             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>  
  10.         </properties>  
  11.          -->  
  12.     </persistence-unit>  
  13. </persistence>  

这个文件基本就这样。 

六、添加实体对象 
User.java: 
Java代码   收藏代码
  1. /** 
  2.  *  
  3.  */  
  4. package net.apex.framework.model;  
  5.   
  6. import javax.persistence.Column;  
  7. import javax.persistence.Entity;  
  8. import javax.persistence.GeneratedValue;  
  9. import javax.persistence.Id;  
  10.   
  11. /** 
  12.  * @author Administrator 
  13.  * 
  14.  */  
  15. @Entity  
  16. public class User {  
  17.     private Long id;  
  18.     private String name;  
  19.     /** 
  20.      * @return the id 
  21.      */  
  22.     @Id  
  23.     @GeneratedValue  
  24.     public Long getId() {  
  25.         return id;  
  26.     }  
  27.     /** 
  28.      * @param id the id to set 
  29.      */  
  30.     public void setId(Long id) {  
  31.         this.id = id;  
  32.     }  
  33.     /** 
  34.      * @return the name 
  35.      */  
  36.     @Column(name="name")  
  37.     public String getName() {  
  38.         return name;  
  39.     }  
  40.     /** 
  41.      * @param name the name to set 
  42.      */  
  43.     public void setName(String name) {  
  44.         this.name = name;  
  45.     }  
  46.       
  47. }  


这个实体对象采用annotaion来做持久化映射 

七、添加测试数据 
sample-data.xml: 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <dataset>  
  3.     <table name="user">  
  4.         <column>id</column>  
  5.         <column>name</column>  
  6.         <row>  
  7.             <value description="id">-1</value>  
  8.             <value description="name">user</value>  
  9.         </row>  
  10.         <row>  
  11.             <value description="id">-2</value>  
  12.             <value description="name">admin</value>  
  13.         </row>  
  14.     </table>  
  15.   
  16. </dataset>  


以上配置基本实现了。在这个maven工程启动时,会自动创建数据库,并将sample-data.xml中的测试数据插入到数据库中。那么如果数据库通过其它来源已插入了一些不错的数据,怎么将它导入到sample-data.xml中呢。有办法: 
Java代码   收藏代码
  1. mvn dbunit:export -Ddest=sample-data.xml  


上面的几个文件的目录是这样的: 
src/main/resources/hibernate.cfg.xml 
src/main/resources/jdbc.properties 
src/main/resources/META-INF/persistence.xml 
src/test/resources/sample-data.xml 
pom.xml
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值