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

mybatis / generator / 1941

12 Jan 2026 04:39PM UTC coverage: 88.75% (+0.4%) from 88.365%
1941

Pull #1412

github

web-flow
Merge b9b8be318 into 98a4124a3
Pull Request #1412: Adopt JSpecify

2331 of 3162 branches covered (73.72%)

1800 of 1949 new or added lines in 202 files covered. (92.36%)

18 existing lines in 10 files now uncovered.

11384 of 12827 relevant lines covered (88.75%)

0.89 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.xml.Attribute;
30
import org.mybatis.generator.api.dom.xml.Document;
31
import org.mybatis.generator.api.dom.xml.TextElement;
32
import org.mybatis.generator.api.dom.xml.XmlElement;
33
import org.mybatis.generator.codegen.XmlConstants;
34

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

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

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

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

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

81
        return valid;
×
82
    }
83

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

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

UNCOV
92
        root.addElement(new TextElement("<!--")); //$NON-NLS-1$
×
93
        root.addElement(new TextElement("  This file is generated by MyBatis Generator.")); //$NON-NLS-1$
×
94
        root.addElement(new TextElement(
×
95
                "  This file is the shell of a Mapper Config file - in many cases you will need to add")); //$NON-NLS-1$
96
        root.addElement(new TextElement("    to this file before it is usable by MyBatis.")); //$NON-NLS-1$
×
97

98
        String s = "  This file was generated on " + new Date() + '.'; //$NON-NLS-1$
×
99
        root.addElement(new TextElement(s));
×
100

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

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

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

NEW
113
        Document document = new Document(XmlConstants.MYBATIS3_MAPPER_CONFIG_PUBLIC_ID,
×
114
                XmlConstants.MYBATIS3_MAPPER_CONFIG_SYSTEM_ID, root);
115

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

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

125
        return answer;
×
126
    }
127

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

144
        return true;
×
145
    }
146
}
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