All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.revengemission.plugins.mybatis.MybatisSelectByPrimaryKeyAndStorePlugin Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package com.revengemission.plugins.mybatis;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

/**
 * 双键查询
 */
public class MybatisSelectByPrimaryKeyAndStorePlugin extends AbstractXmbgPlugin {

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

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        Method method = new Method("selectByPrimaryKeyAndStore");
        method.addParameter(new Parameter(new FullyQualifiedJavaType("long"), "storeId", "@Param(\"storeId\")"));
        method.addParameter(new Parameter(new FullyQualifiedJavaType("long"), "id", "@Param(\"id\")"));
        method.setReturnType(new FullyQualifiedJavaType(getEntityName(introspectedTable)));
        interfaze.addMethod(method);
        return true;
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();

        XmlElement parentElement = document.getRootElement();

        XmlElement deleteLogicByIdsElement = new XmlElement("select");
        deleteLogicByIdsElement.addAttribute(new Attribute("id", "selectByPrimaryKeyAndStore"));

        if (introspectedTable.getBLOBColumns().size() > 0) {
            deleteLogicByIdsElement.addAttribute(new Attribute("resultMap", "ResultMapWithBLOBs"));
            deleteLogicByIdsElement.addElement(
                    new TextElement(
                            "select \n" +
                                    "    ,\n" +
                                    "    \n\t from " + tableName + "\n\t  where  store_id=#{storeId} and id = #{id}"
                    ));
        } else {
            deleteLogicByIdsElement.addAttribute(new Attribute("resultMap", "BaseResultMap"));
            deleteLogicByIdsElement.addElement(
                    new TextElement(
                            "select \n\t from " + tableName + "\n\t  where store_id=#{storeId} and id = #{id}"
                    ));
        }

        parentElement.addElement(deleteLogicByIdsElement);

        return super.sqlMapDocumentGenerated(document, introspectedTable);

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy