给STRUTS2+SPRING项目集成HIBERNATE,DBUNIT

         首先给ECLIPSE安装M2ECLIPSE插件,插件的UPDATESITE地址是http://m2eclipse.sonatype.org/sites/m2e,安装完毕后如果插件报Missing indirectly referenced artifact com.sun:tools:jar:1.5.0: 的错 请在ECLIPSE启动时候加上 -vm "C:\Program Files\Java\jdk1.6.0_01\bin"   后面的参数值是JDK的bin目录,如下图所示:

给STRUTS2+SPRING项目集成HIBERNATE,DBUNIT_第1张图片 

              插件安装完毕后,双击pom.xml文件,我们将会看到如下界面 ,

 

    插件解析了POM.XML文件使其可视化了,我们先点击dependencies页,然后就可以点击ADD按钮增加需要的依赖JAR包了,如下图:

 

添加完毕后,pom.xml的依赖项描述如下:

  
  
  
  
  1. <dependencies> 
  2.     <!-- Junit --> 
  3.     <dependency> 
  4.         <groupId>junit</groupId> 
  5.         <artifactId>junit</artifactId> 
  6.         <version>4.0</version> 
  7.         <scope>test</scope> 
  8.     </dependency> 
  9.  
  10.     <!--  Struts 2 --> 
  11.     <dependency> 
  12.            <groupId>org.apache.struts</groupId> 
  13.            <artifactId>struts2-core</artifactId> 
  14.            <version>2.0.11.2</version> 
  15.        </dependency> 
  16.        <dependency> 
  17.            <groupId>org.apache.struts</groupId> 
  18.            <artifactId>struts2-sitemesh-plugin</artifactId> 
  19.            <version>2.0.11.2</version> 
  20.        </dependency> 
  21.        <dependency> 
  22.            <groupId>org.apache.struts</groupId> 
  23.            <artifactId>struts2-spring-plugin</artifactId> 
  24.            <version>2.0.11.2</version> 
  25.        </dependency> 
  26.  
  27.     <!-- Servlet & Jsp --> 
  28.     <dependency> 
  29.         <groupId>javax.servlet</groupId> 
  30.         <artifactId>servlet-api</artifactId> 
  31.         <version>2.4</version> 
  32.         <scope>provided</scope> 
  33.     </dependency> 
  34.     <dependency> 
  35.         <groupId>javax.servlet</groupId> 
  36.         <artifactId>jsp-api</artifactId> 
  37.         <version>2.0</version> 
  38.         <scope>provided</scope> 
  39.     </dependency> 
  40.  
  41.     <!-- Jakarta Commons --> 
  42.     <dependency> 
  43.         <groupId>commons-fileupload</groupId> 
  44.         <artifactId>commons-fileupload</artifactId> 
  45.         <version>1.1.1</version> 
  46.     </dependency> 
  47.     <dependency> 
  48.         <groupId>commons-beanutils</groupId> 
  49.         <artifactId>commons-beanutils</artifactId> 
  50.         <version>1.7.0</version> 
  51.         <type>jar</type> 
  52.         <scope>compile</scope> 
  53.     </dependency> 
  54.     <!-- JPA  -->   
  55.     <dependency> 
  56.         <groupId>javax.persistence</groupId> 
  57.         <artifactId>persistence-api</artifactId> 
  58.         <version>1.0</version> 
  59.         <type>jar</type> 
  60.         <scope>compile</scope> 
  61.     </dependency> 
  62.     <!-- hibernate  -->   
  63.     <dependency> 
  64.         <groupId>org.hibernate</groupId> 
  65.         <artifactId>hibernate-annotations</artifactId> 
  66.         <version>3.3.0.ga</version> 
  67.         <type>jar</type> 
  68.         <scope>compile</scope> 
  69.     </dependency> 
  70.     <dependency> 
  71.         <groupId>org.hibernate</groupId> 
  72.         <artifactId>hibernate-commons-annotations</artifactId> 
  73.         <version>3.3.0.ga</version> 
  74.         <type>jar</type> 
  75.         <scope>compile</scope> 
  76.     </dependency> 
  77.     <dependency> 
  78.         <groupId>org.hibernate</groupId> 
  79.         <artifactId>hibernate</artifactId> 
  80.         <version>3.2.5.ga</version> 
  81.         <type>jar</type> 
  82.         <scope>compile</scope> 
  83.     </dependency> 
  84.     <!-- mysql connector  --> 
  85.     <dependency> 
  86.         <groupId>${jdbc.groupId}</groupId>    
  87.            <artifactId>${jdbc.artifactId}</artifactId>    
  88.            <version>${jdbc.version}</version>    
  89.         <type>jar</type> 
  90.         <scope>compile</scope> 
  91.     </dependency> 
  92.     <!-- Dwr --> 
  93.     <dependency> 
  94.         <groupId>uk.ltd.getahead</groupId> 
  95.         <artifactId>dwr</artifactId> 
  96.         <version>1.1.1</version> 
  97.         <type>jar</type> 
  98.         <scope>compile</scope> 
  99.     </dependency> 
  100.     <!-- data source --> 
  101.     <dependency> 
  102.         <groupId>c3p0</groupId> 
  103.         <artifactId>c3p0</artifactId> 
  104.         <version>0.9.1</version> 
  105.         <type>jar</type> 
  106.         <scope>compile</scope> 
  107.     </dependency> 
  108.     <!-- spring  -->   
  109.      <dependency> 
  110.         <groupId>org.springframework</groupId> 
  111.         <artifactId>spring-hibernate3</artifactId> 
  112.         <version>2.0.8</version> 
  113.         <type>jar</type> 
  114.         <scope>compile</scope>   
  115.     </dependency> 
  116.     <dependency> 
  117.         <groupId>org.springframework</groupId> 
  118.         <artifactId>spring-test</artifactId> 
  119.         <version>2.5.4</version> 
  120.         <type>jar</type> 
  121.         <scope>compile</scope> 
  122.     </dependency> 
  123.     <dependency> 
  124.         <groupId>org.springframework</groupId> 
  125.         <artifactId>spring-beans</artifactId> 
  126.         <version>2.5.4</version> 
  127.         <type>jar</type> 
  128.         <scope>compile</scope> 
  129.     </dependency> 
  130.     <dependency> 
  131.         <groupId>org.springframework</groupId> 
  132.         <artifactId>spring-core</artifactId> 
  133.         <version>2.5.6</version> 
  134.         <type>jar</type> 
  135.         <scope>compile</scope> 
  136.     </dependency> 
  137.     <dependency> 
  138.         <groupId>org.springframework</groupId> 
  139.         <artifactId>spring</artifactId> 
  140.         <version>2.5.6</version> 
  141.         <type>jar</type> 
  142.         <scope>compile</scope> 
  143.     </dependency> 
  144. </dependencies> 

 从中可以发现mysql connector 有点奇怪,没有版本号和JAR包名,这是因为这些信息作为公共信息提取出来了,在pom.xml尾端我们加入了一下属性描述,如下:

 

  
  
  
  
  1. <properties> 
  2.        <dbunit.dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dbunit.dataTypeFactoryName> 
  3.        <dbunit.operation.type>CLEAN_INSERT</dbunit.operation.type> 
  4.        <hibernate.dialect>org.hibernate.dialect.MySQL5InnoDBDialect</hibernate.dialect> 
  5.        <jdbc.groupId>mysql</jdbc.groupId> 
  6.     <jdbc.artifactId>mysql-connector-java</jdbc.artifactId> 
  7.        <jdbc.version>5.1.13</jdbc.version> 
  8.        <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName> 
  9.        <jdbc.url><![CDATA[jdbc:mysql://localhost/test?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf-8]]></jdbc.url> 
  10.        <jdbc.username>root</jdbc.username> 
  11.        <jdbc.password>root</jdbc.password> 
  12.   </properties> 

注:dbunit.dataTypeFactoryName  是dbunit数据类型生成器的类型配置,可以根据不同的数据库进行设置,有如下配置可供选择

  • org.dbunit.ext.db2.Db2DataTypeFactory
  • org.dbunit.ext.h2.H2DataTypeFactory
  • org.dbunit.ext.db2.HsqldbDataTypeFactory
  • org.dbunit.ext.mssql.MsSqlDataTypeFactory
  • org.dbunit.ext.mysql.MySqlDataTypeFactory
  • org.dbunit.ext.oracle.OracleDataTypeFactory
  • org.dbunit.ext.oracle.Oracle10DataTypeFactory
  • org.dbunit.ext.postgresql.PostgresqlDataTypeFactory
  • org.dbunit.ext.netezza.NetezzaDataTypeFactory

 OK 添加完依赖项以后我们添加 dbunit插件和hibernate3-maven插件,我们点击plugins页,然后点击ADD按钮就可以添加了,方法雷同添加依赖项,添加完毕以后我们需要对插件进行配置,这只能直接改pom.xml文件进行配置,我们来看下最终的配置xml片段,

  
  
  
  
  1. <plugin> 
  2.     <groupId>org.codehaus.mojo</groupId> 
  3.     <artifactId>dbunit-maven-plugin</artifactId> 
  4.     <version>1.0-beta-3</version> 
  5.      <executions>    
  6.          <execution>    
  7.            <id>test-compile</id>    
  8.            <phase>test-compile</phase>    
  9.            <goals>    
  10.              <goal>operation</goal>    
  11.            </goals>    
  12.          </execution>    
  13.          <execution>    
  14.            <id>test</id>    
  15.            <phase>test</phase>    
  16.            <goals>    
  17.              <goal>operation</goal>    
  18.            </goals>    
  19.          </execution>    
  20.        </executions>    
  21.        <dependencies>    
  22.          <dependency>    
  23.            <groupId>${jdbc.groupId}</groupId>    
  24.            <artifactId>${jdbc.artifactId}</artifactId>    
  25.            <version>${jdbc.version}</version>    
  26.          </dependency>    
  27.        </dependencies>    
  28.        <configuration>    
  29.          <dataTypeFactoryName>${dbunit.dataTypeFactoryName}</dataTypeFactoryName>    
  30.          <driver>${jdbc.driverClassName}</driver>    
  31.          <username>${jdbc.username}</username>    
  32.          <password>${jdbc.password}</password>    
  33.          <url>${jdbc.url}</url>    
  34.          <src>src/test/resources/sample-data.xml</src>    
  35.          <type>${dbunit.operation.type}</type>    
  36.          <skip>${maven.test.skip}</skip><!--很重要,可以控制是否要在package时插入测试数据-->    
  37.        </configuration>    
  38. </plugin> 
  39. <plugin> 
  40.     <groupId>org.codehaus.mojo</groupId> 
  41.     <artifactId>hibernate3-maven-plugin</artifactId> 
  42.     <version>2.2</version> 
  43.     <dependencies> 
  44.         <dependency> 
  45.          <groupId>${jdbc.groupId}</groupId>    
  46.            <artifactId>${jdbc.artifactId}</artifactId>    
  47.            <version>${jdbc.version}</version>    
  48.         </dependency> 
  49.     </dependencies> 
  50.     <configuration> 
  51.         <components> 
  52.             <component> 
  53.                 <name>hbm2ddl</name> 
  54.                 <implementation>annotationconfiguration</implementation> 
  55.             </component> 
  56.         </components> 
  57.         <componentProperties> 
  58.             <drop>true</drop> 
  59.             <jdk5>true</jdk5> 
  60.             <propertyfile>target/classes/jdbc.properties</propertyfile> 
  61.         </componentProperties> 
  62.         <skip>${maven.test.skip}</skip><!--很重要,可以控制是否要在package时重新建表--> 
  63.     </configuration> 
  64.     <executions> 
  65.         <execution> 
  66.             <phase>process-test-resources</phase> 
  67.             <goals> 
  68.                 <goal>hbm2ddl</goal> 
  69.             </goals> 
  70.         </execution> 
  71.     </executions> 
  72. </plugin> 

 以上配置的具体含义可以查看这些插件的官方文档。

        好了以上配置完成以后,我们接着创建配置文件和实体类,列表如下:

1、创建实体类/src/main/java/com/ceair/app/model   User.java  使用注解来标识字段,之后可以通过hibernate3-maven-plug插件生成对应的表,代码如下:

  
  
  
  
  1. package com.ceair.app.model;  
  2.  
  3. import javax.persistence.Column;    
  4. import javax.persistence.Entity;    
  5. import javax.persistence.GeneratedValue;    
  6. import javax.persistence.Id;    
  7.     
  8. /**   
  9.  * @author Administrator   
  10.  *   
  11.  */    
  12. @Entity    
  13. public class User {    
  14.     private Long id;    
  15.     private String name;    
  16.     /**   
  17.      * @return the id   
  18.      */    
  19.     @Id    
  20.     @GeneratedValue    
  21.     public Long getId() {    
  22.         return id;    
  23.     }    
  24.     /**   
  25.      * @param id the id to set   
  26.      */    
  27.     public void setId(Long id) {    
  28.         this.id = id;    
  29.     }    
  30.     /**   
  31.      * @return the name   
  32.      */    
  33.     @Column(name="name")    
  34.     public String getName() {    
  35.         return name;    
  36.     }    
  37.     /**   
  38.      * @param name the name to set   
  39.      */    
  40.     public void setName(String name) {    
  41.         this.name = name;    
  42.     }    
  43.         
  44. }   

2、创建hibernate配置文件 src/main/resources/hibernate.cfg.xml 对实体类进行映射,配置内容如下:

  
  
  
  
  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="com.ceair.app.model.User"/>    
  7.     </session-factory>    
  8. </hibernate-configuration>   

3、创建 src/main/resources/jdbc.properties  此文件中的信息applicationContext.xml和hibernate3-maven-plugin将会用到,内容如下:

  
  
  
  
  1. hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect  
  2. hibernate.connection.username=root 
  3. hibernate.connection.password=root 
  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 
注意:值一定要去掉前后空格,切记!!

4、创建JPA描述文件src/main/resources/META-INF/persistence.xml ,其内容如下:

 

  
  
  
  
  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>   

5、创建DBUNIT种子数据文件 src/test/resources/sample-data.xml dbunit-maven-plug插件需要使用此文件,其内容如下:

  
  
  
  
  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">管理员</value>    
  13.         </row>    
  14.     </table>    
  15. </dataset>   

6、修改src/main/resources/applicationContext.xml 添加hibernate相关的配置,添加内容如下:

  
  
  
  
  1.   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
        </bean>
  2. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close"> 
  3.        <property name="driverClassName" value="${hibernate.connection.driver_class}"/> 
  4.        <property name="url" value="${hibernate.connection.url}"/> 
  5.        <property name="username" value="${hibernate.connection.username}"/> 
  6.        <property name="password" value="${hibernate.connection.password}"/> 
  7.       <property name="maxActive" value="100"/> 
  8.        <property name="maxWait" value="1000"/> 
  9.        <property name="poolPreparedStatements" value="true"/> 
  10.        <property name="defaultAutoCommit" value="true"/>   
  11.    </bean> 
  12. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">    
  13.        <property name="dataSource" ref="dataSource"/>    
  14.        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>    
  15.          
  16.        <property name="hibernateProperties">    
  17.            <props>    
  18.                <prop key="hibernate.dialect">${hibernate.dialect}</prop>    
  19.                <prop key="hibernate.cache.use_second_level_cache">false</prop>    
  20.                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>    
  21.                <prop key="show_sql">true</prop>    
  22.            </props>    
  23.        </property>    
  24.    </bean>   

注意: datasource使用了org.springframework.jdbc.datasource.DriverManagerDataSource 不具备连接池功能,生产环境可以使用com.mchange.v2.c3p0.ComboPooledDataSource,sessionFactory中的show_sql项在生产中应该是false

最后安装MYSQL5,需要注意的是如果之前安装过MYSQL但是忘了root用户密码的话,请删除C:\Documents and Settings\All Users\Application Data 下的MYSQL文件夹后再安装,还有记得选择默认语言编码为UTF-8。

以上工作全部完成以后,打开命令行,进入项目目录后输入mvn jetty:run后启动项目,项目启动后会自动在MYSQL中创建user表 并插入sample-data.xml中的数据,如下图所示:

 

 

 

你可能感兴趣的:(spring,Hibernate,项目,dbunit,休闲)