`
Mov_webhobo
  • 浏览: 213632 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

定制ibator前的一些思考

阅读更多

ibator是iBATIS的一个代码生成工具,关于它的定制,可以参考JavaEye上两篇博文:

 

 

 

但在决定对ibator定制前,有几个问题需要思考:

 

  1. 为什么要定制
  2. 哪些功能需要定制
  3. 编码规范

其中最需要注意的是编码规范问题,否则定制出来的代码不符合规范,大家还是需要花时间调整。

结合我们这边的一些实际情况,就这几个问题分别进行下解答。

【注:我们定制的是ibator 1.2.2 使用的iBATIS版本号 2.3.4.726】

 

1.为什么要定制

 

 

原有版本的问题

 

  • 生成无用的注释,如getter / setter注释
  • 生成无用的示例(example)方法、类
  • 不符合技术部编码规范

2.哪些功能需要定制

 

对ibator的定制动作对代码的调整类型上可以分为三类:

 

  1. update类[修改和我们要求不相符的部分]
  2. delete类[删除无用的部分]
  3. insert类[添加我们想要但它没有的部分]

如:

update类 调整DAO方法顺序为 增 删 改 查 其他

delete类 删除Model[POJO、Domain]类getter/setter方法注释、删除默认生成的示例方法或类

insert类 在Model类中覆盖Object的toString() 提取表字段注释为Model属性文档注释  为DAO增加翻页等常用方法 增加生成Service Action等代码 增加自动调整配置文件

 

可以先从简单做起,不要想着一下子做完美

 

3.编码规范

Model、DAO、DAO实现、sqlmap XML的编码规范分别,结合实际代码进行说明:

Model类

 

package mov.common.domain;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * Ip段
 */
public class IpSection {
    /** IP段编号 */
    private Integer sectionId;

    /** 开始IP */
    private Long sIp;

    /** 结束IP */
    private Long eIp;

    /** 开始IP */
    private String startIp;

    /** 结束IP */
    private String endIp;

    /** 国家编码:1中国 -1未知 */
    private Integer countryCode;

    /** 国家编码:1中国 -1未知 */
    private Integer provinceCode;

    /** 国家编码:1中国 -1未知 */
    private Integer areaCode;

    /** 国家编码:1中国 -1未知 */
    private Integer subAreaCode;

    /** 网络接入商类型, 1联通 2电信 4铁通 5长城宽带 6教育网 -1未知 */
    private Integer networkProvider;

    /** 地域描述 */
    private String areaDesc;

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    // getter && setters 省略
}

 Model类一般约定:

1.property尽量使用包装类而非基本类型,以便于判断用户是否输入内容

2.property使用文档注释,尽量只占用一行

3.覆盖Object#toString()方法,并且toString()方法在getter、setter方法前

 

DAO接口

 

package mov.common.dao;

import mov.common.domain.IpSection;

/**
 * IP段DAO接口
 */
public interface IpSectionDAO {
    Integer insert(IpSection record);

    Integer insertSelective(IpSection record);

    int deleteByPrimaryKey(int sectionId);

    int updateByPrimaryKey(IpSection record);

    int updateByPrimaryKeySelective(IpSection record);

    IpSection selectByPrimaryKey(int sectionId);
}

 DAO接口一般约定:

1.根据主键删除、根据主键获取单行尽量使用基本类型

2.方法按照增、删、改、查的前后顺序分组排列

3.增 删 改返回值类型保存ibator原有返回值,增加返回Integer 删除 修改 返回int

 

DAO实现类

 

package mov.common.dao.impl;

import mov.common.dao.IpSectionDAO;
import mov.common.domain.IpSection;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

/**
 * IP段DAO实现
 */
public class IpSectionDAOImpl extends SqlMapClientDaoSupport implements
		IpSectionDAO {

	public Integer insert(IpSection record) {
		return (Integer) getSqlMapClientTemplate().insert(
				"ip_section.insert", record);
	}

	public Integer insertSelective(IpSection record) {
		return (Integer) getSqlMapClientTemplate().insert(
				"ip_section.insertSelective", record);
	}

	public int deleteByPrimaryKey(int sectionId) {
		return getSqlMapClientTemplate().delete(
				"ip_section.deleteByPrimaryKey", sectionId);
	}

	public int updateByPrimaryKey(IpSection record) {
		return getSqlMapClientTemplate().update(
				"ip_section.updateByPrimaryKey", record);
	}

	public int updateByPrimaryKeySelective(IpSection record) {
		return getSqlMapClientTemplate().update(
				"ip_section.updateByPrimaryKeySelective", record);
	}

	public IpSection selectByPrimaryKey(int sectionId) {
		return (IpSection) getSqlMapClientTemplate().queryForObject(
				"ip_section.selectByPrimaryKey", sectionId);
	}
}

DAO实现一般约束:

1.尽量减少中间变量,ibator原版中生成的方法体有些冗余

2.应用的SQL需要使用命名空间

 

 

sqlmap XML

 

<?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="ip_section" >
  <typeAlias alias="IpSection" type="cn.xxt.common.domain.IpSection"/>
  
  <resultMap id="BaseResultMap" class="IpSection" >
    <result column="section_id" property="sectionId" jdbcType="INTEGER" />
    <result column="s_ip" property="sIp" jdbcType="INTEGER" />
    <result column="e_ip" property="eIp" jdbcType="INTEGER" />
    <result column="start_ip" property="startIp" jdbcType="VARCHAR" />
    <result column="end_ip" property="endIp" jdbcType="VARCHAR" />
    <result column="country_code" property="countryCode" jdbcType="INTEGER" />
    <result column="province_code" property="provinceCode" jdbcType="INTEGER" />
    <result column="area_code" property="areaCode" jdbcType="INTEGER" />
    <result column="sub_area_code" property="subAreaCode" jdbcType="INTEGER" />
    <result column="network_provider" property="networkProvider" jdbcType="TINYINT" />
    <result column="area_desc" property="areaDesc" jdbcType="VARCHAR" />
  </resultMap>
  
  <sql id="Base_Column_List" >
    section_id, s_ip, e_ip, start_ip, end_ip, country_code, province_code, area_code, 
    sub_area_code, network_provider, area_desc
  </sql>
  
  <!-- ========================================================================
  	INSERT
  ========================================================================= -->
  <insert id="insert" parameterClass="IpSection" >
    <selectKey resultClass="java.lang.Integer" keyProperty="sectionId" >
      SELECT nextval('s_ip_section')
    </selectKey>
    insert into ip_section (section_id, s_ip, e_ip, start_ip, end_ip, 
      country_code, province_code, area_code, sub_area_code, 
      network_provider, area_desc)
    values (#sectionId:INTEGER#, #sIp:INTEGER#, #eIp:INTEGER#, #startIp:VARCHAR#, #endIp:VARCHAR#, 
      #countryCode:INTEGER#, #provinceCode:INTEGER#, #areaCode:INTEGER#, #subAreaCode:INTEGER#, 
      #networkProvider:TINYINT#, #areaDesc:VARCHAR#)
  </insert>
  
  <insert id="insertSelective" parameterClass="IpSection" >
    <selectKey resultClass="java.lang.Integer" keyProperty="sectionId" >
      SELECT nextval('s_ip_section')
    </selectKey>
    insert into ip_section
    <dynamic prepend="(" >
      <isNotNull prepend="," property="sectionId" >
        section_id
      </isNotNull>
      <isNotNull prepend="," property="sIp" >
        s_ip
      </isNotNull>
      <isNotNull prepend="," property="eIp" >
        e_ip
      </isNotNull>
      <isNotNull prepend="," property="startIp" >
        start_ip
      </isNotNull>
      <isNotNull prepend="," property="endIp" >
        end_ip
      </isNotNull>
      <isNotNull prepend="," property="countryCode" >
        country_code
      </isNotNull>
      <isNotNull prepend="," property="provinceCode" >
        province_code
      </isNotNull>
      <isNotNull prepend="," property="areaCode" >
        area_code
      </isNotNull>
      <isNotNull prepend="," property="subAreaCode" >
        sub_area_code
      </isNotNull>
      <isNotNull prepend="," property="networkProvider" >
        network_provider
      </isNotNull>
      <isNotNull prepend="," property="areaDesc" >
        area_desc
      </isNotNull>
      )
    </dynamic>
    values
    <dynamic prepend="(" >
      <isNotNull prepend="," property="sectionId" >
        #sectionId:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="sIp" >
        #sIp:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="eIp" >
        #eIp:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="startIp" >
        #startIp:VARCHAR#
      </isNotNull>
      <isNotNull prepend="," property="endIp" >
        #endIp:VARCHAR#
      </isNotNull>
      <isNotNull prepend="," property="countryCode" >
        #countryCode:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="provinceCode" >
        #provinceCode:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="areaCode" >
        #areaCode:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="subAreaCode" >
        #subAreaCode:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="networkProvider" >
        #networkProvider:TINYINT#
      </isNotNull>
      <isNotNull prepend="," property="areaDesc" >
        #areaDesc:VARCHAR#
      </isNotNull>
      )
    </dynamic>
  </insert>
  
  <!-- ========================================================================
  	DELETE
  ========================================================================= -->
  <delete id="deleteByPrimaryKey" parameterClass="int" >
    delete from ip_section
    where section_id = #sectionId:INTEGER#
  </delete>
  
  <!-- ========================================================================
  	UPDATE
  ========================================================================= -->
  <update id="updateByPrimaryKey" parameterClass="IpSection" >
    update ip_section
    set s_ip = #sIp:INTEGER#,
      e_ip = #eIp:INTEGER#,
      start_ip = #startIp:VARCHAR#,
      end_ip = #endIp:VARCHAR#,
      country_code = #countryCode:INTEGER#,
      province_code = #provinceCode:INTEGER#,
      area_code = #areaCode:INTEGER#,
      sub_area_code = #subAreaCode:INTEGER#,
      network_provider = #networkProvider:TINYINT#,
      area_desc = #areaDesc:VARCHAR#
    where section_id = #sectionId:INTEGER#
  </update>
  
  <update id="updateByPrimaryKeySelective" parameterClass="IpSection" >
    update ip_section
    <dynamic prepend="set" >
      <isNotNull prepend="," property="sIp" >
        s_ip = #sIp:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="eIp" >
        e_ip = #eIp:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="startIp" >
        start_ip = #startIp:VARCHAR#
      </isNotNull>
      <isNotNull prepend="," property="endIp" >
        end_ip = #endIp:VARCHAR#
      </isNotNull>
      <isNotNull prepend="," property="countryCode" >
        country_code = #countryCode:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="provinceCode" >
        province_code = #provinceCode:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="areaCode" >
        area_code = #areaCode:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="subAreaCode" >
        sub_area_code = #subAreaCode:INTEGER#
      </isNotNull>
      <isNotNull prepend="," property="networkProvider" >
        network_provider = #networkProvider:TINYINT#
      </isNotNull>
      <isNotNull prepend="," property="areaDesc" >
        area_desc = #areaDesc:VARCHAR#
      </isNotNull>
    </dynamic>
    where section_id = #sectionId:INTEGER#
  </update>
  
  <!-- ========================================================================
  	SELECT
  ========================================================================= -->
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterClass="int" >
    select 
    <include refid="ip_section.Base_Column_List" />
    from ip_section
    where section_id = #sectionId:INTEGER#
  </select>
</sqlMap>

 

sqlmap XML一般约束

1.按照resultMap sql片段 增 删 改 查的顺序分组排列

2.由于XML文件一般长度会较长,建议对增 删 改 查 增加类似示例中的注释以方便快速

 

这些规范仅仅是我们这儿的一些粗略约定,读者需要根据所在公司的实际情况进行认真、细致的分析

 

定制ibator并不是特别难,但是要定制一个实用、符合规范的ibator必须做好这些准备工作,否则事倍功半,何苦折腾呢!

分享到:
评论

相关推荐

    iBATOR-V1.1.0

    iBATOR is a code generator for iBATIS.

    ibator优化的jar包

    ibator插件优化的jar包,安装完ibator后,将eclipse\plugins\org.apache.ibatis.ibator.core_1.2.1下的jar包替换即可。

    ibator1.2.2无注释

    ibator1.2.2多了点功能,具体可以百度,重新编译了下,生成注释去掉了

    eclipse集成的ibator插件

    eclipse的集成插件,ibator.jar,可以直接mybatis导表,图形化展示,简单易用,直接放到eclipse的plugin文件夹下即可

    ibator教学视频

    ibator教学视频,手把手教你使用ibator

    为 Ibatis 2.3.4 构建增强的 Apache Ibator 实体类生成工具

    Ibator is a code generator for iBATIS. Ibator will introspect a database table (or many tables) and will generate iBATIS artifacts that can be used to access the table(s). This abates some of the ...

    ibator使用指导

    在myelipse7.5中安装ibator插件的方法与安装一般插件的方法是一致的,有以下2种方式(个人推荐使用手动安装方式,避免网络等原因造成安装不成功): 1. 手动安装方式 将邮件中附件的Ibator插件压缩文件IbatorForEclipse...

    ibator 1.2.1

    eclipse的ibatis代码生成器,最新版ibator 1.2.1

    ibator使用心得

    ibator相对于hibernate框架能完全自主编写sql代码,同时又有hibernate便于管理的优点,是非常理想的持久层技术

    Ibator参考程序

    做SSI项目时,Ibator映射出来的内容实在太多了,有很多内容都用不上,参考Ibator的结构,实现对基本字段和方法的映射!

    ibator1.2.1配置文件

    自动生成dto\dao\xml 自带批处理自动生成程序

    IBATOR动态生成sql和DAO层

    此项目通过对ibator的改造,通过执行cmd命令自动生成sql与Dao,大大提高开发效率

    ibator优化版,使用数据库的注释

    使用数据库的注释,不用自带的注释 http://blog.csdn.net/tiantangpw/article/details/43489817 运行命令 java -jar ibator.jar -configfile ibatorConfig.xml -overwrite &gt;&gt;ibator.log

    ibator-config_1_1.dtd

    ibator-config_1_1.dtd ibator用

    ibator API帮助文档.chm

    Ibatis 生成器 Ibator 的 API 文档

    ibator插件+ibatorConfig文件

    ibator插件+ibatorConfig文件,加入到eclipse中,按照配置文件修改一下就ok了

    ibator的eclipse插件

    直接丢到eclipse的plugins目录下就可以了,在一些公司下载不了的地方,方便使用

    IBator的安装使用

    ibatis 配置文件自动生成工具

    处理后的ibator1.2.1

    去除了注释、去除Example方法及去除生成的id前面的“ibatorgenerated_”

    ibator1.2.1

    用户ibatis自动成才代码的一种工具,减少开发人员的工作量。

Global site tag (gtag.js) - Google Analytics