这一步要实现的目标是在执行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加入以下代码:
三、配置数据库连接
除了pom.xml文件。还需一个jdbc.properties文件。内容如下:
四、配置实体,由于我们采用hibernate框架
hibernate.cfg.xml:
五、配置持久化方案
persistence.xml:
这个文件基本就这样。
六、添加实体对象
User.java:
这个实体对象采用annotaion来做持久化映射
七、添加测试数据
sample-data.xml:
以上配置基本实现了。在这个maven工程启动时,会自动创建数据库,并将sample-data.xml中的测试数据插入到数据库中。那么如果数据库通过其它来源已插入了一些不错的数据,怎么将它导入到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
一、加入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加入以下代码:
- <build>
- <finalName>sshExt</finalName>
- <!-- hibernate3-maven-plugin负责生成数据库 -->
- <plugins>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>hibernate3-maven-plugin</artifactId>
- <version>2.1</version>
- <dependencies>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.0.5</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>
- <!-- dbunit-maven-plugin负责数据库的导入导出 -->
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>dbunit-maven-plugin</artifactId>
- <version>1.0-beta-1</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>
- </plugins>
- </build>
三、配置数据库连接
除了pom.xml文件。还需一个jdbc.properties文件。内容如下:
- hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
- hibernate.connection.username=root
- hibernate.connection.password=
- hibernate.connection.url=jdbc:mysql://localhost/test?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8
- hibernate.connection.driver_class=com.mysql.jdbc.Driver
四、配置实体,由于我们采用hibernate框架
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="net.apex.framework.model.User"/>
- <!-- <mapping class="net.apex.framework.model.Role"/> -->
- </session-factory>
- </hibernate-configuration>
五、配置持久化方案
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>
这个文件基本就这样。
六、添加实体对象
User.java:
- /**
- *
- */
- package net.apex.framework.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;
- }
- }
这个实体对象采用annotaion来做持久化映射
七、添加测试数据
sample-data.xml:
- <?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">admin</value>
- </row>
- </table>
- </dataset>
以上配置基本实现了。在这个maven工程启动时,会自动创建数据库,并将sample-data.xml中的测试数据插入到数据库中。那么如果数据库通过其它来源已插入了一些不错的数据,怎么将它导入到sample-data.xml中呢。有办法:
- 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