Search in sources :

Example 31 with TopLevelClass

use of org.mybatis.generator.api.dom.java.TopLevelClass in project PublicCMS-preview by sanluan.

the class ServicePlugin method clientGenerated.

public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
    String packageName = interfaze.getType().getPackageName();
    String shortName = interfaze.getType().getShortName();
    String mapperName = interfaze.getType().getFullyQualifiedName();
    if (shortName.endsWith("Mapper")) {
        int idx = packageName.lastIndexOf(".");
        if (idx < 0) {
            idx = packageName.length();
        }
        String servicePackage = packageName.substring(0, idx);
        String serviceName = shortName.substring(0, shortName.length() - 6);
        serviceName += "Service";
        String fullName = servicePackage + ".service." + serviceName;
        TopLevelClass topLevelClazz = new TopLevelClass(fullName);
        topLevelClazz.setVisibility(JavaVisibility.PUBLIC);
        topLevelClazz.addImportedType(mapperName);
        topLevelClazz.addImportedType("org.springframework.stereotype.Service");
        {
            // 增加备注信息
            topLevelClazz.addFileCommentLine("/*");
            topLevelClazz.addFileCommentLine("    this file generator by ServicePlugin");
            topLevelClazz.addFileCommentLine("    @Author: xinlu");
            topLevelClazz.addFileCommentLine("    @Email: 442559691@qq.com");
            topLevelClazz.addFileCommentLine("*/");
            topLevelClazz.addFileCommentLine("");
        }
        topLevelClazz.addAnnotation("@Service");
        topLevelClazz.addAnnotation("@Transactional");
        topLevelClazz.addImportedType("org.springframework.transaction.annotation.Transactional");
        {
            Field field = new Field();
            field.setVisibility(JavaVisibility.PRIVATE);
            field.setType(new FullyQualifiedJavaType(mapperName));
            field.setName("mapper");
            field.addAnnotation("@Autowired");
            topLevelClazz.addImportedType("org.springframework.beans.factory.annotation.Autowired");
            topLevelClazz.addField(field);
        }
        List<Method> methods = interfaze.getMethods();
        for (Method it : methods) {
            Method m = new Method();
            m.setVisibility(JavaVisibility.PUBLIC);
            if (it.getName().endsWith("WithRowbounds")) {
                m.setName(it.getName().replace("WithRowbounds", "WithPage"));
                FullyQualifiedJavaType pageHandler = new FullyQualifiedJavaType("com.publiccms.common.handler.PageHandler");
                m.setReturnType(pageHandler);
                topLevelClazz.addImportedType(pageHandler);
                m.addBodyLine("PageHandler page = new PageHandler(pageIndex, pageSize, mapper.countByExample(example), null);");
                StringBuilder bodyline = new StringBuilder("");
                bodyline.append("page.setList(mapper.");
                bodyline.append(it.getName());
                bodyline.append("(");
                List<Parameter> params = it.getParameters();
                List<String> paramTxt = new ArrayList<>();
                for (Parameter p : params) {
                    FullyQualifiedJavaType t = p.getType();
                    topLevelClazz.addImportedType(t);
                    if ("RowBounds".equals(t.getShortNameWithoutTypeArguments())) {
                        m.addParameter(new Parameter(new FullyQualifiedJavaType("long"), "pageIndex"));
                        m.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "pageSize"));
                        paramTxt.add("new RowBounds((pageIndex - 1) * pageSize, pageSize)");
                    } else {
                        Parameter tmp = new Parameter(t, p.getName());
                        m.addParameter(tmp);
                        paramTxt.add(tmp.getName());
                    }
                }
                bodyline.append(StringUtils.join(paramTxt, ", "));
                bodyline.append("));");
                m.addBodyLine(bodyline.toString());
                m.addBodyLine("return page;");
            } else {
                m.setName(it.getName());
                m.setReturnType(it.getReturnType());
                topLevelClazz.addImportedType(it.getReturnType());
                String bodyline = "return mapper.";
                bodyline += m.getName();
                bodyline += "(";
                List<Parameter> params = it.getParameters();
                List<String> paramTxt = new ArrayList<>();
                for (Parameter p : params) {
                    FullyQualifiedJavaType t = p.getType();
                    topLevelClazz.addImportedType(t);
                    Parameter tmp = new Parameter(t, p.getName());
                    m.addParameter(tmp);
                    paramTxt.add(tmp.getName());
                }
                bodyline += StringUtils.join(paramTxt, ",");
                bodyline += ");";
                m.addBodyLine(bodyline);
            }
            topLevelClazz.addMethod(m);
        }
        services.add(topLevelClazz);
    }
    return true;
}
Also used : Field(org.mybatis.generator.api.dom.java.Field) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) TopLevelClass(org.mybatis.generator.api.dom.java.TopLevelClass) ArrayList(java.util.ArrayList) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Aggregations

TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)31 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)22 Method (org.mybatis.generator.api.dom.java.Method)19 ArrayList (java.util.ArrayList)17 Field (org.mybatis.generator.api.dom.java.Field)17 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)15 CommentGenerator (org.mybatis.generator.api.CommentGenerator)12 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)11 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)9 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)9 Plugin (org.mybatis.generator.api.Plugin)8 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 Parameter (org.mybatis.generator.api.dom.java.Parameter)6 Interface (org.mybatis.generator.api.dom.java.Interface)3 DefaultJavaFormatter (org.mybatis.generator.api.dom.DefaultJavaFormatter)2 Ordering (com.google.common.collect.Ordering)1 GeneratedJavaFile (org.mybatis.generator.api.GeneratedJavaFile)1 InnerClass (org.mybatis.generator.api.dom.java.InnerClass)1 Rules (org.mybatis.generator.internal.rules.Rules)1