`
随影清风
  • 浏览: 39417 次
  • 性别: Icon_minigender_1
  • 来自: 福州
文章分类
社区版块
存档分类
最新评论

修改mybatis的MBG工具源码,支持mysql分页

阅读更多
   最近要做一个项目(私人的,呵呵),因为新公司的新项目是用 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 工具 。
     
    
  • MBG.rar (347.7 KB)
  • 下载次数: 223
分享到:
评论
6 楼 jiny_tianxia 2013-02-18  
mbg生成的xml是可以扩展的,可以选择使用mbg生成的查询sql,也可以自定义sql
至于想让mbg做到表关联的sql生成,那不是相当于重复了hibernate的工作了,那还不如直接用hibernate的了。
5 楼 flying3615 2011-10-31  
引用
foreach2Element.addElement(if2Element);
       
        if2Element.addElement(new TextElement("limit ${criterion.value},${criterion.secondValue}"));
        answer.addElement(ifElement);
        /**  以上创建的分页节点结构如下
         *   <foreach collection="oredCriteria" item="criteria">


改成
answer.addElement(foreachElement); 否则生成的xml格式不对
4 楼 smallbee 2011-08-22  
类:ExampleGenerator
需要修改此处
method
                .addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
        method.addBodyLine("}"); //$NON-NLS-1$
        method.addBodyLine("criteria.add(new Criterion(condition,value));"); //$NON-NLS-1$
        if (criteriaLists.size() > 1) {
            method.addBodyLine("allCriteria = null;"); //$NON-NLS-1$
        }
3 楼 liu2511981 2011-03-15  
楼主,你好

  请问:能让MBG 支持关联关系映射吗?将外键表映射成自己的一个属性,当取这个属性时,直接在发起一条SQL 去拿到这个属性对象,就像Hibernate中一样。

  可以吗?如何做呢?
2 楼 随影清风 2011-02-12  
不知道。我没有碰到过你这个问题。。不好意思。。
1 楼 wangyun71 2011-02-06  
请问 MyBatis 的中文乱码你是怎么解决的
我现在的情况是
action是中文
log出来的内容如下:
2011-02-06 21:14:06 [java.sql.PreparedStatement]-[DEBUG] ==>  Executing: INSERT INTO SYSUSER VALUES (?,?,?,?)
2011-02-06 21:14:06 [java.sql.PreparedStatement]-[DEBUG] ==> Parameters: null, 阿斯顿(String), 阿斯顿(String), 阿斯顿(String)
但是到了数据库里是乱码
我的数据库连接、过滤器、数据库的字符集设置都是utf-8
但就是写进去的是乱码
不知道是怎么回事
能否说一下你的处理方式?
谢谢

相关推荐

    mybatis不改源码实现物理分页

    标题中的“mybatis不改源码实现物理分页”指的是在使用MyBatis这个流行的Java持久层框架时,如何在不修改MyBatis核心源码的情况下实现数据库的物理分页功能。物理分页是直接在数据库层面进行分页查询,通常更高效,...

    mybatis支持MYSQL分页

    mybatis支持MYSQL分页 暂只支持MYSQL且不适用与MYBATIS-SPRING一起使用 调用方法 List selectList(String statement, int start ,int end) 与 List selectList(String statement, Object parameter, int start ,int ...

    mybatis分页插件源码

    MyBatis 分页插件是数据库操作中常用的一个工具,它极大地简化了在MyBatis框架下实现分页查询的工作。这个源码分享主要针对MySQL和Oracle两大主流数据库,为开发者提供了高效且易于使用的分页解决方案。下面我们将...

    mybatis逆向工具源码

    可视化mybatis逆向工具源码,支持MySQL/Oracle/PostgreSQL/SQLserver数据库,支持分页和JPA注解等拓展功能; 使用方法:直接在IDE中运行com.mybatis.generator下的MainUI类即可启动;

    MyBatis 分页源码简单

    MyBatis 是一款著名的 Java 持久层框架,它简化了数据库操作,提供了动态 SQL 功能,并且在处理分页查询时也有着强大的支持。本文将深入探讨 MyBatis 的分页源码,理解其背后的实现机制。 首先,我们要明白 MyBatis...

    06实现mybatis分页插件demo

    06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo06实现mybatis分页插件demo...

    基于mysql的数据库mybatis 分页插件

    mybatis 分页 mybatis-generate Mysql数据库 大家知道mybatis自动生成代码是没有分页功能的 我在网上找了很久 有很多内容 但正真可以使用的少之又少 本人整合了网上的资源 整理了基于Mysql数据库的mybatis插件 经...

    ajax+jso+mybatis+mysql模仿百度简单分页案例

    在本案例中,我们主要探讨如何使用Ajax、JSON、MyBatis和MySQL来实现一个类似于百度的简单分页功能。这个教程特别适合那些希望提升自己技能的程序员,通过阅读源代码和注释,你可以深入理解这些技术的结合使用。下面...

    springboot整合mybatis-plus实现多表分页查询

    在本文中,我们将深入探讨如何在SpringBoot项目中整合MyBatis-Plus,实现多表分页查询。MyBatis-Plus是一个强大的MyBatis扩展,简化了对数据库的操作,包括CRUD操作以及复杂的关联查询。它提供了丰富的API,使得开发...

    基于Springboot+Mybatis+Redis+MySql+MQ的校园医疗管理系统源码+数据库.zip

    基于Springboot+Mybatis+Redis+MySql+MQ的校园医疗管理系统源码+数据库.zip 基于Springboot+Mybatis+Redis+MySql+MQ的校园医疗管理系统源码+数据库.zip 基于Springboot+Mybatis+Redis+MySql+MQ的校园医疗管理系统...

    mybatis-plus源码(mybatis-plus-3.5.1.zip)

    3. **自动化填充字段**:支持实体类字段的自动填充,如创建时间、修改时间和乐观锁版本号等,只需要在实体类字段上添加注解即可。 4. **批量插入与更新**:MyBatis-Plus支持一次性插入或更新多条数据,提高了数据...

    mybatis-plus源码

    在深入探讨MyBatis-Plus源码之前,我们先了解一下MyBatis-Plus的基本概念和作用。 MyBatis-Plus是对MyBatis框架的一种增强,它在MyBatis的基础上进行了功能扩展,比如自动填充字段、条件构造器、一键生成代码等,极...

    Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)SSM源码.zip

    Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)SSM源码Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)SSM源码Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)SSM源码Java EE企业级应用...

    mybatis完整工具包(含分页、sql自动生成、cache改造等)完全免费

    之前上传了两个mybatis的插件(分页、sql自动生成),看见大家这么喜欢,本想免费让大家下载,当上传的资源却没法修改。干脆就重新上传。这次上传的资源不但包含之前的代码,还修正了之前的一些问题(详见:...

    mybatis核心源码

    mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码mybatis核心源码...

    修改mybatis-generator源码实现实体类字段上方加上数据库中填写的注释

    标题 "修改mybatis-generator源码实现实体类字段上方加上数据库中填写的注释" 描述了一种增强MyBatis Generator(MBG)功能的方法,目的是在生成的实体类字段上添加对应数据库字段的注释。这能提高代码的可读性和...

Global site tag (gtag.js) - Google Analytics