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

mybatis / generator / 1646

21 Apr 2025 10:17PM UTC coverage: 88.157% (-0.2%) from 88.328%
1646

push

github

hazendaz
[ci] Run auto formatting

2518 of 3412 branches covered (73.8%)

994 of 1117 new or added lines in 164 files covered. (88.99%)

23 existing lines in 12 files now uncovered.

10578 of 11999 relevant lines covered (88.16%)

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

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

57
    @Override
58
    public boolean validate(List<String> warnings) {
59
        boolean valid = true;
×
60

NEW
61
        if (!stringHasValue(properties.getProperty("targetProject"))) { //$NON-NLS-1$
×
62
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
×
63
                    "MapperConfigPlugin", //$NON-NLS-1$
64
                    "targetProject")); //$NON-NLS-1$
65
            valid = false;
×
66
        }
67

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

75
        return valid;
×
76
    }
77

78
    @Override
79
    public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles() {
NEW
80
        Document document = new Document(XmlConstants.MYBATIS3_MAPPER_CONFIG_PUBLIC_ID,
×
81
                XmlConstants.MYBATIS3_MAPPER_CONFIG_SYSTEM_ID);
82

83
        if (mapperFiles.isEmpty()) {
×
84
            return Collections.emptyList();
×
85
        }
86

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

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

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

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

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

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

NEW
111
        GeneratedXmlFile gxf = new GeneratedXmlFile(document, properties.getProperty("fileName", "MapperConfig.xml"), //$NON-NLS-1$ //$NON-NLS-2$
×
112
                properties.getProperty("targetPackage"), //$NON-NLS-1$
×
113
                properties.getProperty("targetProject"), //$NON-NLS-1$
×
114
                false, context.getXmlFormatter());
×
115

116
        List<GeneratedXmlFile> answer = new ArrayList<>(1);
×
117
        answer.add(gxf);
×
118

119
        return answer;
×
120
    }
121

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

136
        return true;
×
137
    }
138
}
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

© 2025 Coveralls, Inc