Skip to content

Commit

Permalink
提供基于索引生成基础方法. (#6652)
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong authored Jan 3, 2025
1 parent bd1563c commit 0b1f60f
Show file tree
Hide file tree
Showing 15 changed files with 748 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2011-2024, baomidou ([email protected]).
*
* 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.
*/
package com.baomidou.mybatisplus.generator;

import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.model.MapperMethod;

import java.util.List;
import java.util.Set;

/**
* Mapper层方法生成处理器
*
* @author nieqiurong
* @since 3.5.10
*/
public interface IGenerateMapperMethodHandler {

/**
* 获取生成方法
*
* @param tableInfo 表信息
* @return 索引方法
*/
List<MapperMethod> getMethodList(TableInfo tableInfo);

/**
* 获取需要导入的包
*
* @param tableInfo 表信息
* @return 导包列表
*/
Set<String> getImportPackages(TableInfo tableInfo);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.function.ConverterFileName;
import com.baomidou.mybatisplus.generator.IGenerateMapperMethodHandler;
import com.baomidou.mybatisplus.generator.model.MapperMethod;
import com.baomidou.mybatisplus.generator.util.ClassUtils;
import lombok.Getter;
import org.apache.ibatis.cache.Cache;
Expand All @@ -29,8 +31,11 @@
import org.slf4j.LoggerFactory;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* 控制器属性配置
Expand Down Expand Up @@ -160,6 +165,13 @@ public Class<? extends Cache> getCache() {
return this.cache == null ? LoggingCache.class : this.cache;
}

/**
* Mapper层方法生成
*
* @since 3.5.10
*/
private IGenerateMapperMethodHandler generateMapperMethodHandler;

@Override
@NotNull
public Map<String, Object> renderData(@NotNull TableInfo tableInfo) {
Expand All @@ -179,6 +191,14 @@ public Map<String, Object> renderData(@NotNull TableInfo tableInfo) {
data.put("superMapperClass", ClassUtils.getSimpleName(this.superClass));
data.put("generateMapperXml", this.generateMapperXml);
data.put("generateMapper", this.generateMapper);
List<MapperMethod> methodList = null;
Set<String> importPackages = null;
if (generateMapperMethodHandler != null) {
methodList = generateMapperMethodHandler.getMethodList(tableInfo);
importPackages = generateMapperMethodHandler.getImportPackages(tableInfo);
}
data.put("importPackages", importPackages == null ? Collections.emptySet() : importPackages);
data.put("mapperMethodList", methodList == null ? Collections.emptyList() : methodList);
return data;
}

Expand Down Expand Up @@ -396,6 +416,18 @@ public Builder disableMapperXml() {
return this;
}

/**
* Mapper层方法生成处理器
*
* @param generateMapperMethodHandler 处理器
* @return this
* @since 3.5.10
*/
public Builder generateMapperMethodHandler(IGenerateMapperMethodHandler generateMapperMethodHandler) {
this.mapper.generateMapperMethodHandler = generateMapperMethodHandler;
return this;
}

@NotNull
public Mapper get() {
return this.mapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import com.baomidou.mybatisplus.generator.config.builder.Entity;
import com.baomidou.mybatisplus.generator.config.builder.Service;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.jdbc.DatabaseMetaDataWrapper;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
Expand All @@ -42,11 +44,13 @@ public class TableInfo {
/**
* 策略配置
*/
@Getter
private final StrategyConfig strategyConfig;

/**
* 全局配置信息
*/
@Getter
private final GlobalConfig globalConfig;

/**
Expand Down Expand Up @@ -134,6 +138,23 @@ public class TableInfo {
*/
private final Entity entity;

/**
* 索引信息
*
* @since 3.5.10
*/
@Setter
@Getter
private List<DatabaseMetaDataWrapper.Index> indexList;

/**
* 字段信息
*
* @since 3.5.10
*/
@Getter
private final Map<String, TableField> tableFieldMap = new HashMap<>();

/**
* 构造方法
*
Expand Down Expand Up @@ -184,7 +205,9 @@ public void addField(@NotNull TableField field) {
if (entity.matchIgnoreColumns(field.getColumnName())) {
// 忽略字段不在处理
return;
} else if (entity.matchSuperEntityColumns(field.getColumnName())) {
}
tableFieldMap.put(field.getName(), field);
if (entity.matchSuperEntityColumns(field.getColumnName())) {
this.commonFields.add(field);
} else {
this.fields.add(field);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2011-2024, baomidou ([email protected]).
*
* 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.
*/
package com.baomidou.mybatisplus.generator.index;

import com.baomidou.mybatisplus.generator.IGenerateMapperMethodHandler;

/**
* @author nieqiurong
* @since 3.5.10
*/
public abstract class AbstractMapperMethodHandler implements IGenerateMapperMethodHandler {

/**
* 生成Java方法(default)
* <pre>
* default ${returnValue} ${methodName}(${args}) {
* return ${returnBody};
* }
* <pre/>
* Example:
* <pre>
* default UserInfo selectByCardNo(String cardNo) {
* return selectOne(Wrappers.<UserInfo>query().eq(TUserInfo.CARD_NO, cardNo));
* }
* </pre>
*/
public String buildMethod(String methodName, String args, String returnValue, String returnBody) {
return "default" + " " +
returnValue + " " + methodName + "(" + args + ")" + " " + "{" + "\n" +
" return " + returnBody + ";" + "\n" +
" }\n";
}

/**
* 构建Kotlin方法
* <pre>
* fun ${methodName}(${args}) :${returnValue} {
* return ${returnBody};
* }
* </pre>
* Example:
* <pre>
* fun selectByCardNo(cardNo: String) :UserInfo? {
* return selectOne(Wrappers.query<UserInfo>().eq(UserInfo.CARD_NO, cardNo));
* }
* </pre>
*
* @param methodName 方法名
* @param args 参数列表
* @param returnValue 返回值
* @param returnBody 返回体
* @return 方法
*/
public String buildKotlinMethod(String methodName, String args, String returnValue, String returnBody) {
return "fun " + methodName + "(" + args + ")" + " :" + returnValue + " {" + "\n" +
" return " + returnBody + ";" + "\n" +
" }\n";
}

}
Loading

0 comments on commit 0b1f60f

Please sign in to comment.