过修改ibator1.2.2(http://svn.apache.org/repos/asf/ibatis/java/ibator)
1) 修改org.apache.ibatis.ibator.api.Ibator类,
方法private void writeFile(File file, String content) throws IOException
修改编码如下:
private void writeFile(File file, String content) throws IOException {
java.io.OutputStreamWriter fos = new java.io.OutputStreamWriter(new java.io.FileOutputStream(file), "UTF-8");
fos.write(content);
fos.flush();
fos.close();
}
2)修改org.apache.ibatis.ibator.internal.DefaultCommentGenerator类中的addFieldComment,addClassComment,addGetterComment,addSetterComment,addGeneralMethodComment,addComment方法,修改成你想要的格式。
修改如下方法:
public void addClassComment(InnerClass innerClass,
IntrospectedTable introspectedTable) {
addClassComment(innerClass, introspectedTable.getFullyQualifiedTable(), false);
}
public void addEnumComment(InnerEnum innerEnum,
IntrospectedTable introspectedTable) {
addEnumComment(innerEnum, introspectedTable.getFullyQualifiedTable());
}
public void addFieldComment(Field field,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
addFieldComment(field, introspectedTable.getFullyQualifiedTable(),
introspectedColumn.getRemarks());
}
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
addFieldComment(field, introspectedTable.getFullyQualifiedTable());
}
public void addGeneralMethodComment(Method method,
IntrospectedTable introspectedTable) {
addGeneralMethodComment(method, introspectedTable.getFullyQualifiedTable());
}
public void addGetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
addGetterComment(method, introspectedTable.getFullyQualifiedTable(),
introspectedColumn.getRemarks());
}
public void addSetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
addSetterComment(method, introspectedTable.getFullyQualifiedTable(),
introspectedColumn.getRemarks());
}
public void addClassComment(InnerClass innerClass,
IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
addClassComment(innerClass, introspectedTable.getFullyQualifiedTable(), markAsDoNotDelete);
}
主要是把
introspectedColumn.getActualColumnName()
改成(利用数据库中的comment生成注释)
introspectedColumn.getRemarks()
3)修改好,把生成的jar文件:"ibator-core-1.2.2-SNAPSHOT.jar",改名为"ibator.jar",在安装了ibator插件的Eclipse中,覆盖eclipse\plugins\org.apache.ibatis.ibator.core_1.2.1下的jar文件
4)测试:
4.1) 数据库脚本:
create table
CREATE TABLE `brand` (
`code` varchar(36) NOT NULL default '' COMMENT '代码',
`logopath` varchar(80) default NULL COMMENT 'logo路径',
`name` varchar(40) NOT NULL default '' COMMENT '名字',
`visible` bit(1) NOT NULL COMMENT '是否可见',
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
create table
CREATE TABLE `employee` (
`username` varchar(20) NOT NULL default '' COMMENT '用户名',
`degree` varchar(10) default NULL COMMENT '等级',
`email` varchar(50) default NULL COMMENT '电子邮箱',
`gender` varchar(5) NOT NULL default '' COMMENT '性别',
`imageName` varchar(41) default NULL COMMENT '图片名字',
`password` varchar(20) NOT NULL default '' COMMENT '密码',
`phone` varchar(20) default NULL COMMENT '电话',
`realname` varchar(10) NOT NULL default '' COMMENT '真实姓名',
`school` varchar(20) default NULL COMMENT '学校',
`visible` bit(1) NOT NULL COMMENT '是否可见',
`department_id` int(11) default NULL COMMENT '部门ID',
`card_id` int(11) NOT NULL COMMENT '身份证',
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
4.2)ibatorConfig.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ibatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN" "http://ibatis.apache.org/dtd/ibator-config_1_0.dtd" > <ibatorConfiguration> <classPathEntry location="D:/Eclipse/workspace/helios-3.6/test/lib/mysql_connector_java_5.jar" /> <ibatorContext id="context1" targetRuntime="Ibatis2Java5"> <commentGenerator> <property name="suppressDate" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/test" userId="root" password="root"> </jdbcConnection> <javaModelGenerator targetPackage="cn.zlj.ibatis.entity" targetProject="test" /> <sqlMapGenerator targetPackage="cn.zlj.ibatis.sqlmap" targetProject="test" /> <daoGenerator targetPackage="cn.zlj.ibatis.dao" targetProject="test" implementationPackage="cn.zlj.ibatis.dao.impl" type="spring" /> <table tableName="brand"> </table> <table tableName="employee"> </table> </ibatorContext> </ibatorConfiguration>
4.3)ibator生成文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" > <sqlMap namespace="brand" > <resultMap id="BaseResultMap" class="cn.zlj.ibatis.entity.Brand" > <result column="code" property="code" jdbcType="VARCHAR" /> <result column="logopath" property="logopath" jdbcType="VARCHAR" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="visible" property="visible" jdbcType="BIT" /> </resultMap> <sql id="Example_Where_Clause" > <iterate property="oredCriteria" conjunction="or" prepend="where" removeFirstPrepend="iterate" > <isEqual property="oredCriteria[].valid" compareValue="true" > ( <iterate prepend="and" property="oredCriteria[].criteriaWithoutValue" conjunction="and" > $oredCriteria[].criteriaWithoutValue[]$ </iterate> <iterate prepend="and" property="oredCriteria[].criteriaWithSingleValue" conjunction="and" > $oredCriteria[].criteriaWithSingleValue[].condition$ #oredCriteria[].criteriaWithSingleValue[].value# </iterate> <iterate prepend="and" property="oredCriteria[].criteriaWithListValue" conjunction="and" > $oredCriteria[].criteriaWithListValue[].condition$ <iterate property="oredCriteria[].criteriaWithListValue[].values" open="(" close=")" conjunction="," > #oredCriteria[].criteriaWithListValue[].values[]# </iterate> </iterate> <iterate prepend="and" property="oredCriteria[].criteriaWithBetweenValue" conjunction="and" > $oredCriteria[].criteriaWithBetweenValue[].condition$ #oredCriteria[].criteriaWithBetweenValue[].values[0]# and #oredCriteria[].criteriaWithBetweenValue[].values[1]# </iterate> ) </isEqual> </iterate> </sql> <sql id="Base_Column_List" > code, logopath, name, visible </sql> <select id="selectByExample" resultMap="BaseResultMap" parameterClass="cn.zlj.ibatis.entity.BrandExample" > select <isParameterPresent > <isEqual property="distinct" compareValue="true" > distinct </isEqual> </isParameterPresent> <include refid="brand.Base_Column_List" /> from brand <isParameterPresent > <include refid="brand.Example_Where_Clause" /> <isNotNull property="orderByClause" > order by $orderByClause$ </isNotNull> </isParameterPresent> </select> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterClass="cn.zlj.ibatis.entity.Brand" > select <include refid="brand.Base_Column_List" /> from brand where code = #code:VARCHAR# </select> <delete id="deleteByPrimaryKey" parameterClass="cn.zlj.ibatis.entity.Brand" > delete from brand where code = #code:VARCHAR# </delete> <delete id="deleteByExample" parameterClass="cn.zlj.ibatis.entity.BrandExample" > delete from brand <include refid="brand.Example_Where_Clause" /> </delete> <insert id="insert" parameterClass="cn.zlj.ibatis.entity.Brand" > insert into brand (code, logopath, name, visible) values (#code:VARCHAR#, #logopath:VARCHAR#, #name:VARCHAR#, #visible:BIT#) </insert> <insert id="insertSelective" parameterClass="cn.zlj.ibatis.entity.Brand" > insert into brand <dynamic prepend="(" > <isNotNull prepend="," property="code" > code </isNotNull> <isNotNull prepend="," property="logopath" > logopath </isNotNull> <isNotNull prepend="," property="name" > name </isNotNull> <isNotNull prepend="," property="visible" > visible </isNotNull> ) </dynamic> values <dynamic prepend="(" > <isNotNull prepend="," property="code" > #code:VARCHAR# </isNotNull> <isNotNull prepend="," property="logopath" > #logopath:VARCHAR# </isNotNull> <isNotNull prepend="," property="name" > #name:VARCHAR# </isNotNull> <isNotNull prepend="," property="visible" > #visible:BIT# </isNotNull> ) </dynamic> </insert> <select id="countByExample" parameterClass="cn.zlj.ibatis.entity.BrandExample" resultClass="java.lang.Integer" > select count(*) from brand <include refid="brand.Example_Where_Clause" /> </select> <update id="updateByExampleSelective" > update brand <dynamic prepend="set" > <isNotNull prepend="," property="record.code" > code = #record.code:VARCHAR# </isNotNull> <isNotNull prepend="," property="record.logopath" > logopath = #record.logopath:VARCHAR# </isNotNull> <isNotNull prepend="," property="record.name" > name = #record.name:VARCHAR# </isNotNull> <isNotNull prepend="," property="record.visible" > visible = #record.visible:BIT# </isNotNull> </dynamic> <isParameterPresent > <include refid="brand.Example_Where_Clause" /> </isParameterPresent> </update> <update id="updateByExample" > update brand set code = #record.code:VARCHAR#, logopath = #record.logopath:VARCHAR#, name = #record.name:VARCHAR#, visible = #record.visible:BIT# <isParameterPresent > <include refid="brand.Example_Where_Clause" /> </isParameterPresent> </update> <update id="updateByPrimaryKeySelective" parameterClass="cn.zlj.ibatis.entity.Brand" > update brand <dynamic prepend="set" > <isNotNull prepend="," property="logopath" > logopath = #logopath:VARCHAR# </isNotNull> <isNotNull prepend="," property="name" > name = #name:VARCHAR# </isNotNull> <isNotNull prepend="," property="visible" > visible = #visible:BIT# </isNotNull> </dynamic> where code = #code:VARCHAR# </update> <update id="updateByPrimaryKey" parameterClass="cn.zlj.ibatis.entity.Brand" > update brand set logopath = #logopath:VARCHAR#, name = #name:VARCHAR#, visible = #visible:BIT# where code = #code:VARCHAR# </update> </sqlMap>
OK,修改完成,去掉了很多原来自动生成的注释。
可以自己写实现org.apache.ibatis.ibator.api.CommentGenerato 或者重写org.apache.ibatis.ibator.internal.DefaultCommentGenerator这个类的
ibator1.2.2与以前的版本的主要改变就是以上这几个方法,原来的这种 addFieldComment(Field field, FullyQualifiedTable table, String columnName)方法, 在下一版本中就会被去掉了。
|