最近要做一个项目(私人的,呵呵),因为新公司的新项目是用 spring2.5+ibatis 开发的,所以为了熟悉ibatis ,我的私人项目就决定使用spring3.0 + mybatis3.0 来边熟练边做。
我是一个懒人,在mybatis官网的时候偶然看到竟然有表生成器MBG(一开始还以为只是生成实体对象),就下载下来用了(省了写bean的时间,呵呵)。没想到这工具竟然不是IDE的插件(和hibernate差好多啊),而是要手写(支持用各种方式,比如ant,或者java代码)生成文件,没办法,又下载了这工具的文档看了下,花了点时间,总算用java把这工具写好了。
写好之后,意外发现了,这工具不单帮我生成了实体bean,还帮我生成了相对应的XML文件(里面竟然连增,删,改,查 已经都写好了,不过仅支持单表操作,不知道和我的表没有设外键有没关系,没试过)以及 dao的接口mapper文件,这下爽死了,连配置文件和接口都省的写了(至少省了单表操作的sql)。
使用了下,发现问题了,查询不支持分页,无语,但是我又不甘心啊,如果表都用这玩意生成那我不是爽死了,可以省了很多事,所以我又去官网down了这工具的源码下来研究,并修改了源码,终于支持分页了(我只增加了mysql的分页),哈哈。。
废话不多说了,直接上修改方法。
1、修改源码要注意的事情:源码包的路径是绝对不能修改的,因为发现源码里面有些配置文件的路径是写死的, - - 这悲剧的。。
2、其他的不多说,有兴趣自己研究,不算很难,我只说我要修改的文件。
(1)\org\mybatis\generator\codegen\mybatis3\xmlmapper 这个包里面的类都是为了生成配置xml文件,在里面的elements包下的SelectByExampleWithoutBLOBsElementGenerator.java文件,这个文件是写查询的sql语句,修改如下:
修改addElements方法如下:
public void addElements(XmlElement parentElement) {
String fqjt = introspectedTable.getExampleType();
XmlElement answer = new XmlElement("select"); //$NON-NLS-1$
answer.addAttribute(new Attribute("id", //$NON-NLS-1$
introspectedTable.getSelectByExampleStatementId()));
answer.addAttribute(new Attribute(
"resultMap", introspectedTable.getBaseResultMapId())); //$NON-NLS-1$
answer.addAttribute(new Attribute("parameterType", fqjt)); //$NON-NLS-1$
context.getCommentGenerator().addComment(answer);
answer.addElement(new TextElement("select")); //$NON-NLS-1$
XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
ifElement.addAttribute(new Attribute("test", "distinct")); //$NON-NLS-1$ //$NON-NLS-2$
ifElement.addElement(new TextElement("distinct")); //$NON-NLS-1$
answer.addElement(ifElement);
StringBuilder sb = new StringBuilder();
if (stringHasValue(introspectedTable
.getSelectByExampleQueryId())) {
sb.append('\'');
sb.append(introspectedTable.getSelectByExampleQueryId());
sb.append("' as QUERYID,"); //$NON-NLS-1$
answer.addElement(new TextElement(sb.toString()));
}
answer.addElement(getBaseColumnListElement());
sb.setLength(0);
sb.append("from "); //$NON-NLS-1$
sb.append(introspectedTable
.getAliasedFullyQualifiedTableNameAtRuntime());
answer.addElement((new TextElement(sb.toString())));
answer.addElement(getExampleIncludeElement());
ifElement = new XmlElement("if"); //$NON-NLS-1$
ifElement.addAttribute(new Attribute("test", "orderByClause != null"));
ifElement.addElement(new TextElement("order by ${orderByClause}"));
answer.addElement(ifElement);
//---------------增加分页added by oxj 生成对应的xml配置分页的节点---------------------------
XmlElement foreachElement = new XmlElement("foreach");
foreachElement.addAttribute(new Attribute("collection", "oredCriteria"));
foreachElement.addAttribute(new Attribute("item","criteria"));
ifElement = new XmlElement("if");
ifElement.addAttribute(new Attribute("test","criteria.valid"));
foreachElement.addElement(ifElement);
XmlElement foreach2Element = new XmlElement("foreach");
foreach2Element.addAttribute(new Attribute("collection", "criteria.criteria"));
foreach2Element.addAttribute(new Attribute("item","criterion"));
ifElement.addElement(foreach2Element);
XmlElement if2Element = new XmlElement("if");
if2Element.addAttribute(new Attribute("test","criterion.limitValue"));
foreach2Element.addElement(if2Element);
if2Element.addElement(new TextElement("limit ${criterion.value},${criterion.secondValue}"));
answer.addElement(ifElement);
/** 以上创建的分页节点结构如下
* <foreach collection="oredCriteria" item="criteria">
* <if test="criteria.valid">
* <foreach collection="criteria.criteria" item="criterion">
* <if test= "criterion.limitValue">
* limit ${criterion.value},${criterion.secondValue}
* </if>
* </foreach>
* </if>
* </foreach>
*/
//------------------------------------------
if (context.getPlugins()
.sqlMapSelectByExampleWithoutBLOBsElementGenerated(answer,
introspectedTable)) {
parentElement.addElement(answer);
}
}
其中 //--------- 这种注释段落是我增加的代码。
(2)org\mybatis\generator\codegen\mybatis3\model这个包下的文件是为了生成实体bean以及相应的dao接口
我要修改里面的ExampleGenerator.java文件,修改如下:
a、修改getCriterionInnerClass方法如下:
private InnerClass getCriterionInnerClass(TopLevelClass topLevelClass) {
Field field;
Method method;
InnerClass answer = new InnerClass(new FullyQualifiedJavaType(
"Criterion")); //$NON-NLS-1$
answer.setVisibility(JavaVisibility.PUBLIC);
answer.setStatic(true);
context.getCommentGenerator().addClassComment(answer,
introspectedTable);
field = new Field();
field.setName("condition"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getStringInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("value"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getObjectInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("secondValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getObjectInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("noValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("singleValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("betweenValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
//--------------增加分页的功能added by oxj 增加一个boolean的变量,标识该对象是否是分页条件-----------
field = new Field();
field.setName("limitValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
//---------------------------------------------
field = new Field();
field.setName("listValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("typeHandler"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getStringInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("Criterion"); //$NON-NLS-1$
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addBodyLine("super();"); //$NON-NLS-1$
method.addBodyLine("this.condition = condition;"); //$NON-NLS-1$
method.addBodyLine("this.typeHandler = null;"); //$NON-NLS-1$
method.addBodyLine("this.noValue = true;"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("Criterion"); //$NON-NLS-1$
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "typeHandler")); //$NON-NLS-1$
method.addBodyLine("super();"); //$NON-NLS-1$
method.addBodyLine("this.condition = condition;"); //$NON-NLS-1$
method.addBodyLine("this.value = value;"); //$NON-NLS-1$
method.addBodyLine("this.typeHandler = typeHandler;"); //$NON-NLS-1$
method.addBodyLine("if (value instanceof List<?>) {"); //$NON-NLS-1$
method.addBodyLine("this.listValue = true;"); //$NON-NLS-1$
method.addBodyLine("} else {"); //$NON-NLS-1$
method.addBodyLine("this.singleValue = true;"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("Criterion"); //$NON-NLS-1$
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addBodyLine("this(condition, value, null);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("Criterion"); //$NON-NLS-1$
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "secondValue")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "typeHandler")); //$NON-NLS-1$
method.addBodyLine("super();"); //$NON-NLS-1$
method.addBodyLine("this.condition = condition;"); //$NON-NLS-1$
method.addBodyLine("this.value = value;"); //$NON-NLS-1$
method.addBodyLine("this.secondValue = secondValue;"); //$NON-NLS-1$
method.addBodyLine("this.typeHandler = typeHandler;"); //$NON-NLS-1$
//-------------增加分页added by oxj 为了和betweenValue区分到底是哪个赋值---------------
method.addBodyLine("if(\"limit\".equals(condition))");
method.addBodyLine(" this.limitValue = true;"); //$NON-NLS-1$
method.addBodyLine("else");
//-------------------------------------------
method.addBodyLine(" this.betweenValue = true;"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("Criterion"); //$NON-NLS-1$
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "secondValue")); //$NON-NLS-1$
method.addBodyLine("this(condition, value, secondValue, null);"); //$NON-NLS-1$
answer.addMethod(method);
return answer;
}
同上 //----------部分是我增加的代码,
b、修改getGeneratedCriteriaInnerClass方法如下:
private InnerClass getGeneratedCriteriaInnerClass(
TopLevelClass topLevelClass) {
Field field;
Method method;
InnerClass answer = new InnerClass(FullyQualifiedJavaType
.getGeneratedCriteriaInstance());
answer.setVisibility(JavaVisibility.PROTECTED);
answer.setStatic(true);
answer.setAbstract(true);
context.getCommentGenerator().addClassComment(answer,
introspectedTable);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("GeneratedCriteria"); //$NON-NLS-1$
method.setConstructor(true);
method.addBodyLine("super();"); //$NON-NLS-1$
method.addBodyLine("criteria = new ArrayList<Criterion>();"); //$NON-NLS-1$
answer.addMethod(method);
List<String> criteriaLists = new ArrayList<String>();
criteriaLists.add("criteria"); //$NON-NLS-1$
for (IntrospectedColumn introspectedColumn : introspectedTable
.getNonBLOBColumns()) {
if (stringHasValue(introspectedColumn
.getTypeHandler())) {
String name = addtypeHandledObjectsAndMethods(
introspectedColumn, method, answer);
criteriaLists.add(name);
}
}
// now generate the isValid method
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("isValid"); //$NON-NLS-1$
method.setReturnType(FullyQualifiedJavaType
.getBooleanPrimitiveInstance());
StringBuilder sb = new StringBuilder();
Iterator<String> strIter = criteriaLists.iterator();
sb.append("return "); //$NON-NLS-1$
sb.append(strIter.next());
sb.append(".size() > 0"); //$NON-NLS-1$
if (!strIter.hasNext()) {
sb.append(';');
}
method.addBodyLine(sb.toString());
while (strIter.hasNext()) {
sb.setLength(0);
OutputUtilities.javaIndent(sb, 1);
sb.append("|| "); //$NON-NLS-1$
sb.append(strIter.next());
sb.append(".size() > 0"); //$NON-NLS-1$
if (!strIter.hasNext()) {
sb.append(';');
}
method.addBodyLine(sb.toString());
}
answer.addMethod(method);
// now generate the getAllCriteria method
if (criteriaLists.size() > 1) {
field = new Field();
field.setName("allCriteria"); //$NON-NLS-1$
field.setType(new FullyQualifiedJavaType("List<Criterion>")); //$NON-NLS-1$
field.setVisibility(JavaVisibility.PROTECTED);
answer.addField(field);
}
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("getAllCriteria"); //$NON-NLS-1$
method.setReturnType(new FullyQualifiedJavaType("List<Criterion>")); //$NON-NLS-1$
if (criteriaLists.size() < 2) {
method.addBodyLine("return criteria;"); //$NON-NLS-1$
} else {
method.addBodyLine("if (allCriteria == null) {"); //$NON-NLS-1$
method.addBodyLine("allCriteria = new ArrayList<Criterion>();"); //$NON-NLS-1$
strIter = criteriaLists.iterator();
while (strIter.hasNext()) {
method.addBodyLine(String.format("allCriteria.addAll(%s);", strIter.next())); //$NON-NLS-1$
}
method.addBodyLine("}"); //$NON-NLS-1$
method.addBodyLine("return allCriteria;"); //$NON-NLS-1$
}
answer.addMethod(method);
//--------------增加分页added by oxj 增加分页的方法对外开放供调用-------------------------------------
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("setLimitValue"); //$NON-NLS-1$
method.setReturnType(new FullyQualifiedJavaType("Criteria")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getIntInstance(), "value1")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getIntInstance(), "value2")); //$NON-NLS-1$
method.addBodyLine("if (value2==0 )");
method.addBodyLine(" throw new RuntimeException(\"value2 is 0\");");
method.addBodyLine("addCriterion(\"limit\",value1,value2,\"null\");");
method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$
answer.addMethod(method);
//--------------------------------------------------
//--------------增加分页added by oxj 增加limitValue值的get方法,供配置的xml文件的OGNL表达式使用-------------------------------------
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("getLimitValue"); //$NON-NLS-1$
method.setReturnType(new FullyQualifiedJavaType("Criteria")); //$NON-NLS-1$
method.addBodyLine("return (Criteria)criteria;"); //$NON-NLS-1$
answer.addMethod(method);
//--------------------------------------------------
// now we need to generate the methods that will be used in the SqlMap
// to generate the dynamic where clause
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewListInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewArrayListInstance());
field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
FullyQualifiedJavaType listOfCriterion = new FullyQualifiedJavaType(
"java.util.List<Criterion>"); //$NON-NLS-1$
field.setType(listOfCriterion);
field.setName("criteria"); //$NON-NLS-1$
answer.addField(field);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(field.getType());
method.setName(getGetterMethodName(field.getName(), field
.getType()));
method.addBodyLine("return criteria;"); //$NON-NLS-1$
answer.addMethod(method);
// now add the methods for simplifying the individual field set methods
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterion"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addBodyLine("if (condition == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value for condition cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method.addBodyLine("criteria.add(new Criterion(condition));"); //$NON-NLS-1$
if (criteriaLists.size() > 1) {
method.addBodyLine("allCriteria = null;"); //$NON-NLS-1$
}
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterion"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
if (criteriaLists.size() > 1) {
method.addBodyLine("allCriteria = null;"); //$NON-NLS-1$
}
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterion"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value1")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value2")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("criteria.add(new Criterion(condition, value1, value2));"); //$NON-NLS-1$
if (criteriaLists.size() > 1) {
method.addBodyLine("allCriteria = null;"); //$NON-NLS-1$
}
answer.addMethod(method);
FullyQualifiedJavaType listOfDates = new FullyQualifiedJavaType(
"java.util.List<java.util.Date>"); //$NON-NLS-1$
if (introspectedTable.hasJDBCDateColumns()) {
topLevelClass.addImportedType(FullyQualifiedJavaType
.getDateInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewIteratorInstance());
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("addCriterion(condition, new java.sql.Date(value.getTime()), property);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(listOfDates, "values")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (values == null || values.size() == 0) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();"); //$NON-NLS-1$
method.addBodyLine("Iterator<Date> iter = values.iterator();"); //$NON-NLS-1$
method.addBodyLine("while (iter.hasNext()) {"); //$NON-NLS-1$
method
.addBodyLine("dateList.add(new java.sql.Date(iter.next().getTime()));"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method.addBodyLine("addCriterion(condition, dateList, property);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value1")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value2")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);"); //$NON-NLS-1$
answer.addMethod(method);
}
修改完了,然后只要运行之前写好的MBG工具即可看到生成的代码。
这里放上我写好的附件,
MBG.rar是修改后的源码,
MBGTools是我用java代码写好的 MBG 工具 。
我是一个懒人,在mybatis官网的时候偶然看到竟然有表生成器MBG(一开始还以为只是生成实体对象),就下载下来用了(省了写bean的时间,呵呵)。没想到这工具竟然不是IDE的插件(和hibernate差好多啊),而是要手写(支持用各种方式,比如ant,或者java代码)生成文件,没办法,又下载了这工具的文档看了下,花了点时间,总算用java把这工具写好了。
写好之后,意外发现了,这工具不单帮我生成了实体bean,还帮我生成了相对应的XML文件(里面竟然连增,删,改,查 已经都写好了,不过仅支持单表操作,不知道和我的表没有设外键有没关系,没试过)以及 dao的接口mapper文件,这下爽死了,连配置文件和接口都省的写了(至少省了单表操作的sql)。
使用了下,发现问题了,查询不支持分页,无语,但是我又不甘心啊,如果表都用这玩意生成那我不是爽死了,可以省了很多事,所以我又去官网down了这工具的源码下来研究,并修改了源码,终于支持分页了(我只增加了mysql的分页),哈哈。。
废话不多说了,直接上修改方法。
1、修改源码要注意的事情:源码包的路径是绝对不能修改的,因为发现源码里面有些配置文件的路径是写死的, - - 这悲剧的。。
2、其他的不多说,有兴趣自己研究,不算很难,我只说我要修改的文件。
(1)\org\mybatis\generator\codegen\mybatis3\xmlmapper 这个包里面的类都是为了生成配置xml文件,在里面的elements包下的SelectByExampleWithoutBLOBsElementGenerator.java文件,这个文件是写查询的sql语句,修改如下:
修改addElements方法如下:
public void addElements(XmlElement parentElement) {
String fqjt = introspectedTable.getExampleType();
XmlElement answer = new XmlElement("select"); //$NON-NLS-1$
answer.addAttribute(new Attribute("id", //$NON-NLS-1$
introspectedTable.getSelectByExampleStatementId()));
answer.addAttribute(new Attribute(
"resultMap", introspectedTable.getBaseResultMapId())); //$NON-NLS-1$
answer.addAttribute(new Attribute("parameterType", fqjt)); //$NON-NLS-1$
context.getCommentGenerator().addComment(answer);
answer.addElement(new TextElement("select")); //$NON-NLS-1$
XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
ifElement.addAttribute(new Attribute("test", "distinct")); //$NON-NLS-1$ //$NON-NLS-2$
ifElement.addElement(new TextElement("distinct")); //$NON-NLS-1$
answer.addElement(ifElement);
StringBuilder sb = new StringBuilder();
if (stringHasValue(introspectedTable
.getSelectByExampleQueryId())) {
sb.append('\'');
sb.append(introspectedTable.getSelectByExampleQueryId());
sb.append("' as QUERYID,"); //$NON-NLS-1$
answer.addElement(new TextElement(sb.toString()));
}
answer.addElement(getBaseColumnListElement());
sb.setLength(0);
sb.append("from "); //$NON-NLS-1$
sb.append(introspectedTable
.getAliasedFullyQualifiedTableNameAtRuntime());
answer.addElement((new TextElement(sb.toString())));
answer.addElement(getExampleIncludeElement());
ifElement = new XmlElement("if"); //$NON-NLS-1$
ifElement.addAttribute(new Attribute("test", "orderByClause != null"));
ifElement.addElement(new TextElement("order by ${orderByClause}"));
answer.addElement(ifElement);
//---------------增加分页added by oxj 生成对应的xml配置分页的节点---------------------------
XmlElement foreachElement = new XmlElement("foreach");
foreachElement.addAttribute(new Attribute("collection", "oredCriteria"));
foreachElement.addAttribute(new Attribute("item","criteria"));
ifElement = new XmlElement("if");
ifElement.addAttribute(new Attribute("test","criteria.valid"));
foreachElement.addElement(ifElement);
XmlElement foreach2Element = new XmlElement("foreach");
foreach2Element.addAttribute(new Attribute("collection", "criteria.criteria"));
foreach2Element.addAttribute(new Attribute("item","criterion"));
ifElement.addElement(foreach2Element);
XmlElement if2Element = new XmlElement("if");
if2Element.addAttribute(new Attribute("test","criterion.limitValue"));
foreach2Element.addElement(if2Element);
if2Element.addElement(new TextElement("limit ${criterion.value},${criterion.secondValue}"));
answer.addElement(ifElement);
/** 以上创建的分页节点结构如下
* <foreach collection="oredCriteria" item="criteria">
* <if test="criteria.valid">
* <foreach collection="criteria.criteria" item="criterion">
* <if test= "criterion.limitValue">
* limit ${criterion.value},${criterion.secondValue}
* </if>
* </foreach>
* </if>
* </foreach>
*/
//------------------------------------------
if (context.getPlugins()
.sqlMapSelectByExampleWithoutBLOBsElementGenerated(answer,
introspectedTable)) {
parentElement.addElement(answer);
}
}
其中 //--------- 这种注释段落是我增加的代码。
(2)org\mybatis\generator\codegen\mybatis3\model这个包下的文件是为了生成实体bean以及相应的dao接口
我要修改里面的ExampleGenerator.java文件,修改如下:
a、修改getCriterionInnerClass方法如下:
private InnerClass getCriterionInnerClass(TopLevelClass topLevelClass) {
Field field;
Method method;
InnerClass answer = new InnerClass(new FullyQualifiedJavaType(
"Criterion")); //$NON-NLS-1$
answer.setVisibility(JavaVisibility.PUBLIC);
answer.setStatic(true);
context.getCommentGenerator().addClassComment(answer,
introspectedTable);
field = new Field();
field.setName("condition"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getStringInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("value"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getObjectInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("secondValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getObjectInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("noValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("singleValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("betweenValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
//--------------增加分页的功能added by oxj 增加一个boolean的变量,标识该对象是否是分页条件-----------
field = new Field();
field.setName("limitValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
//---------------------------------------------
field = new Field();
field.setName("listValue"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
field.setName("typeHandler"); //$NON-NLS-1$
field.setType(FullyQualifiedJavaType.getStringInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("Criterion"); //$NON-NLS-1$
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addBodyLine("super();"); //$NON-NLS-1$
method.addBodyLine("this.condition = condition;"); //$NON-NLS-1$
method.addBodyLine("this.typeHandler = null;"); //$NON-NLS-1$
method.addBodyLine("this.noValue = true;"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("Criterion"); //$NON-NLS-1$
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "typeHandler")); //$NON-NLS-1$
method.addBodyLine("super();"); //$NON-NLS-1$
method.addBodyLine("this.condition = condition;"); //$NON-NLS-1$
method.addBodyLine("this.value = value;"); //$NON-NLS-1$
method.addBodyLine("this.typeHandler = typeHandler;"); //$NON-NLS-1$
method.addBodyLine("if (value instanceof List<?>) {"); //$NON-NLS-1$
method.addBodyLine("this.listValue = true;"); //$NON-NLS-1$
method.addBodyLine("} else {"); //$NON-NLS-1$
method.addBodyLine("this.singleValue = true;"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("Criterion"); //$NON-NLS-1$
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addBodyLine("this(condition, value, null);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("Criterion"); //$NON-NLS-1$
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "secondValue")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "typeHandler")); //$NON-NLS-1$
method.addBodyLine("super();"); //$NON-NLS-1$
method.addBodyLine("this.condition = condition;"); //$NON-NLS-1$
method.addBodyLine("this.value = value;"); //$NON-NLS-1$
method.addBodyLine("this.secondValue = secondValue;"); //$NON-NLS-1$
method.addBodyLine("this.typeHandler = typeHandler;"); //$NON-NLS-1$
//-------------增加分页added by oxj 为了和betweenValue区分到底是哪个赋值---------------
method.addBodyLine("if(\"limit\".equals(condition))");
method.addBodyLine(" this.limitValue = true;"); //$NON-NLS-1$
method.addBodyLine("else");
//-------------------------------------------
method.addBodyLine(" this.betweenValue = true;"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("Criterion"); //$NON-NLS-1$
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "secondValue")); //$NON-NLS-1$
method.addBodyLine("this(condition, value, secondValue, null);"); //$NON-NLS-1$
answer.addMethod(method);
return answer;
}
同上 //----------部分是我增加的代码,
b、修改getGeneratedCriteriaInnerClass方法如下:
private InnerClass getGeneratedCriteriaInnerClass(
TopLevelClass topLevelClass) {
Field field;
Method method;
InnerClass answer = new InnerClass(FullyQualifiedJavaType
.getGeneratedCriteriaInstance());
answer.setVisibility(JavaVisibility.PROTECTED);
answer.setStatic(true);
answer.setAbstract(true);
context.getCommentGenerator().addClassComment(answer,
introspectedTable);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("GeneratedCriteria"); //$NON-NLS-1$
method.setConstructor(true);
method.addBodyLine("super();"); //$NON-NLS-1$
method.addBodyLine("criteria = new ArrayList<Criterion>();"); //$NON-NLS-1$
answer.addMethod(method);
List<String> criteriaLists = new ArrayList<String>();
criteriaLists.add("criteria"); //$NON-NLS-1$
for (IntrospectedColumn introspectedColumn : introspectedTable
.getNonBLOBColumns()) {
if (stringHasValue(introspectedColumn
.getTypeHandler())) {
String name = addtypeHandledObjectsAndMethods(
introspectedColumn, method, answer);
criteriaLists.add(name);
}
}
// now generate the isValid method
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("isValid"); //$NON-NLS-1$
method.setReturnType(FullyQualifiedJavaType
.getBooleanPrimitiveInstance());
StringBuilder sb = new StringBuilder();
Iterator<String> strIter = criteriaLists.iterator();
sb.append("return "); //$NON-NLS-1$
sb.append(strIter.next());
sb.append(".size() > 0"); //$NON-NLS-1$
if (!strIter.hasNext()) {
sb.append(';');
}
method.addBodyLine(sb.toString());
while (strIter.hasNext()) {
sb.setLength(0);
OutputUtilities.javaIndent(sb, 1);
sb.append("|| "); //$NON-NLS-1$
sb.append(strIter.next());
sb.append(".size() > 0"); //$NON-NLS-1$
if (!strIter.hasNext()) {
sb.append(';');
}
method.addBodyLine(sb.toString());
}
answer.addMethod(method);
// now generate the getAllCriteria method
if (criteriaLists.size() > 1) {
field = new Field();
field.setName("allCriteria"); //$NON-NLS-1$
field.setType(new FullyQualifiedJavaType("List<Criterion>")); //$NON-NLS-1$
field.setVisibility(JavaVisibility.PROTECTED);
answer.addField(field);
}
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("getAllCriteria"); //$NON-NLS-1$
method.setReturnType(new FullyQualifiedJavaType("List<Criterion>")); //$NON-NLS-1$
if (criteriaLists.size() < 2) {
method.addBodyLine("return criteria;"); //$NON-NLS-1$
} else {
method.addBodyLine("if (allCriteria == null) {"); //$NON-NLS-1$
method.addBodyLine("allCriteria = new ArrayList<Criterion>();"); //$NON-NLS-1$
strIter = criteriaLists.iterator();
while (strIter.hasNext()) {
method.addBodyLine(String.format("allCriteria.addAll(%s);", strIter.next())); //$NON-NLS-1$
}
method.addBodyLine("}"); //$NON-NLS-1$
method.addBodyLine("return allCriteria;"); //$NON-NLS-1$
}
answer.addMethod(method);
//--------------增加分页added by oxj 增加分页的方法对外开放供调用-------------------------------------
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("setLimitValue"); //$NON-NLS-1$
method.setReturnType(new FullyQualifiedJavaType("Criteria")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getIntInstance(), "value1")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getIntInstance(), "value2")); //$NON-NLS-1$
method.addBodyLine("if (value2==0 )");
method.addBodyLine(" throw new RuntimeException(\"value2 is 0\");");
method.addBodyLine("addCriterion(\"limit\",value1,value2,\"null\");");
method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$
answer.addMethod(method);
//--------------------------------------------------
//--------------增加分页added by oxj 增加limitValue值的get方法,供配置的xml文件的OGNL表达式使用-------------------------------------
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("getLimitValue"); //$NON-NLS-1$
method.setReturnType(new FullyQualifiedJavaType("Criteria")); //$NON-NLS-1$
method.addBodyLine("return (Criteria)criteria;"); //$NON-NLS-1$
answer.addMethod(method);
//--------------------------------------------------
// now we need to generate the methods that will be used in the SqlMap
// to generate the dynamic where clause
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewListInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewArrayListInstance());
field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
FullyQualifiedJavaType listOfCriterion = new FullyQualifiedJavaType(
"java.util.List<Criterion>"); //$NON-NLS-1$
field.setType(listOfCriterion);
field.setName("criteria"); //$NON-NLS-1$
answer.addField(field);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(field.getType());
method.setName(getGetterMethodName(field.getName(), field
.getType()));
method.addBodyLine("return criteria;"); //$NON-NLS-1$
answer.addMethod(method);
// now add the methods for simplifying the individual field set methods
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterion"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addBodyLine("if (condition == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value for condition cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method.addBodyLine("criteria.add(new Criterion(condition));"); //$NON-NLS-1$
if (criteriaLists.size() > 1) {
method.addBodyLine("allCriteria = null;"); //$NON-NLS-1$
}
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterion"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
if (criteriaLists.size() > 1) {
method.addBodyLine("allCriteria = null;"); //$NON-NLS-1$
}
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterion"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value1")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value2")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("criteria.add(new Criterion(condition, value1, value2));"); //$NON-NLS-1$
if (criteriaLists.size() > 1) {
method.addBodyLine("allCriteria = null;"); //$NON-NLS-1$
}
answer.addMethod(method);
FullyQualifiedJavaType listOfDates = new FullyQualifiedJavaType(
"java.util.List<java.util.Date>"); //$NON-NLS-1$
if (introspectedTable.hasJDBCDateColumns()) {
topLevelClass.addImportedType(FullyQualifiedJavaType
.getDateInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewIteratorInstance());
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("addCriterion(condition, new java.sql.Date(value.getTime()), property);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(listOfDates, "values")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (values == null || values.size() == 0) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();"); //$NON-NLS-1$
method.addBodyLine("Iterator<Date> iter = values.iterator();"); //$NON-NLS-1$
method.addBodyLine("while (iter.hasNext()) {"); //$NON-NLS-1$
method
.addBodyLine("dateList.add(new java.sql.Date(iter.next().getTime()));"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method.addBodyLine("addCriterion(condition, dateList, property);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value1")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value2")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);"); //$NON-NLS-1$
answer.addMethod(method);
}
修改完了,然后只要运行之前写好的MBG工具即可看到生成的代码。
这里放上我写好的附件,
MBG.rar是修改后的源码,
MBGTools是我用java代码写好的 MBG 工具 。