net.sourceforge.jweb.mybatis.generator.plugins.UserDefinedXMLMapperPlugin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jweb-maven-plugin Show documentation
Show all versions of jweb-maven-plugin Show documentation
maven plugins and mybatis generator plugins
package net.sourceforge.jweb.mybatis.generator.plugins;
/*
* Copyright 2002-2016 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
*
* http://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.
*/
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import net.sourceforge.jweb.maven.util.XMLUtil;
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.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.Element;
import org.mybatis.generator.logging.Log;
import org.mybatis.generator.logging.LogFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* In my experience, The generated code can not meet requirement perfectly, so you need to insert your own code in generated
* code. in RAD, the database(Domain) may change during development frequently, so when regenerating code, you must be very careful to maintain
* your piece of code in pojo and mapper xml. so UserDefinedXMLMapperPlugin can hold your part of babatis code and xml.
*
* Usage:
*
* <plugin type="net.sourceforge.jweb.mybatis.generator.plugins.UserDefinedXMLMapperPlugin">
* <property name="baseDirectory" value="base directory"/>
* </plugin>
*
*
* If baseDirectory is not set, plugin will use current, i.e. new File("").getAbsolutePath();
* if set and start with /, then plugin will consider you are using absolute path, in Windows, please use absolute path starts with /,
* for example, /C:\\a\\b.
* if set and not start with /, then plugin will consider you are using relative path, i.e. new File("").getAbsolutePath()+ baseDirectory
*
* In your generatorConfig.xml table node, add:
*
* <table ...>
* <!-- your mapper file, you can use any tag name as root -->
* <property name="UserDefinedXMLMapper" value="file name"/>
*
* <!-- interface will insert to Mapper interface -->
* <property name="xxxInterface" value="public Domain selectByProductId(int productId);"/>
*
* <!-- Implemention will insert to Mapper class if generating IMPL -->
* <property name="xxxImplement" value="public Domain selectByProductId(int productId){return sqlClient.queryForList(...', '...');}"/>
* </table>
*
* Your defined xml mapper can use any tag name as root, NOTE, no xml validation.
*
* @author [email protected]
*
*/
public class UserDefinedXMLMapperPlugin extends PluginAdapter {
private final static Log LOG=LogFactory.getLog(UserDefinedXMLMapperPlugin.class);
private String baseDirectory;
public boolean validate(List warnings) {
Properties properties=this.getProperties();
if(properties==null){
baseDirectory=new File("").getAbsolutePath();
LOG.warn("[UserDefinedXMLMapperPlugin] You did not set baseDirectory, so use current directory default: "+baseDirectory);
}
else {
String value=properties.getProperty("baseDirectory");
if(value==null){
baseDirectory=new File("").getAbsolutePath();
LOG.warn("[UserDefinedXMLMapperPlugin] You did not set baseDirectory, so use current directory default: "+baseDirectory);
}
else {
if(value.startsWith("/")){
File f = new File(value);
if(f.exists()){
baseDirectory=f.getAbsolutePath();
LOG.debug("[UserDefinedXMLMapperPlugin] You set baseDirectory, "+baseDirectory);
}
else {
LOG.warn("[UserDefinedXMLMapperPlugin] You set baseDirectory, but it is not exist, validation failed on: "+baseDirectory);
return false;
}
}
else {
String currentBase=new File("").getAbsolutePath();
if(!currentBase.endsWith("/")) currentBase=currentBase+"/";
baseDirectory=currentBase+value;
LOG.debug("[UserDefinedXMLMapperPlugin] Your real baseDirectory, "+baseDirectory);
}
}
}
if(!baseDirectory.endsWith("/")) baseDirectory=baseDirectory+"/";
return true;
}
@SuppressWarnings("rawtypes")
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
Properties properties=introspectedTable.getTableConfiguration().getProperties();
if(properties==null) return true;
for(Entry entry:properties.entrySet()){
String key=entry.getKey().toString();
if(key.endsWith("Interface")){
if(interfaze!=null)interfaze.addMethod(new LiterialMethod(introspectedTable.getTableConfiguration().getTableName(), entry.getValue().toString()));
}
else if(key.endsWith("Import")){
if(interfaze!=null)interfaze.addImportedType(new FullyQualifiedJavaType(entry.getValue().toString()));
if(topLevelClass!=null)topLevelClass.addImportedType(new FullyQualifiedJavaType(entry.getValue().toString()));
}
else if(key.endsWith("Implement")){
if(topLevelClass!=null){
topLevelClass.addMethod(new LiterialMethod(introspectedTable.getTableConfiguration().getTableName(), entry.getValue().toString()));
}
}
}
return true;
}
@Override
public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
Properties properties=introspectedTable.getTableConfiguration().getProperties();
if(properties==null) return true;
String mapper=properties.getProperty("UserDefinedXMLMapper");
if(mapper==null) return true;
File mapperFile=new File(baseDirectory+mapper);
if(!mapperFile.exists()){
LOG.warn(mapper+" not exists, ignored");
return true;
}
Document generatorDom=getDocumentInstance(sqlMap);
try {
org.w3c.dom.Document doc=XMLUtil.createDocument(mapperFile);
NodeList nodeList = doc.getDocumentElement().getChildNodes();
if(nodeList!=null){
for(int i=0;i