Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 53 additions & 23 deletions src/main/java/com/baomidou/GenerateMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.baomidou.config.ConstVal;
import com.baomidou.config.builder.ConfigBuilder;
import com.baomidou.config.po.TableInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
Expand All @@ -17,6 +18,7 @@

/**
* 生成文件
*
* @author YangHu
* @since 2016/8/30
*/
Expand Down Expand Up @@ -116,11 +118,26 @@ private void mkdirs(Map<String, String> pathInfo) {
private void initOutputFiles() {
outputFiles = new HashMap<String, String>();
Map<String, String> pathInfo = config.getPathInfo();
outputFiles.put(ConstVal.ENTITY, pathInfo.get(ConstVal.ENTITY_PATH) + ConstVal.ENTITY_NAME);
outputFiles.put(ConstVal.MAPPER, pathInfo.get(ConstVal.MAPPER_PATH) + ConstVal.MAPPER_NAME);
outputFiles.put(ConstVal.XML, pathInfo.get(ConstVal.XML_PATH) + ConstVal.XML_NAME);
outputFiles.put(ConstVal.SERIVCE, pathInfo.get(ConstVal.SERIVCE_PATH) + ConstVal.SERVICE_NAME);
outputFiles.put(ConstVal.SERVICEIMPL, pathInfo.get(ConstVal.SERVICEIMPL_PATH) + ConstVal.SERVICEIMPL_NAME);
String entityPath = pathInfo.get(ConstVal.ENTITY_PATH);
if (StringUtils.isNotBlank(entityPath)) {
outputFiles.put(ConstVal.ENTITY, entityPath + ConstVal.ENTITY_NAME);
}
String mapperPath = pathInfo.get(ConstVal.MAPPER_PATH);
if (StringUtils.isNotBlank(mapperPath)) {
outputFiles.put(ConstVal.MAPPER, mapperPath + ConstVal.MAPPER_NAME);
}
String xmlPath = pathInfo.get(ConstVal.XML_PATH);
if (StringUtils.isNotBlank(xmlPath)) {
outputFiles.put(ConstVal.XML, xmlPath + ConstVal.XML_NAME);
}
String servicePath = pathInfo.get(ConstVal.SERIVCE_PATH);
if (StringUtils.isNotBlank(servicePath)) {
outputFiles.put(ConstVal.SERIVCE, servicePath + ConstVal.SERVICE_NAME);
}
String serviceImplPath = pathInfo.get(ConstVal.SERVICEIMPL_PATH);
if (StringUtils.isNotBlank(serviceImplPath)) {
outputFiles.put(ConstVal.SERVICEIMPL, serviceImplPath + ConstVal.SERVICEIMPL_NAME);
}
}

/**
Expand All @@ -130,27 +147,40 @@ private void initOutputFiles() {
*/
private void batchOutput(String entityName, VelocityContext context) {
try {
String entityFile = String.format(outputFiles.get(ConstVal.ENTITY), entityName);
String mapperFile = String.format(outputFiles.get(ConstVal.MAPPER), entityName);
String xmlFile = String.format(outputFiles.get(ConstVal.XML), entityName);
String serviceFile = String.format(outputFiles.get(ConstVal.SERIVCE), entityName);
String implFile = String.format(outputFiles.get(ConstVal.SERVICEIMPL), entityName);

// 根据override标识来判断是否需要创建文件
if (isCreate(entityFile)) {
vmToFile(context, ConstVal.TEMPLATE_ENTITY, entityFile);
String entityPath = outputFiles.get(ConstVal.ENTITY);
if (StringUtils.isNotBlank(entityPath)) {
String entityFile = String.format(entityPath, entityName);
if (createOrOverride(entityFile)) {
vmToFile(context, ConstVal.TEMPLATE_ENTITY, entityFile);
}
}
if (isCreate(mapperFile)) {
vmToFile(context, ConstVal.TEMPLATE_MAPPER, mapperFile);
String mapperPath = outputFiles.get(ConstVal.MAPPER);
if (StringUtils.isNotBlank(mapperPath)) {
String mapperFile = String.format(mapperPath, entityName);
if (createOrOverride(mapperFile)) {
vmToFile(context, ConstVal.TEMPLATE_MAPPER, mapperFile);
}
}
if (isCreate(xmlFile)) {
vmToFile(context, ConstVal.TEMPLATE_XML, xmlFile);
String xmlPath = outputFiles.get(ConstVal.XML);
if (StringUtils.isNotBlank(xmlPath)) {
String xmlFile = String.format(xmlPath, entityName);
if (createOrOverride(xmlFile)) {
vmToFile(context, ConstVal.TEMPLATE_XML, xmlFile);
}
}
if (isCreate(serviceFile)) {
vmToFile(context, ConstVal.TEMPLATE_SERVICE, serviceFile);
String servicePath = outputFiles.get(ConstVal.SERIVCE);
if (StringUtils.isNotBlank(servicePath)) {
String serviceFile = String.format(servicePath, entityName);
if (createOrOverride(serviceFile)) {
vmToFile(context, ConstVal.TEMPLATE_SERVICE, serviceFile);
}
}
if (isCreate(implFile)) {
vmToFile(context, ConstVal.TEMPLATE_SERVICEIMPL, implFile);
String serviceImplPath = outputFiles.get(ConstVal.SERVICEIMPL);
if (StringUtils.isNotBlank(serviceImplPath)) {
String implFile = String.format(serviceImplPath, entityName);
if (createOrOverride(implFile)) {
vmToFile(context, ConstVal.TEMPLATE_SERVICEIMPL, implFile);
}
}
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -196,7 +226,7 @@ private VelocityEngine getVelocityEngine() {
*
* @return 是否
*/
private boolean isCreate(String filePath) {
private boolean createOrOverride(String filePath) {
File file = new File(filePath);
return !file.exists() || isFileOverride();
}
Expand Down
37 changes: 25 additions & 12 deletions src/main/java/com/baomidou/config/builder/ConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.baomidou.config.po.TableField;
import com.baomidou.config.po.TableInfo;
import com.baomidou.config.rules.*;
import javafx.scene.control.Tab;
import org.apache.commons.lang.StringUtils;

import java.io.File;
Expand Down Expand Up @@ -127,18 +126,32 @@ public List<TableInfo> getTableInfoList() {
*/
private void handlerPackage(String outputDir, PackageConfig config) {
packageInfo = new HashMap<String, String>();
packageInfo.put(ConstVal.ENTITY, joinPackage(config.getParent(), config.getEntity()));
packageInfo.put(ConstVal.MAPPER, joinPackage(config.getParent(), config.getMapper()));
packageInfo.put(ConstVal.XML, joinPackage(config.getParent(), config.getXml()));
packageInfo.put(ConstVal.SERIVCE, joinPackage(config.getParent(), config.getService()));
packageInfo.put(ConstVal.SERVICEIMPL, joinPackage(config.getParent(), config.getServiceImpl()));

pathInfo = new HashMap<String, String>();
pathInfo.put(ConstVal.ENTITY_PATH, joinPath(outputDir, packageInfo.get(ConstVal.ENTITY)));
pathInfo.put(ConstVal.MAPPER_PATH, joinPath(outputDir, packageInfo.get(ConstVal.MAPPER)));
pathInfo.put(ConstVal.XML_PATH, joinPath(outputDir, packageInfo.get(ConstVal.XML)));
pathInfo.put(ConstVal.SERIVCE_PATH, joinPath(outputDir, packageInfo.get(ConstVal.SERIVCE)));
pathInfo.put(ConstVal.SERVICEIMPL_PATH, joinPath(outputDir, packageInfo.get(ConstVal.SERVICEIMPL)));
String entityPath = config.getEntity();
if (StringUtils.isNotBlank(entityPath)) {
packageInfo.put(ConstVal.ENTITY, joinPackage(config.getParent(), entityPath));
pathInfo.put(ConstVal.ENTITY_PATH, joinPath(outputDir, packageInfo.get(ConstVal.ENTITY)));
}
String mapperPath = config.getMapper();
if (StringUtils.isNotBlank(mapperPath)) {
packageInfo.put(ConstVal.MAPPER, joinPackage(config.getParent(), mapperPath));
pathInfo.put(ConstVal.MAPPER_PATH, joinPath(outputDir, packageInfo.get(ConstVal.MAPPER)));
}
String xmlPath = config.getXml();
if (StringUtils.isNotBlank(xmlPath)) {
packageInfo.put(ConstVal.XML, joinPackage(config.getParent(), xmlPath));
pathInfo.put(ConstVal.XML_PATH, joinPath(outputDir, packageInfo.get(ConstVal.XML)));
}
String servicePath = config.getService();
if (StringUtils.isNotBlank(servicePath)) {
packageInfo.put(ConstVal.SERIVCE, joinPackage(config.getParent(), servicePath));
pathInfo.put(ConstVal.SERIVCE_PATH, joinPath(outputDir, packageInfo.get(ConstVal.SERIVCE)));
}
String serviceImplPath = config.getServiceImpl();
if (StringUtils.isNotBlank(serviceImplPath)) {
packageInfo.put(ConstVal.SERVICEIMPL, joinPackage(config.getParent(), serviceImplPath));
pathInfo.put(ConstVal.SERVICEIMPL_PATH, joinPath(outputDir, packageInfo.get(ConstVal.SERVICEIMPL)));
}
}

/**
Expand Down