首先给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目录,如下图所示:
插件安装完毕后,双击pom.xml文件,我们将会看到如下界面 ,
插件解析了POM.XML文件使其可视化了,我们先点击dependencies页,然后就可以点击ADD按钮增加需要的依赖JAR包了,如下图:
添加完毕后,pom.xml的依赖项描述如下:
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.0version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.apache.strutsgroupId>
- <artifactId>struts2-coreartifactId>
- <version>2.0.11.2version>
- dependency>
- <dependency>
- <groupId>org.apache.strutsgroupId>
- <artifactId>struts2-sitemesh-pluginartifactId>
- <version>2.0.11.2version>
- dependency>
- <dependency>
- <groupId>org.apache.strutsgroupId>
- <artifactId>struts2-spring-pluginartifactId>
- <version>2.0.11.2version>
- dependency>
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>servlet-apiartifactId>
- <version>2.4version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>jsp-apiartifactId>
- <version>2.0version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>commons-fileuploadgroupId>
- <artifactId>commons-fileuploadartifactId>
- <version>1.1.1version>
- dependency>
- <dependency>
- <groupId>commons-beanutilsgroupId>
- <artifactId>commons-beanutilsartifactId>
- <version>1.7.0version>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>javax.persistencegroupId>
- <artifactId>persistence-apiartifactId>
- <version>1.0version>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>org.hibernategroupId>
- <artifactId>hibernate-annotationsartifactId>
- <version>3.3.0.gaversion>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>org.hibernategroupId>
- <artifactId>hibernate-commons-annotationsartifactId>
- <version>3.3.0.gaversion>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>org.hibernategroupId>
- <artifactId>hibernateartifactId>
- <version>3.2.5.gaversion>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>${jdbc.groupId}groupId>
- <artifactId>${jdbc.artifactId}artifactId>
- <version>${jdbc.version}version>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>uk.ltd.getaheadgroupId>
- <artifactId>dwrartifactId>
- <version>1.1.1version>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>c3p0groupId>
- <artifactId>c3p0artifactId>
- <version>0.9.1version>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-hibernate3artifactId>
- <version>2.0.8version>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>2.5.4version>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-beansartifactId>
- <version>2.5.4version>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-coreartifactId>
- <version>2.5.6version>
- <type>jartype>
- <scope>compilescope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>springartifactId>
- <version>2.5.6version>
- <type>jartype>
- <scope>compilescope>
- dependency>
- dependencies>
从中可以发现mysql connector 有点奇怪,没有版本号和JAR包名,这是因为这些信息作为公共信息提取出来了,在pom.xml尾端我们加入了一下属性描述,如下:
- <properties>
- <dbunit.dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactorydbunit.dataTypeFactoryName>
- <dbunit.operation.type>CLEAN_INSERTdbunit.operation.type>
- <hibernate.dialect>org.hibernate.dialect.MySQL5InnoDBDialecthibernate.dialect>
- <jdbc.groupId>mysqljdbc.groupId>
- <jdbc.artifactId>mysql-connector-javajdbc.artifactId>
- <jdbc.version>5.1.13jdbc.version>
- <jdbc.driverClassName>com.mysql.jdbc.Driverjdbc.driverClassName>
- <jdbc.url>jdbc.url>
- <jdbc.username>rootjdbc.username>
- <jdbc.password>rootjdbc.password>
- 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片段,
- <plugin>
- <groupId>org.codehaus.mojogroupId>
- <artifactId>dbunit-maven-pluginartifactId>
- <version>1.0-beta-3version>
- <executions>
- <execution>
- <id>test-compileid>
- <phase>test-compilephase>
- <goals>
- <goal>operationgoal>
- goals>
- execution>
- <execution>
- <id>testid>
- <phase>testphase>
- <goals>
- <goal>operationgoal>
- goals>
- execution>
- executions>
- <dependencies>
- <dependency>
- <groupId>${jdbc.groupId}groupId>
- <artifactId>${jdbc.artifactId}artifactId>
- <version>${jdbc.version}version>
- dependency>
- dependencies>
- <configuration>
- <dataTypeFactoryName>${dbunit.dataTypeFactoryName}dataTypeFactoryName>
- <driver>${jdbc.driverClassName}driver>
- <username>${jdbc.username}username>
- <password>${jdbc.password}password>
- <url>${jdbc.url}url>
- <src>src/test/resources/sample-data.xmlsrc>
- <type>${dbunit.operation.type}type>
- <skip>${maven.test.skip}skip>
- configuration>
- plugin>
- <plugin>
- <groupId>org.codehaus.mojogroupId>
- <artifactId>hibernate3-maven-pluginartifactId>
- <version>2.2version>
- <dependencies>
- <dependency>
- <groupId>${jdbc.groupId}groupId>
- <artifactId>${jdbc.artifactId}artifactId>
- <version>${jdbc.version}version>
- dependency>
- dependencies>
- <configuration>
- <components>
- <component>
- <name>hbm2ddlname>
- <implementation>annotationconfigurationimplementation>
- component>
- components>
- <componentProperties>
- <drop>truedrop>
- <jdk5>truejdk5>
- <propertyfile>target/classes/jdbc.propertiespropertyfile>
- componentProperties>
- <skip>${maven.test.skip}skip>
- configuration>
- <executions>
- <execution>
- <phase>process-test-resourcesphase>
- <goals>
- <goal>hbm2ddlgoal>
- goals>
- execution>
- executions>
- plugin>
以上配置的具体含义可以查看这些插件的官方文档。
好了以上配置完成以后,我们接着创建配置文件和实体类,列表如下:
1、创建实体类/src/main/java/com/ceair/app/model User.java 使用注解来标识字段,之后可以通过hibernate3-maven-plug插件生成对应的表,代码如下:
- package com.ceair.app.model;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- /**
- * @author Administrator
- *
- */
- @Entity
- public class User {
- private Long id;
- private String name;
- /**
- * @return the id
- */
- @Id
- @GeneratedValue
- public Long getId() {
- return id;
- }
- /**
- * @param id the id to set
- */
- public void setId(Long id) {
- this.id = id;
- }
- /**
- * @return the name
- */
- @Column(name="name")
- public String getName() {
- return name;
- }
- /**
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- }
2、创建hibernate配置文件 src/main/resources/hibernate.cfg.xml 对实体类进行映射,配置内容如下:
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <mapping class="com.ceair.app.model.User"/>
- session-factory>
- hibernate-configuration>
3、创建 src/main/resources/jdbc.properties 此文件中的信息applicationContext.xml和hibernate3-maven-plugin将会用到,内容如下:
注意:值一定要去掉前后空格,切记!!
- hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
- hibernate.connection.username=root
- hibernate.connection.password=root
- hibernate.connection.url=jdbc:mysql://localhost/test?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8
- hibernate.connection.driver_class=com.mysql.jdbc.Driver
4、创建JPA描述文件src/main/resources/META-INF/persistence.xml ,其内容如下:
- <persistence xmlns="http://java.sun.com/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
- version="1.0">
- <persistence-unit name="ApplicationEntityManager" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistenceprovider>