maven根据hibernate生成是实体类

1、在pom文件加入如下配置:

<build>
		<plugins>
		
		    <!-- Hibernate生成实体配置 -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>hibernate3-maven-plugin</artifactId>
				<version>2.2</version>

				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.21</version>
					</dependency>
					<dependency>
						<groupId>cglib</groupId>
						<artifactId>cglib</artifactId>
						<version>2.2</version>
					</dependency>
				</dependencies>

				<configuration>
					<components>
						<component>
							<name>hbm2java</name>
							<outputDirectory>src/main/java/com/anysoft/cms/</outputDirectory>
							<!-- 主要用于反向控制数据库引擎通过JDBC连接数据 -->
							<implementation>jdbcconfiguration</implementation>
						</component>
					</components>
					<componentProperties>
					    <!-- Hibernate配置文件 -->
						<configurationfile>src/main/resources/hibernate.cfg.xml </configurationfile>
						<!-- 
						如果设为true将会生成JPA的元素注解
						使用annotations from 
						javax.persistence and org.hibernate.annotations
						默认值是false  
						-->
						<ejb3>true</ejb3>
						<!-- 指明生成java类的包名 -->
						<packagename>entity</packagename>
					</componentProperties>
				</configuration>
			</plugin>
			<!-- Hibernate生成实体配置 -->
			
		</plugins>
	</build>

2、配置Hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->

<hibernate-configuration>
	<session-factory>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">pass#word1</property>
		<property name="hibernate.connection.url">jdbc:mysql://192.168.130.4:3306/any_cms?characterEncoding=utf-8</property>
		<property name="hibernate.connection.shutdown">true</property>
	</session-factory>
</hibernate-configuration>

 

 

关于使用maven hibernate Plugin 生成java实体类使用 maven hibernate Plugin 主要有6种功能  hibernate3:hbm2cfgxml : Generates hibernate.cfg.xml

hibernate3:hbm2ddl: Generates database schema. 

hibernate3: hbm2doc : Generates HTML documentation for database schema

hibernate3:hbm2hbmxml: Generates a set of hbm.xml files

hibernate3:hbm2java: Generates Java classes from set of *.hbm.xml files

hibernate3:hbmtemplate: Renders arbitrary templates against Hibernate Mapping information

 

这里主要介绍hibernate3:hbm2java这个功能如何使用它生成java实体类

使用这个插件的前置条件是首先配置好hibernate.cfg.xml这个文件配置结果如下所示:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->

<hibernate-configuration>
	<session-factory>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">pass#word1</property>
		<property name="hibernate.connection.url">jdbc:mysql://192.168.130.4:3306/any_cms?characterEncoding=utf-8</property>
		<property name="hibernate.connection.shutdown">true</property>
	</session-factory>
</hibernate-configuration>

 然后配置pom文件中的信息如下所示:

<build>  
 <plugins> 
   <plugin>   
  <groupId>org.codehaus.mojo</groupId>  
   <artifactId>hibernate3-maven-plugin</artifactId>   
  <version>2.2</version>   
  <dependencies>   
   <dependency>    
   <groupId>mysql</groupId>   
    <artifactId>mysql-connector-java</artifactId>  
     <version>3.1.11</version>   
   </dependency>  
    <dependency>    
   <groupId>cglib</groupId>    
   <artifactId>cglib</artifactId> 
      <version>2.2</version>  
    </dependency>    
 </dependencies>   
  <configuration>     
 <components>     
  <component>    
    <name>hbm2java</name>   
     <outputDirectory>src/main/java/</outputDirectory>   
     <implementation>jdbcconfiguration</implementation>     
      </component>   
   </components> 
     <componentProperties> 
 <configurationfile>src/main/resources/hibernate.cfg.xml </configurationfile>   
    <ejb3>true</ejb3>    
   <packagename>model</packagename>   
   </componentProperties>   
  </configuration>   
 </plugin>  
 </plugins>
 </build>

 对POM配置文件一些重要配置进行简单的说明 >hibernate3-maven-plugin

这个主要依赖mysql驱动如果是mysql数据库类型,取决于连接的数据库和cglib包主要用于反向控制

 

<component><name/><component>是maven插件的goal的名称在maven hibernate插件中主要有hbm2cfgxml、hbm2ddl、hbm2doc、hbm2hbmxml、hbm2java、hbmtemplate等6种。 <outputDirectory/>主要指明要输出的路径 <implementation/>需要实现的hibernate配置主要有configuration、annotationconfiguration、jpaconfiguration、jdbcconfiguration等4种实现方式 Jdbcconfiguration主要用于反向控制数据库引擎通过JDBC连接数据库其他3种详情请参考hibernate tools参考说明书 <componentProperties><configurationfile/></componentProperties>指明hibernate的配置文件路径 <ejb3>true</ejb3>如果设为true将会生成JPA的元素注解使用annotations from javax.persistence and org.hibernate.annotations默认值是false <packagename>指明生成java类的包名<component><name/><component>是maven插件的goal的名称在maven hibernate插件中主要有hbm2cfgxml、hbm2ddl、hbm2doc、hbm2hbmxml、hbm2java、hbmtemplate等6种。 <outputDirectory/>主要指明要输出的路径 <implementation/>需要实现的hibernate配置主要有configuration、annotationconfiguration、jpaconfiguration、jdbcconfiguration等4种实现方式 Jdbcconfiguration主要用于反向控制数据库引擎通过JDBC连接数据库其他3种详情请参考hibernate tools参考说明书 <componentProperties><configurationfile/></componentProperties>指明hibernate的配置文件路径 <ejb3>true</ejb3>如果设为true将会生成JPA的元素注解使用annotations from javax.persistence and org.hibernate.annotations默认值是false <packagename>指明生成java类的包名

 

上述配置文件配置好后就可以运行maven插件命令了在命令行下面可以直接使用 hibernate3:hbm2java或者在elipse下面选择工程右键选择run as –》run configuration 在goals输入hibernate3:hbm2java

 

上面的操作是对连接的数据库进行整库的表进行java类生成如果生成部分或者个别的表用上述操作将会重新把所有生成的java类覆盖所有要针对部分或者个别的表生成实体类请在POM文件中增加反转控制配置文件A reveng.xml 这个文件被使用来定制如何通过工具来反向控制数据库引擎如下所示:

<componentProperties>  
<revengfile>src/main/resources/hibernate.reveng.xml</revengfile>
 </componentProperties>

 在componentProperties中增加revengfile标签并指明文件的路径 关于hibernate.reveng.xml的代码示例如下:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-reverse-engineering       
  SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" > 
<hibernate-reverse-engineering>     
<!--<schema-selection match-schema="TMDCS"/>-->     
<!-- <table-filter match-schema="cityhome"  match-name=".*"/> -->     
<schema-selection match-schema="cityhome" match-table="ACCOUNT"/>
 </hibernate-reverse-engineering>

 <schema-selection match-schema="cityhome" match-table="ACCOUNT"/>

match-schema=" COMMON_SCHEMA"  数据库拥有着 match-table="ACCOUNT"  表名 支持"CODES_开头的表名match-table="CODES_.*" 上述配置文件起到的作用就是在schema cityhome 中 只生成 ACCOUNT这个表的java实体类其他的都被过滤了 Reveng.xml有很多种反向控制这里只介绍了最简单的一种如果不满足可以参考hibernate tools 查询其他更详细的配置

 

参考地址:http://wenku.baidu.com/view/121954c0aa00b52acfc7cadb.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值