版权声明:小哥哥小姐姐们,本文为小博主原创文章,转载请附上博主博文网址,并标注作者谢谢~~。违者必究 https://blog.csdn.net/HuHui_/article/details/85150480
系列
- 【Mybatis】Mybatis-Generator-Postgresql返回主键插件
- 【Mybatis】Mybatis-Generator-Oracle返回主键插件
- 【Mybatis】Mybatis-Generator-batchDelete(批量删除)方法插件
- 【Mybatis】Mybatis-Generator-Postgresql&Mysql(批量新增)方法插件
- 【Mybatis】Mybatis-Generator-Oracle(批量新增)方法插件
- 【Mybatis】Mybatis-Generator-Mysql(批量更新)方法插件
- 【Mybatis】Mybatis-Generator-Postgresql(批量更新)方法插件
- 【Mybatis】Mybatis-Generator-Oracle(批量更新)方法插件
- 【Mybatis】Mybatis-Generator-Tool 生成Java和Xml的Method工具类
前言
Mybatis-Generator-Tool 生成Java和Xml的Method工具类。
github上针对mybatis-generator还有很多不同类型的插件,我实在懒的看,而且太多复杂的功能,我简单点搞自己需要的。
装备
-
maven
<dependencies> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> </dependencies>
Core-Code
MethodGeneratorTool
/**
* <b><code>MethodGeneratorTool</code></b>
* <p/>
* Description: 方法生成器
* <p/>
* <b>Creation Time:</b> 2018/12/14 0:50.
*
* @author HuWeihui
*/
public class MethodGeneratorTool {
private final static String BATCH_INSERT = "batchInsert";
private final static String PARAMETER_NAME = "recordList";
private final static String DELETE_PARAMETER_NAME = "ids";
private final static String BATCH_UPDATE = "batchUpdate";
private final static String BATCH_DELETE = "batchDelete";
public final static Integer INSERT = 0;
public final static Integer UPDATE = 1;
/**
* java方法生成构造器.
*
* @param methodName the method name
* @param visibility the visibility
* @param returnJavaType the return java type
* @param parameters the parameters
* @author HuWeihui
* @since hui_project v1
*/
public static Method methodGenerator(String methodName,
JavaVisibility visibility,
FullyQualifiedJavaType returnJavaType,
Parameter... parameters) {
Method method = new Method();
method.setName(methodName);
method.setVisibility(visibility);
method.setReturnType(returnJavaType);
for (Parameter parameter : parameters) {
method.addParameter(parameter);
}
return method;
}
/**
* 导入基础的java类型
*
* @param introspectedTable the introspected table
* @return the set
* @author HuWeihui
* @since hui_project v1
*/
public static Set<FullyQualifiedJavaType> importedBaseTypesGenerator(IntrospectedTable introspectedTable){
//获取实体类类型
FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
//@Param需要导入的类型
FullyQualifiedJavaType paramType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param");
//Integer类型
FullyQualifiedJavaType intInstance = FullyQualifiedJavaType.getIntInstance();
//List<Entity>
FullyQualifiedJavaType listParameterType = FullyQualifiedJavaType.getNewListInstance();
Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
importedTypes.add(parameterType);
importedTypes.add(intInstance);
importedTypes.add(paramType);
importedTypes.add(listParameterType);
return importedTypes;
}
/**
* 默认的批量新增/更新方法构造器.
*
* @param interfaze the interfaze
* @param introspectedTable the introspected table
* @param context the context
* @author HuWeihui
* @since hui_project v1
*/
public static void defaultBatchInsertOrUpdateMethodGen(Integer type ,Interface interfaze,IntrospectedTable introspectedTable, Context context){
//JAVA导入基础包
Set<FullyQualifiedJavaType> importedTypes = MethodGeneratorTool.importedBaseTypesGenerator(introspectedTable);
//List<Entity>
FullyQualifiedJavaType listParameterType = FullyQualifiedJavaType.getNewListInstance();
listParameterType.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass());
String methodName = BATCH_INSERT;
//1.batchInsert
if (type.equals(UPDATE)){
methodName = BATCH_UPDATE;
}
Method insertMethod = MethodGeneratorTool.methodGenerator(methodName,
JavaVisibility.DEFAULT,
FullyQualifiedJavaType.getIntInstance(),
new Parameter(listParameterType, PARAMETER_NAME, "@Param(\"" + PARAMETER_NAME + "\")"));
CommentGenerator commentGenerator = context.getCommentGenerator();
commentGenerator.addGeneralMethodComment(insertMethod, introspectedTable);
interfaze.addImportedTypes(importedTypes);
interfaze.addMethod(insertMethod);
}
/**
* 默认的批量删除方法构造器.
*
* @param interfaze the interfaze
* @param introspectedTable the introspected table
* @param context the context
* @author HuWeihui
* @since hui_project v1
*/
public static void defaultBatchDeleteMethodGen(Interface interfaze,IntrospectedTable introspectedTable, Context context){
//JAVA导入基础包
Set<FullyQualifiedJavaType> importedTypes = MethodGeneratorTool.importedBaseTypesGenerator(introspectedTable);
FullyQualifiedJavaType paramType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType();
Method batchDeleteMethod = MethodGeneratorTool.methodGenerator(BATCH_DELETE,
JavaVisibility.DEFAULT,
FullyQualifiedJavaType.getIntInstance(),
new Parameter(new FullyQualifiedJavaType(paramType.getFullyQualifiedName()+"[]"), DELETE_PARAMETER_NAME, "@Param(\""+DELETE_PARAMETER_NAME+"\")"));
context.getCommentGenerator().addGeneralMethodComment(batchDeleteMethod,introspectedTable);
interfaze.addImportedTypes(importedTypes);
interfaze.addMethod(batchDeleteMethod);
}
}
SqlMapperGeneratorTool
/**
* <b><code>SqlMapperGeneratorTool</code></b>
* <p/>
* Description: 一些基础XMLELEMENT的构造器
* <p/>
* <b>Creation Time:</b> 2018/12/15 18:40.
*
* @author HuWeihui
*/
public class SqlMapperGeneratorTool {
/**
* The constant INSERT.
*/
public static final String INSERT = "insert";
/**
* The constant DELETE.
*/
public static final String DELETE = "delete";
/**
* The constant UPDATE.
*/
public static final String UPDATE = "update";
/**
* The constant SELECT.
*/
public static final String SELECT = "select";
/**
* 基础XmlElement构造器.
*
* @param sqlElementType the sql element type
* @param sqlMapperId the sql mapper id
* @param parameterType the parameter type
* @return the xml element
* @author HuWeihui
* @since hui_project v1
*/
public static XmlElement baseElementGenerator(String sqlElementType, String sqlMapperId,FullyQualifiedJavaType parameterType){
XmlElement baseElement = new XmlElement(sqlElementType);
baseElement.addAttribute(new Attribute("id", sqlMapperId));
baseElement.addAttribute(new Attribute("parameterType", parameterType.getFullyQualifiedName()));
return baseElement;
}
/**
*基础foreach Element构造器.
*
* @param collectionName the collection name
* @param itemName the item name
* @param indexName the index name
* @param separatorName the separator name
* @return the xml element
* @author HuWeihui
* @since hui_project v1
*/
public static XmlElement baseForeachElementGenerator(String collectionName,String itemName,String indexName ,String separatorName){
XmlElement foreachElement = new XmlElement("foreach");
if (null!=collectionName){
foreachElement.addAttribute(new Attribute("collection", collectionName));
}
if (null!=itemName){
foreachElement.addAttribute(new Attribute("item", itemName));
}
if (null!=indexName){
foreachElement.addAttribute(new Attribute("index", indexName));
}
if (null!=separatorName){
foreachElement.addAttribute(new Attribute("separator", separatorName));
}
return foreachElement;
}
/**
* 基础IF Element构造器.
*
* @param columnJavaTypeName the column java type name
* @param sql the sql
* @param judgeNull the judge null
* @return the xml element
* @author HuWeihui
* @since hui_project v1
*/
public static XmlElement baseIfJudgeElementGen(String columnJavaTypeName,String sql ,boolean judgeNull){
String colmunJudge = "";
if (judgeNull){
colmunJudge = columnJavaTypeName + " ==null ";
}else {
colmunJudge = columnJavaTypeName + " !=null ";
}
XmlElement ifElement = new XmlElement("if");
ifElement.addAttribute(new Attribute("test", colmunJudge));
ifElement.addElement(new TextElement(sql));
return ifElement;
}
/**
* 基础Trim Element构造器.
*
* @param prefix the prefix
* @param suffix the suffix
* @param suffixOverrides the suffix overrides
* @return the xml element
* @author HuWeihui
* @since hui_project v1
*/
public static XmlElement baseTrimElement(String prefix,String suffix,String suffixOverrides){
XmlElement trimElement = new XmlElement("trim");
if (null != prefix){
trimElement.addAttribute(new Attribute("prefix", prefix));
}
if (null != suffix){
trimElement.addAttribute(new Attribute("suffix", suffix));
}
if (null!=suffixOverrides){
trimElement.addAttribute(new Attribute("suffixOverrides", suffixOverrides));
}
return trimElement;
}
}
Github
https://github.com/ithuhui/hui-mybatis-plugins/tree/master/src/main/java/com/hui/mybatis/plugins
作者
作者:HuHui
转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢