最近在项目中用到ibator1.2.1,但是感觉其自动生成的代码不能满足自己的需要,在网上找了资料:
1.
eclipse ibabis代码生成插件abator功能扩展
2.
aquan198163 写道 可以用iBatis的include标签设计类似模板的东西,简化分页sql
自己下载代码进行了相关扩展:
1.支持Oracle、MySQL分页
2.在生成的example类增加了Oracle、MySQL分页的相关字段
0.3版本:
增加了控制生成oracle分页和mysql分页的属性(原来是都生成)
<!--
<table tableName="BROWSELOGS" enableMysqlPagination="true">
<table tableName="BROWSELOGS" enableOraclePagination="false" >
-->
<table tableName="BROWSELOGS">
<columnOverride column="ID" javaType="Integer" />
<columnOverride column="VISITOR" javaType="Integer" />
<columnOverride column="DOTIME" jdbcType="Timestamp" />
</table>
当然这个时候,xml会报错,因为你引用的是网络上面的dtd("http://ibatis.apache.org/dtd/ibator-config_1_0.dtd"),不用理会,一样可以生成,如果都不填写,就是都不生成。
建议在ibatorContext加上
Ibatis2Java5:
<classPathEntry location="D:\UserLibraries\Oracle10g_driver.jar" />
<ibatorContext id="context" targetRuntime="Ibatis2Java5" >
完整版的配置:
<?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:\UserLibraries\mysql-connector-java-5.1.12-bin.jar" />
<ibatorContext id="context1" targetRuntime="Ibatis2Java5">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/demo" userId="root"
password="aaa" />
<javaModelGenerator targetPackage="com.ibatis.pojo"
targetProject="test" />
<sqlMapGenerator targetPackage="com.ibatis.sqlmap"
targetProject="test" />
<daoGenerator targetPackage="com.ibatis.dao"
targetProject="test" type="SPRING" />
<table tableName="ueye_account" enableMysqlPagination="true">
<columnOverride column="ID" javaType="Integer" />
<columnOverride column="VISITOR" javaType="Integer" />
<columnOverride column="DOTIME" jdbcType="Timestamp" />
</table>
</ibatorContext>
</ibatorConfiguration>
0.2版本增加了:
4个list的set方法:
protected List criteriaWithoutValue;
protected List criteriaWithSingleValue;
protected List criteriaWithListValue;
protected List criteriaWithBetweenValue;
原来的方法里面没有set。只有get方法,我觉得有时候我不知道传入的是哪个字段,要自己定义一个条件不知道怎么放进去,我就想法先取出list,在set进去,如下:
// //过滤条件,仿造的是example类中的情况
// Criteria criteria=example.createCriteria();
// //得到example里面的list
// List<Object> listSingle=criteria.getCriteriaWithSingleValue();
// //再把它注入回去
// criteria.setCriteriaWithSingleValue(this.processFilterInfo(listSingle, sigma.getFilterInfo()));
processFilterInfo方法:
public List<Object> processFilterInfo(List<Object> listSingle,List<FilterInfo> filterInfos){
for (int i = 0; i < filterInfos.size(); i++) {
String columnId=filterInfos.get(i).getColumnId();
String logic=filterInfos.get(i).getLogic();
String value=filterInfos.get(i).getValue();
if(logic.equalsIgnoreCase("equal")){
Map<String,Object> map = new HashMap<String,Object>();
map.put("condition", columnId+" =");
map.put("value", value);
listSingle.add(map);
}else if(logic.equalsIgnoreCase("notequal")){
Map<String,Object> map = new HashMap<String,Object>();
map.put("condition", columnId+" <>");
map.put("value", value);
listSingle.add(map);
清单一(修改的相关类):
org.apache.ibatis.ibator.generator.ibatis2.sqlmap---SqlMapGenerator
org.apache.ibatis.ibator.generator.ibatis2.sqlmap.elements---OraclePaginationHeadGenerator
org.apache.ibatis.ibator.generator.ibatis2.sqlmap.elements---OraclePaginationTailGenerator
org.apache.ibatis.ibator.generator.ibatis2.sqlmap.elements---MysqlPaginationLimitGenerator
org.apache.ibatis.ibator.generator.ibatis2.model---ExampleGenerator
org.apache.ibatis.ibator.generator.ibatis2.sqlmap.elements---SelectByExampleWithBLOBsElementGenerator
org.apache.ibatis.ibator.generator.ibatis2.sqlmap.elements---SelectByExampleWithoutBLOBsElementGenerator
org.apache.ibatis.ibator.api.dom.java---FullyQualifiedJavaType
搜索“增加开始”或者“增加结束”就能找到修改的地方
SQL_MAP.xml部分:
<!-- oracle 分页头 -->
<sql id="oracle_Pagination_Head" >
<dynamic >
<isNotEmpty property="oracleStart" >
<isNotEmpty property="oracleEnd" >
<![CDATA[select * from ( select row_.*, rownum rownum_ from ( ]]>
</isNotEmpty>
</isNotEmpty>
</dynamic>
</sql>
<!-- oracle 分页尾 -->
<sql id="oracle_Pagination_Tail" >
<dynamic >
<isNotEmpty property="oracleStart" >
<isNotEmpty property="oracleEnd" >
<![CDATA[) row_ where rownum <= #oracleEnd# ) where rownum_ > #oracleStart#]]>
</isNotEmpty>
</isNotEmpty>
</dynamic>
</sql>
<!-- MySQL 分页 -->
<sql id="mysql_Pagination_Limit" >
<dynamic >
<isNotEmpty property="mysqlOffset" >
<isNotEmpty property="mysqlLength" >
<![CDATA[ limit #mysqlOffset# , #mysqlLength# ]]>
</isNotEmpty>
</isNotEmpty>
</dynamic>
</sql>
<select id="ibatorgenerated_selectByExample" resultMap="ibatorgenerated_BaseResultMap" parameterClass="com.ibatis.pojo.GoodsExample" >
<!-- 增加oracle分页头部 -->
<include refid="oracle_Pagination_Head" />
select id, name, stand, money, leavings, orders
from goods
<isParameterPresent >
<include refid="goods.ibatorgenerated_Example_Where_Clause" />
<isNotNull property="orderByClause" >
order by $orderByClause$
</isNotNull>
</isParameterPresent>
<!-- 增加oracle分页尾部 -->
<include refid="oracle_Pagination_Tail" />
<!-- 增加MySQL分页 -->
<include refid="mysql_Pagination_Limit" />
</select>
Example类部分:
public class GoodsExample {
protected String orderByClause;
private Integer oracleStart;
private Integer oracleEnd;
private Integer mysqlOffset;
private Integer mysqlLength;
protected List oredCriteria;
public GoodsExample() {
oredCriteria = new ArrayList();
}
}
附件为ibator1.2.1插件,直接复制到Eclipse的dropins文件夹下即可