使用maven命令行进行反向hibernate reverse hbm code 代码生成

使用maven命令行进行反向hibernate reverse hbm code 代码生成


一外国友人写的直接用mvn生成反向代码的帖子https://community.jboss.org/message/801327#801327才成功,现在介绍一下

1. 先创建一个空白maven项目,这个项目你爱叫啥名都可以,反正只是拿来生成代码用的,用完就可以删掉了



2. 往pom.xml 里面添加plugin

<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
					<optimise>true</optimise>
					<compilerArgument>-nowarn</compilerArgument>
				</configuration>
			</plugin>
			     <plugin>
				   <groupId>org.codehaus.mojo</groupId>
				   <artifactId>hibernate3-maven-plugin</artifactId>
				   <version>2.2</version>
					 <configuration>
					   <components>
						 <component>
						   <name>hbm2hbmxml</name>
						   <implementation>jdbcconfiguration</implementation>
						   <outputDirectory>target/generated-resources/hibernate3</outputDirectory>
						 </component>
						 <component>
						   <name>hbm2java</name>
						   <implementation>jdbcconfiguration</implementation>
						   <outputDirectory>target/generated-sources/hibernate3</outputDirectory>
						 </component>
					   </components>
					   <componentProperties>
						 <revengfile>src/main/resources/reveng.xml</revengfile>
						 <propertyfile>src/main/resources/hibernate.properties</propertyfile>
						<packagename>com.whatever.domain</packagename>
						 <jdk5>true</jdk5>
						 <ejb3>false</ejb3>
					   </componentProperties>
					 </configuration>
					 <dependencies>
					   <dependency>
						 <groupId>cglib</groupId>
						 <artifactId>cglib-nodep</artifactId>
						 <version>2.2.2</version>
					   </dependency>
					   <dependency>
						  <groupId>mysql</groupId>
						  <artifactId>mysql-connector-java</artifactId>
						  <version>5.1.25</version>
						</dependency>
					 </dependencies>
				   </plugin>
		</plugins>
	</build>

注意那个 com.whatever.domain 换成你自己的域名

3. 往 src/main/resources 下添加 hibernate.properties

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://xxxxxx:xxxxx/xxxxx
hibernate.connection.username=xxxxx
hibernate.connection.password=xxxxx

4. 往 src/main/resources 下添加 reveng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC
  "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
  <schema-selection match-schema="MATCHDB"/>
</hibernate-reverse-engineering>

这个 MATCHDB 就是你要反向的库名

5. 如果你要生成注解方式的 java文件,就运行 mvn hibernate3:hbm2java

如果你要生成 hbm.xml 配合 java 的形式就先把 pom 里面 <ejb3>false</ejb3> 弄成false 然后分别运行 mvn hibernate3:hbm2hbmxml 和 mvn hibernate3:hbm2java
注意:当我们在eclipse中执行maven命令时,不需要加mvn;换句话说,我们在控制台执行maven项目编译操作的命令是mvn compile,而在m2eclipse中执行编译操作是,鼠标右击要编译的项目或要编译项目的POM.xml文件 - > Run as - > maven build.. ,弹出一个编译框,Base directory是要编译项目的跟目录 ,Goals是输入mvn命令的,

猜你喜欢

转载自open023.iteye.com/blog/2324915