首先给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>
- <!-- Junit -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.0</version>
- <scope>test</scope>
- </dependency>
- <!-- Struts 2 -->
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-core</artifactId>
- <version>2.0.11.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-sitemesh-plugin</artifactId>
- <version>2.0.11.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-spring-plugin</artifactId>
- <version>2.0.11.2</version>
- </dependency>
- <!-- Servlet & Jsp -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.4</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.0</version>
- <scope>provided</scope>
- </dependency>
- <!-- Jakarta Commons -->
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.1.1</version>
- </dependency>
- <dependency>
- <groupId>commons-beanutils</groupId>
- <artifactId>commons-beanutils</artifactId>
- <version>1.7.0</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <!-- JPA -->
- <dependency>
- <groupId>javax.persistence</groupId>
- <artifactId>persistence-api</artifactId>
- <version>1.0</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <!-- hibernate -->
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-annotations</artifactId>
- <version>3.3.0.ga</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-commons-annotations</artifactId>
- <version>3.3.0.ga</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate</artifactId>
- <version>3.2.5.ga</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <!-- mysql connector -->
- <dependency>
- <groupId>${jdbc.groupId}</groupId>
- <artifactId>${jdbc.artifactId}</artifactId>
- <version>${jdbc.version}</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <!-- Dwr -->
- <dependency>
- <groupId>uk.ltd.getahead</groupId>
- <artifactId>dwr</artifactId>
- <version>1.1.1</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <!-- data source -->
- <dependency>
- <groupId>c3p0</groupId>
- <artifactId>c3p0</artifactId>
- <version>0.9.1</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <!-- spring -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-hibernate3</artifactId>
- <version>2.0.8</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>2.5.4</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>2.5.4</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>2.5.6</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- <version>2.5.6</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- </dependencies>
从中可以发现mysql connector 有点奇怪,没有版本号和JAR包名,这是因为这些信息作为公共信息提取出来了,在pom.xml尾端我们加入了一下属性描述,如下:
- <properties>
- <dbunit.dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dbunit.dataTypeFactoryName>
- <dbunit.operation.type>CLEAN_INSERT</dbunit.operation.type>
- <hibernate.dialect>org.hibernate.dialect.MySQL5InnoDBDialect</hibernate.dialect>
- <jdbc.groupId>mysql</jdbc.groupId>
- <jdbc.artifactId>mysql-connector-java</jdbc.artifactId>
- <jdbc.version>5.1.13</jdbc.version>
- <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
- <jdbc.url><![CDATA[jdbc:mysql://localhost/test?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8]]></jdbc.url>
- <jdbc.username>root</jdbc.username>
- <jdbc.password>root</jdbc.password>
- </properties>
注:dbunit.dataTypeFactoryName 是dbunit数据类型生成器的类型配置,可以根据不同的数据库进行设置,有如下配置可供选择
OK 添加完依赖项以后我们添加 dbunit插件和hibernate3-maven插件,我们点击plugins页,然后点击ADD按钮就可以添加了,方法雷同添加依赖项,添加完毕以后我们需要对插件进行配置,这只能直接改pom.xml文件进行配置,我们来看下最终的配置xml片段,
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>dbunit-maven-plugin</artifactId>
- <version>1.0-beta-3</version>
- <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>
- <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.xml</src>
- <type>${dbunit.operation.type}</type>
- <skip>${maven.test.skip}</skip><!--很重要,可以控制是否要在package时插入测试数据-->
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>hibernate3-maven-plugin</artifactId>
- <version>2.2</version>
- <dependencies>
- <dependency>
- <groupId>${jdbc.groupId}</groupId>
- <artifactId>${jdbc.artifactId}</artifactId>
- <version>${jdbc.version}</version>
- </dependency>
- </dependencies>
- <configuration>
- <components>
- <component>
- <name>hbm2ddl</name>
- <implementation>annotationconfiguration</implementation>
- </component>
- </components>
- <componentProperties>
- <drop>true</drop>
- <jdk5>true</jdk5>
- <propertyfile>target/classes/jdbc.properties</propertyfile>
- </componentProperties>
- <skip>${maven.test.skip}</skip><!--很重要,可以控制是否要在package时重新建表-->
- </configuration>
- <executions>
- <execution>
- <phase>process-test-resources</phase>
- <goals>
- <goal>hbm2ddl</goal>
- </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 对实体类进行映射,配置内容如下:
- <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "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.HibernatePersistence</provider>
- <!--
- <properties>
- <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
- </properties>
- -->
- </persistence-unit>
- </persistence>
5、创建DBUNIT种子数据文件 src/test/resources/sample-data.xml dbunit-maven-plug插件需要使用此文件,其内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <dataset>
- <table name="user">
- <column>id</column>
- <column>name</column>
- <row>
- <value description="id">-1</value>
- <value description="name">user</value>
- </row>
- <row>
- <value description="id">-2</value>
- <value description="name">管理员</value>
- </row>
- </table>
- </dataset>
6、修改src/main/resources/applicationContext.xml 添加hibernate相关的配置,添加内容如下:
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
- <property name="driverClassName" value="${hibernate.connection.driver_class}"/>
- <property name="url" value="${hibernate.connection.url}"/>
- <property name="username" value="${hibernate.connection.username}"/>
- <property name="password" value="${hibernate.connection.password}"/>
- <property name="maxActive" value="100"/>
- <property name="maxWait" value="1000"/>
- <property name="poolPreparedStatements" value="true"/>
- <property name="defaultAutoCommit" value="true"/>
- </bean>
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
- <prop key="hibernate.cache.use_second_level_cache">false</prop>
- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
- <prop key="show_sql">true</prop>
- </props>
- </property>
- </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中的数据,如下图所示: