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

mybatis / generator / 2202

05 May 2026 07:27PM UTC coverage: 91.746% (+0.04%) from 91.703%
2202

push

github

web-flow
Merge pull request #1508 from mybatis/renovate/com.github.javaparser-javaparser-core-3.x

Update dependency com.github.javaparser:javaparser-core to v3.28.1

2454 of 3154 branches covered (77.81%)

12038 of 13121 relevant lines covered (91.75%)

0.92 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.getProperty("targetProject"))) { //$NON-NLS-1$
×
66
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
×
67
                    "MapperConfigPlugin", //$NON-NLS-1$
68
                    "targetProject")); //$NON-NLS-1$
69
            valid = false;
×
70
        }
71

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

79
        return valid;
×
80
    }
81

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

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

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

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

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

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

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

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

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

124
        return answer;
×
125
    }
126

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

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