教你使用Hibernate tools,生成带中文注释的类和hbm文件
来源:互联网 发布:sql注入攻击方式 编辑:程序博客网 时间:2024/05/16 03:40
实现效果如下:
- package com.fsti.ssh.entity.hibernate;
- // Generated 2013-6-21 18:10:59 by Hibernate Tools 3.4.0.CR1
- import java.math.BigDecimal;
- import java.util.Date;
- import java.util.LinkedHashMap;
- import java.util.Map;
- /**
- * TSsh generated by hbm2java
- */
- public class TSsh implements java.io.Serializable {
- /**
- * 主键-seq_ssh.
- */
- private String id;
- /**
- * 名称.
- */
- private String name;
- /**
- * 创建时间.
- */
- private Date createDate;
- /**
- * 更新时间.
- */
- private Date updateDate;
- /**
- * 类型;1:普通;2:特殊.
- */
- private String type;
- /**
- * 总数.
- */
- private BigDecimal count;
- public TSsh() {
- }
- public TSsh(String id) {
- this.id = id;
- }
- public TSsh(String id, String name, Date createDate, Date updateDate,
- String type, BigDecimal count) {
- this.id = id;
- this.name = name;
- this.createDate = createDate;
- this.updateDate = updateDate;
- this.type = type;
- this.count = count;
- }
- public static Map<String, String> relation = new LinkedHashMap<String, String>();
- static {
- relation.put("id", "主键-seq_ssh");
- relation.put("name", "名称");
- relation.put("createDate", "创建时间");
- relation.put("updateDate", "更新时间");
- relation.put("type", "类型;1:普通;2:特殊");
- relation.put("count", "总数");
- }
- public String getId() {
- return this.id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getCreateDate() {
- return this.createDate;
- }
- public void setCreateDate(Date createDate) {
- this.createDate = createDate;
- }
- public Date getUpdateDate() {
- return this.updateDate;
- }
- public void setUpdateDate(Date updateDate) {
- this.updateDate = updateDate;
- }
- public String getType() {
- return this.type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public BigDecimal getCount() {
- return this.count;
- }
- public void setCount(BigDecimal count) {
- this.count = count;
- }
- /**
- * toString
- *
- * @return String
- */
- @Override
- public String toString() {
- StringBuffer buffer = new StringBuffer();
- buffer.append(getClass().getName()).append("@")
- .append(Integer.toHexString(hashCode())).append(" [");
- buffer.append("]");
- return buffer.toString();
- }
- }
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!-- Generated 2013-6-21 18:10:59 by Hibernate Tools 3.4.0.CR1 -->
- <hibernate-mapping>
- <class name="com.fsti.ssh.entity.hibernate.TSsh" table="T_SSH">
- <comment>演示表</comment>
- <id name="id" type="string">
- <column name="ID" length="40" />
- <generator class="assigned" />
- </id>
- <property name="name" type="string">
- <column name="NAME" length="500">
- <comment>名称</comment>
- </column>
- </property>
- <property name="createDate" type="date">
- <column name="CREATE_DATE" length="7">
- <comment>创建时间</comment>
- </column>
- </property>
- <property name="updateDate" type="date">
- <column name="UPDATE_DATE" length="7">
- <comment>更新时间</comment>
- </column>
- </property>
- <property name="type" type="string">
- <column name="TYPE" length="1">
- <comment>类型;1:普通;2:特殊</comment>
- </column>
- </property>
- <property name="count" type="big_decimal">
- <column name="COUNT" precision="22" scale="0">
- <comment>总数</comment>
- </column>
- </property>
- </class>
- </hibernate-mapping>
【1】用压缩软件打开 hibernate-tools-3.4.0.CR2.jar
【2】在pojo下,将PojoFileds.ftl 文件拖出压缩包,并进行如下修改:
- <#-- // Fields -->
- <#foreach field in pojo.getAllPropertiesIterator()>
- <#if pojo.getMetaAttribAsBool(field, "gen-property", true)>
- <#if pojo.hasMetaAttribute(field, "field-description")>
- /**
- ${pojo.getFieldJavaDoc(field, 0)}
- */
- </#if>
- <#foreach column in field.columnIterator><#if column.comment?exists && column.comment?trim?length!=0>
- /**
- * ${column.comment}.
- */</#if></#foreach>
- ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${field.name}
- <#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
- </#if>
- </#foreach>
上面的代码,将实现在POJO类中,输出中文注释信息。
修改完成后,拖至压缩包中,即可实现重新打包操作。
【3】使用反编译工具,打开 org.hibernate.tool.hbm2x.TemplateProducer 类class文件,修改如下:
- // FileWriter fileWriter = null;
- Writer fileWriter = null;
- // fileWriter = new FileWriter(destination);
- fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destination), "UTF-8"));
重新编译后,重新打包。
【4】打开org\hibernate\tool\hbm2x\jtidy.properties
在文件末尾添加如下代码:
- input-encoding=utf-8
- 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数据库。
- 教你使用Hibernate tools,生成带中文注释的类和hbm文件
- 教你使用Hibernate tools,生成带中文注释的类和hbm文件
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- 使用Hibernate-tools中的hbm2java和hbm2ddl根据hbm文件自动生成pojo和数据库脚本
- 使用Hibernate Tools从数据库表生成pojo和hbm
- hibernate 的 使用 MyEclipse 工具生成 POJO 类和映射文件*.hbm.xml
- 使用myeclipse的反向工程生成hibernate映射文件(.hbm.xml)和pojo类
- 在MyEclipse的web项目/java项目中,使用Hibernate-tools中的hbm2java和hbm2ddl工具,根据hbm文件自动生成pojo和数据库脚本
- Hibernate反向工程生成hbm.xml文件和带注解模型类
- 使用 Hibernate Tools 自动生成hbm.xml及model java文件
- Eclipse 用Hibernate Tools 生成 .hbm cfg.xml等文件
- Eclipse 用Hibernate Tools 生成 .hbm cfg.xml等文件 .
- Eclipse 用Hibernate Tools 生成 .hbm cfg.xml等文件
- eclipse下使用xdoclet生成hibernate的hbm映射文件
- Hibernate 自动生成 hbm文件
- 使用MiddleGen-hibernate-r5生成hbm文件及POJO文件
- 使用MiddleGen-hibernate-r5生成hbm文件及POJO文件
- finally_clause_and_return_clause_in_java
- 一些C/C++开源项目网址
- 面试题:输入一颗二元查找树,将该树转换为它的镜像, 即在转换后的二元查找树中,左子树的结点都大于右子树的结点。 用递归和循环两种方法完成树的镜像转换。
- Android中Activity启动模式小结
- 通俗易懂多线程(JAVA)
- 教你使用Hibernate tools,生成带中文注释的类和hbm文件
- strrchr函数
- ios7 状态栏
- cv::Mat与iplimage类型如何进行互相转换
- C++ AMP: 基于代码的 C++ AMP 简介
- C#对ini文件的读写操作
- 批处理读取文件
- Servlet及Jsp
- java.lang.NoClassDefFoundError:org/bouncycastle/jce/provider/BouncyCastleProvider