教你使用Hibernate tools,生成带中文注释的类和hbm文件

来源:互联网 发布:sql注入攻击方式 编辑:程序博客网 时间:2024/05/16 03:40

实现效果如下:


[java] view plaincopy
  1. package com.fsti.ssh.entity.hibernate;  
  2.   
  3. // Generated 2013-6-21 18:10:59 by Hibernate Tools 3.4.0.CR1  
  4.   
  5. import java.math.BigDecimal;  
  6. import java.util.Date;  
  7. import java.util.LinkedHashMap;  
  8. import java.util.Map;  
  9.   
  10. /** 
  11.  * TSsh generated by hbm2java 
  12.  */  
  13. public class TSsh implements java.io.Serializable {  
  14.   
  15.     /** 
  16.      * 主键-seq_ssh. 
  17.      */  
  18.     private String id;  
  19.   
  20.     /** 
  21.      * 名称. 
  22.      */  
  23.     private String name;  
  24.   
  25.     /** 
  26.      * 创建时间. 
  27.      */  
  28.     private Date createDate;  
  29.   
  30.     /** 
  31.      * 更新时间. 
  32.      */  
  33.     private Date updateDate;  
  34.   
  35.     /** 
  36.      * 类型;1:普通;2:特殊. 
  37.      */  
  38.     private String type;  
  39.   
  40.     /** 
  41.      * 总数. 
  42.      */  
  43.     private BigDecimal count;  
  44.   
  45.     public TSsh() {  
  46.     }  
  47.   
  48.     public TSsh(String id) {  
  49.         this.id = id;  
  50.     }  
  51.   
  52.     public TSsh(String id, String name, Date createDate, Date updateDate,  
  53.             String type, BigDecimal count) {  
  54.         this.id = id;  
  55.         this.name = name;  
  56.         this.createDate = createDate;  
  57.         this.updateDate = updateDate;  
  58.         this.type = type;  
  59.         this.count = count;  
  60.     }  
  61.   
  62.     public static Map<String, String> relation = new LinkedHashMap<String, String>();  
  63.     static {  
  64.         relation.put("id""主键-seq_ssh");  
  65.         relation.put("name""名称");  
  66.         relation.put("createDate""创建时间");  
  67.         relation.put("updateDate""更新时间");  
  68.         relation.put("type""类型;1:普通;2:特殊");  
  69.         relation.put("count""总数");  
  70.     }  
  71.   
  72.     public String getId() {  
  73.         return this.id;  
  74.     }  
  75.   
  76.     public void setId(String id) {  
  77.         this.id = id;  
  78.     }  
  79.   
  80.     public String getName() {  
  81.         return this.name;  
  82.     }  
  83.   
  84.     public void setName(String name) {  
  85.         this.name = name;  
  86.     }  
  87.   
  88.     public Date getCreateDate() {  
  89.         return this.createDate;  
  90.     }  
  91.   
  92.     public void setCreateDate(Date createDate) {  
  93.         this.createDate = createDate;  
  94.     }  
  95.   
  96.     public Date getUpdateDate() {  
  97.         return this.updateDate;  
  98.     }  
  99.   
  100.     public void setUpdateDate(Date updateDate) {  
  101.         this.updateDate = updateDate;  
  102.     }  
  103.   
  104.     public String getType() {  
  105.         return this.type;  
  106.     }  
  107.   
  108.     public void setType(String type) {  
  109.         this.type = type;  
  110.     }  
  111.   
  112.     public BigDecimal getCount() {  
  113.         return this.count;  
  114.     }  
  115.   
  116.     public void setCount(BigDecimal count) {  
  117.         this.count = count;  
  118.     }  
  119.   
  120.     /** 
  121.      * toString 
  122.      *  
  123.      * @return String 
  124.      */  
  125.     @Override  
  126.     public String toString() {  
  127.         StringBuffer buffer = new StringBuffer();  
  128.   
  129.         buffer.append(getClass().getName()).append("@")  
  130.                 .append(Integer.toHexString(hashCode())).append(" [");  
  131.         buffer.append("]");  
  132.   
  133.         return buffer.toString();  
  134.     }  
  135.   
  136. }  

[html] view plaincopy
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!-- Generated 2013-6-21 18:10:59 by Hibernate Tools 3.4.0.CR1 -->  
  5. <hibernate-mapping>  
  6.     <class name="com.fsti.ssh.entity.hibernate.TSsh" table="T_SSH">  
  7.         <comment>演示表</comment>  
  8.         <id name="id" type="string">  
  9.             <column name="ID" length="40" />  
  10.             <generator class="assigned" />  
  11.         </id>  
  12.         <property name="name" type="string">  
  13.             <column name="NAME" length="500">  
  14.                 <comment>名称</comment>  
  15.             </column>  
  16.         </property>  
  17.         <property name="createDate" type="date">  
  18.             <column name="CREATE_DATE" length="7">  
  19.                 <comment>创建时间</comment>  
  20.             </column>  
  21.         </property>  
  22.         <property name="updateDate" type="date">  
  23.             <column name="UPDATE_DATE" length="7">  
  24.                 <comment>更新时间</comment>  
  25.             </column>  
  26.         </property>  
  27.         <property name="type" type="string">  
  28.             <column name="TYPE" length="1">  
  29.                 <comment>类型;1:普通;2:特殊</comment>  
  30.             </column>  
  31.         </property>  
  32.         <property name="count" type="big_decimal">  
  33.             <column name="COUNT" precision="22" scale="0">  
  34.                 <comment>总数</comment>  
  35.             </column>  
  36.         </property>  
  37.     </class>  
  38. </hibernate-mapping>  



【1】用压缩软件打开 hibernate-tools-3.4.0.CR2.jar


【2】在pojo下,将PojoFileds.ftl 文件拖出压缩包,并进行如下修改:

[plain] view plaincopy
  1. <#-- // Fields -->  
  2.   
  3. <#foreach field in pojo.getAllPropertiesIterator()>  
  4. <#if pojo.getMetaAttribAsBool(field, "gen-property", true)>  
  5.  <#if pojo.hasMetaAttribute(field, "field-description")>     
  6.   /**  
  7.      ${pojo.getFieldJavaDoc(field, 0)}  
  8.      */  
  9.  </#if>     
  10.    
  11.  <#foreach column in field.columnIterator><#if column.comment?exists && column.comment?trim?length!=0>     
  12.  /**   
  13.   * ${column.comment}.  
  14.   */</#if></#foreach>  
  15.   ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${field.name}  
  16.   <#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;  
  17. </#if>  
  18. </#foreach>  

上面的代码,将实现在POJO类中,输出中文注释信息。

修改完成后,拖至压缩包中,即可实现重新打包操作。


【3】使用反编译工具,打开 org.hibernate.tool.hbm2x.TemplateProducer 类class文件,修改如下:

[java] view plaincopy
  1. // FileWriter fileWriter = null;  
  2. Writer fileWriter = null;  

[java] view plaincopy
  1. // fileWriter = new FileWriter(destination);  
  2. fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destination), "UTF-8"));  

重新编译后,重新打包。


【4】打开org\hibernate\tool\hbm2x\jtidy.properties

在文件末尾添加如下代码:


[plain] view plaincopy
  1. input-encoding=utf-8  
  2. output-encoding=utf-8  

【5】重新打包后,将新的Jar包,替换掉原有插件的Jar包。

比如笔者的插件包所在路径是:D:\eclipse\plugins\org.hibernate.eclipse.libs_3.6.0.Final-v20130327-1513-B111\lib\tools\hibernate-tools-3.4.0.CR2.jar;支持中文的hibernate-tools.jar下载


【6】重启eclipse,重新生成文件,即可实现带中文注释的配置文件和类代码。


注意:笔者的数据库环境是Oracle数据库。