利用Mytatis Generator插件生成Model的列表查詢方法

直接上代碼吧java

public class GenerateListPlugin extends PluginAdapter {
    private FullyQualifiedJavaType primaryKeyType;
    private FullyQualifiedJavaType modelType;

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        primaryKeyType = method.getParameters().get(0).getType();
        modelType = method.getReturnType();
        return super.clientSelectByPrimaryKeyMethodGenerated(method, interfaze, introspectedTable);
    }

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        //batch insert
        {
            Method method = new Method();
            method.setName("batchInsert");
            method.setReturnType(new FullyQualifiedJavaType("int"));
            method.addParameter(new Parameter(new FullyQualifiedJavaType("java.util.List<" + modelType.getShortName() + ">"), "list"));
            interfaze.addMethod(method);
            interfaze.addImportedType(new FullyQualifiedJavaType("java.util.List"));
        }
        //list all
        {
            Method method = new Method();
            method.setName("listAll");
            method.setReturnType(new FullyQualifiedJavaType("java.util.List<" + modelType.getShortName() + ">"));
            interfaze.addMethod(method);
            interfaze.addImportedType(new FullyQualifiedJavaType("java.util.List"));
        }

        //list by primary key
        List<IntrospectedColumn> primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();
        if(primaryKeyColumns != null && primaryKeyColumns.size() == 1) {
            Method listMethod = new Method();
            listMethod.setName("listByPrimaryKey");
            listMethod.setReturnType(new FullyQualifiedJavaType("java.util.List<"+modelType.getShortName()+">"));

            Parameter parameter = new Parameter(new FullyQualifiedJavaType("java.util.List<"+primaryKeyType.getShortName()+">"), "list");
            listMethod.addParameter(parameter);
            interfaze.addMethod(listMethod);
            interfaze.addImportedType(new FullyQualifiedJavaType("java.util.List"));
        }

        return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        XmlElement root = document.getRootElement();
        //batch insert
        {
            //batch insert
            XmlElement batchInsert = new XmlElement("insert");
            batchInsert.addAttribute(new Attribute("id", "batchInsert"));
            batchInsert.addAttribute(new Attribute("parameterType", "list"));
            StringBuilder cols = new StringBuilder();
            StringBuilder vals = new StringBuilder();
            List<IntrospectedColumn> baseColumns = introspectedTable.getBaseColumns();
            boolean isFirst = true;
            for(IntrospectedColumn column : baseColumns) {
                if(!isFirst) {
                    cols.append(',');
                    vals.append(',');
                } else {
                    isFirst = false;
                }
                cols.append(column.getActualColumnName());
                //#{id,jdbcType=BIGINT}
                vals.append("#{item.").append(column.getJavaProperty()).append(",jdbcType=").append(column.getJdbcTypeName()).append('}');

            }
            StringBuilder insertBuilder = new StringBuilder();
            insertBuilder.append("insert into").append(introspectedTable.getFullyQualifiedTable().getIntrospectedTableName())
                    .append("(").append(cols).append(") values ");
            TextElement insert = new TextElement(insertBuilder.toString());
            batchInsert.addElement(insert);
            XmlElement foreach = new XmlElement("foreach");
            foreach.addAttribute(new Attribute("collection", "list"));
            foreach.addAttribute(new Attribute("item", "item"));
            foreach.addAttribute(new Attribute("separator", ","));
            foreach.addElement(new TextElement("(" + vals.toString() + ")"));
            batchInsert.addElement(foreach);
            root.addElement(batchInsert);
        }

        //list all
        {
            XmlElement selectAll = new XmlElement("select");
            selectAll.addAttribute(new Attribute("id", "listAll"));
            selectAll.addAttribute(new Attribute("resultMap", "BaseResultMap"));

            TextElement selectSql = new TextElement("select");
            selectAll.addElement(selectSql);
            XmlElement include = new XmlElement("include");
            include.addAttribute(new Attribute("refid", "Base_Column_List"));
            selectAll.addElement(include);
            selectAll.addElement(new TextElement("from " + introspectedTable.getTableConfiguration().getTableName()));

            root.addElement(selectAll);
        }

        //list by primary key
        List<IntrospectedColumn> primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();
        if(primaryKeyColumns != null && primaryKeyColumns.size() == 1) {
            XmlElement listByPrimaryKey = new XmlElement("select");
            listByPrimaryKey.addAttribute(new Attribute("id", "listByPrimaryKey"));
            listByPrimaryKey.addAttribute(new Attribute("resultMap", "BaseResultMap"));


            TextElement selectEle = new TextElement("select");
            listByPrimaryKey.addElement(selectEle);
            XmlElement includeEle = new XmlElement("include");
            includeEle.addAttribute(new Attribute("refid", "Base_Column_List"));
            listByPrimaryKey.addElement(includeEle);
            listByPrimaryKey.addElement(new TextElement("from " + introspectedTable.getTableConfiguration().getTableName()));
            TextElement where = new TextElement("where " + primaryKeyColumns.get(0).getActualColumnName() + " in");
            listByPrimaryKey.addElement(where);
            XmlElement foreach = new XmlElement("foreach");
            foreach.addAttribute(new Attribute("collection", "list"));
            foreach.addAttribute(new Attribute("item", "item"));
            foreach.addAttribute(new Attribute("open", "("));
            foreach.addAttribute(new Attribute("separator", ","));
            foreach.addAttribute(new Attribute("close", ")"));
            foreach.addElement(new TextElement("#{item}"));
            listByPrimaryKey.addElement(foreach);
            root.addElement(listByPrimaryKey);
        }

        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }
}

生成batchInsert、listByPrimaryKey、listAll方法sql

listByPrimaryKey方法不支持複合主鍵。app

相關文章
相關標籤/搜索