• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / generator / 2095

13 Mar 2026 07:14PM UTC coverage: 90.125% (+0.07%) from 90.056%
2095

push

github

web-flow
Merge pull request #1465 from mybatis/renovate/maven-3.x

Update dependency maven to v3.9.14

2362 of 3125 branches covered (75.58%)

11691 of 12972 relevant lines covered (90.12%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/plugins/MapperConfigPlugin.java
1
/*
2
 *    Copyright 2006-2026 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.generator.plugins;
17

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

21
import java.util.ArrayList;
22
import java.util.Collections;
23
import java.util.Date;
24
import java.util.List;
25

26
import org.mybatis.generator.api.GeneratedXmlFile;
27
import org.mybatis.generator.api.IntrospectedTable;
28
import org.mybatis.generator.api.PluginAdapter;
29
import org.mybatis.generator.api.dom.OutputUtilities;
30
import org.mybatis.generator.api.dom.xml.Attribute;
31
import org.mybatis.generator.api.dom.xml.Document;
32
import org.mybatis.generator.api.dom.xml.TextElement;
33
import org.mybatis.generator.api.dom.xml.XmlElement;
34
import org.mybatis.generator.codegen.XmlConstants;
35

36
/**
37
 * This plugin generates a MapperConfig file containing mapper entries for SQL
38
 * maps generated for MyBatis3. This demonstrates hooking into the code
39
 * generation lifecycle and generating additional XML files.
40
 *
41
 * <p>This plugin accepts three properties:
42
 *
43
 * <ul>
44
 * <li><code>fileName</code> (optional) the name of the generated file. this
45
 * defaults to "SqlMapConfig.xml" if not specified.</li>
46
 * <li><code>targetPackage</code> (required) the name of the package where the file
47
 * should be placed. Specified like "com.mycompany.sql".</li>
48
 * <li><code>targetProject</code> (required) the name of the project where the file
49
 * should be placed.</li>
50
 * </ul>
51
 *
52
 * <p>Note: targetPackage and targetProject follow the same rules as the
53
 * targetPackage and targetProject values on the sqlMapGenerator configuration
54
 * element.
55
 *
56
 * @author Jeff Butler
57
 */
58
public class MapperConfigPlugin extends PluginAdapter {
×
59

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

62
    @Override
63
    public boolean validate(List<String> warnings) {
64
        boolean valid = true;
×
65

66
        if (!stringHasValue(properties
×
67
                .getProperty("targetProject"))) { //$NON-NLS-1$
×
68
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
×
69
                    "MapperConfigPlugin", //$NON-NLS-1$
70
                    "targetProject")); //$NON-NLS-1$
71
            valid = false;
×
72
        }
73

74
        if (!stringHasValue(properties
×
75
                .getProperty("targetPackage"))) { //$NON-NLS-1$
×
76
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
×
77
                    "MapperConfigPlugin", //$NON-NLS-1$
78
                    "targetPackage")); //$NON-NLS-1$
79
            valid = false;
×
80
        }
81

82
        return valid;
×
83
    }
84

85
    @Override
86
    public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles() {
87
        if (mapperFiles.isEmpty()) {
×
88
            return Collections.emptyList();
×
89
        }
90

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

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

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

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

108
        XmlElement mapper;
109
        for (String mapperFile : mapperFiles) {
×
110
            mapper = new XmlElement("mapper"); //$NON-NLS-1$
×
111
            mapper.addAttribute(new Attribute("resource", mapperFile)); //$NON-NLS-1$
×
112
            mappers.addElement(mapper);
×
113
        }
×
114

115
        Document document = new Document(XmlConstants.MYBATIS3_MAPPER_CONFIG_PUBLIC_ID,
×
116
                XmlConstants.MYBATIS3_MAPPER_CONFIG_SYSTEM_ID, root);
117

118
        GeneratedXmlFile gxf = new GeneratedXmlFile(document, properties
×
119
                .getProperty("fileName", "MapperConfig.xml"), //$NON-NLS-1$ //$NON-NLS-2$
×
120
                properties.getProperty("targetPackage"), //$NON-NLS-1$
×
121
                properties.getProperty("targetProject"), //$NON-NLS-1$
×
122
                false);
123

124
        List<GeneratedXmlFile> answer = new ArrayList<>(1);
×
125
        answer.add(gxf);
×
126

127
        return answer;
×
128
    }
129

130
    /*
131
     * This method collects the name of every SqlMap file generated in
132
     * this context.
133
     */
134
    @Override
135
    public boolean sqlMapGenerated(GeneratedXmlFile sqlMap,
136
            IntrospectedTable introspectedTable) {
137
        StringBuilder sb = new StringBuilder();
×
138
        sb.append(sqlMap.getTargetPackage());
×
139
        sb.append('.');
×
140
        String temp = sb.toString();
×
141
        sb.setLength(0);
×
142
        sb.append(temp.replace('.', '/'));
×
143
        sb.append(sqlMap.getFileName());
×
144
        mapperFiles.add(sb.toString());
×
145

146
        return true;
×
147
    }
148
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc