定制ibator前的一些思考

阅读更多

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

 

 

  • 生成中文注释[www-hello] http://lj-zhu.iteye.com/blog/732325
  • ibator改造之返回数据库注释和数据库分页[matychen] http://www.iteye.com/topic/821983

 

但在决定对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

 




  
  
  
    
    
    
    
    
    
    
    
    
    
    
  
  
  
    section_id, s_ip, e_ip, start_ip, end_ip, country_code, province_code, area_code, 
    sub_area_code, network_provider, area_desc
  
  
  
  
    
      SELECT nextval('s_ip_section')
    
    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#)
  
  
  
    
      SELECT nextval('s_ip_section')
    
    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#
      
      )
    
  
  
  
  
    delete from ip_section
    where section_id = #sectionId:INTEGER#
  
  
  
  
    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 ip_section
    
      
        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#
  
  
  
  

 

sqlmap XML一般约束

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

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

 

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

 

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

你可能感兴趣的:(ibator,定制前的思考)