MapperConfigPlugin.java

/*
 *    Copyright 2006-2026 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.generator.plugins;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
import static org.mybatis.generator.internal.util.messages.Messages.getString;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.OutputUtilities;
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 org.mybatis.generator.codegen.XmlConstants;

/**
 * This plugin generates a MapperConfig file containing mapper entries for SQL
 * maps generated for MyBatis3. This demonstrates hooking into the code
 * generation lifecycle and generating additional XML files.
 *
 * <p>This plugin accepts three properties:
 *
 * <ul>
 * <li><code>fileName</code> (optional) the name of the generated file. this
 * defaults to "SqlMapConfig.xml" if not specified.</li>
 * <li><code>targetPackage</code> (required) the name of the package where the file
 * should be placed. Specified like "com.mycompany.sql".</li>
 * <li><code>targetProject</code> (required) the name of the project where the file
 * should be placed.</li>
 * </ul>
 *
 * <p>Note: targetPackage and targetProject follow the same rules as the
 * targetPackage and targetProject values on the sqlMapGenerator configuration
 * element.
 *
 * @author Jeff Butler
 */
public class MapperConfigPlugin extends PluginAdapter {

    private final List<String> mapperFiles = new ArrayList<>();

    @Override
    public boolean validate(List<String> warnings) {
        boolean valid = true;

        if (!stringHasValue(properties.getProperty("targetProject"))) { //$NON-NLS-1$
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                    "MapperConfigPlugin", //$NON-NLS-1$
                    "targetProject")); //$NON-NLS-1$
            valid = false;
        }

        if (!stringHasValue(properties.getProperty("targetPackage"))) { //$NON-NLS-1$
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                    "MapperConfigPlugin", //$NON-NLS-1$
                    "targetPackage")); //$NON-NLS-1$
            valid = false;
        }

        return valid;
    }

    @Override
    public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles() {
        if (mapperFiles.isEmpty()) {
            return Collections.emptyList();
        }

        XmlElement root = new XmlElement("configuration"); //$NON-NLS-1$

        root.addElement(new TextElement("<!--")); //$NON-NLS-1$
        root.addElement(new TextElement(OutputUtilities.xmlIndent(1)
                + "This file is generated by MyBatis Generator.")); //$NON-NLS-1$
        root.addElement(new TextElement(OutputUtilities.xmlIndent(1)
                + "This file is the shell of a Mapper Config file - in many cases you will need to add")); //$NON-NLS-1$
        root.addElement(new TextElement(OutputUtilities.xmlIndent(2)
                + "to this file before it is usable by MyBatis.")); //$NON-NLS-1$
        root.addElement(new TextElement(OutputUtilities.xmlIndent(1)
                + "This file was generated on " + new Date() + '.')); //$NON-NLS-1$

        root.addElement(new TextElement("-->")); //$NON-NLS-1$

        XmlElement mappers = new XmlElement("mappers"); //$NON-NLS-1$
        root.addElement(mappers);

        XmlElement mapper;
        for (String mapperFile : mapperFiles) {
            mapper = new XmlElement("mapper"); //$NON-NLS-1$
            mapper.addAttribute(new Attribute("resource", mapperFile)); //$NON-NLS-1$
            mappers.addElement(mapper);
        }

        Document document = new Document(XmlConstants.MYBATIS3_MAPPER_CONFIG_PUBLIC_ID,
                XmlConstants.MYBATIS3_MAPPER_CONFIG_SYSTEM_ID, root);

        GeneratedXmlFile gxf = new GeneratedXmlFile(document, properties
                .getProperty("fileName", "MapperConfig.xml"), //$NON-NLS-1$ //$NON-NLS-2$
                properties.getProperty("targetPackage"), //$NON-NLS-1$
                properties.getProperty("targetProject"), //$NON-NLS-1$
                false);

        List<GeneratedXmlFile> answer = new ArrayList<>(1);
        answer.add(gxf);

        return answer;
    }

    /*
     * This method collects the name of every SqlMap file generated in
     * this context.
     */
    @Override
    public boolean sqlMapGenerated(GeneratedXmlFile sqlMap,
            IntrospectedTable introspectedTable) {
        StringBuilder sb = new StringBuilder();
        sb.append(sqlMap.getTargetPackage());
        sb.append('.');
        String temp = sb.toString();
        sb.setLength(0);
        sb.append(temp.replace('.', '/'));
        sb.append(sqlMap.getFileName());
        mapperFiles.add(sb.toString());

        return true;
    }
}