Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #294

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open

Main #294

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
Original file line number Diff line number Diff line change
Expand Up @@ -121,35 +121,39 @@ public String getEntity(boolean kotlin) {
public TemplateConfig disable(@NotNull TemplateType... templateTypes) {
if (templateTypes != null && templateTypes.length > 0) {
for (TemplateType templateType : templateTypes) {
switch (templateType) {
case ENTITY:
this.entity = null;
this.entityKt = null;
//暂时没其他多的需求,使用一个单独的boolean变量进行支持一下.
this.disableEntity = true;
break;
case CONTROLLER:
this.controller = null;
break;
case MAPPER:
this.mapper = null;
break;
case XML:
this.xml = null;
break;
case SERVICE:
this.service = null;
break;
case SERVICE_IMPL:
this.serviceImpl = null;
break;
default:
}
disableTemplateType(templateType);
}
}
return this;
}

private void disableTemplateType(TemplateType templateType) {
switch (templateType) {
case ENTITY:
this.entity = null;
this.entityKt = null;
//暂时没其他多的需求,使用一个单独的boolean变量进行支持一下.
this.disableEntity = true;
break;
case CONTROLLER:
this.controller = null;
break;
case MAPPER:
this.mapper = null;
break;
case XML:
this.xml = null;
break;
case SERVICE:
this.service = null;
break;
case SERVICE_IMPL:
this.serviceImpl = null;
break;
default:
}
}

/**
* 禁用全部模板
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baomidou.mybatisplus.generator.config.po;

public interface ColumnNameStrategy {
String convert(String columnName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baomidou.mybatisplus.generator.config.po;

public class NoChangeStrategy implements ColumnNameStrategy {
@Override
public String convert(String columnName) {
return columnName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ public TableField(@NotNull ConfigBuilder configBuilder, @NotNull String name) {
this.globalConfig = configBuilder.getGlobalConfig();
}


private boolean shouldConvertBooleanProperty(String propertyName) {
return entity.isBooleanColumnRemoveIsPrefix()
&& "boolean".equalsIgnoreCase(this.getPropertyType())
&& propertyName.startsWith("is");
}

private void convertBooleanProperty() {
this.convert = true;
}

private void updatePropertyName(String propertyName) {
this.propertyName = StringUtils.removePrefixAfterPrefixToLower(propertyName, 2);
}


/**
* 设置属性名称
*
Expand All @@ -151,27 +167,32 @@ public TableField(@NotNull ConfigBuilder configBuilder, @NotNull String name) {
*/
public TableField setPropertyName(@NotNull String propertyName, @NotNull IColumnType columnType) {
this.columnType = columnType;
if (entity.isBooleanColumnRemoveIsPrefix()
&& "boolean".equalsIgnoreCase(this.getPropertyType()) && propertyName.startsWith("is")) {
this.convert = true;
this.propertyName = StringUtils.removePrefixAfterPrefixToLower(propertyName, 2);
if (shouldConvertBooleanProperty(propertyName)) {
convertBooleanProperty();
updatePropertyName(propertyName);
return this;
}
// 下划线转驼峰策略
if (NamingStrategy.underline_to_camel.equals(this.entity.getColumnNaming())) {
this.convert = !propertyName.equalsIgnoreCase(NamingStrategy.underlineToCamel(this.columnName));
}
// 原样输出策略
if (NamingStrategy.no_change.equals(this.entity.getColumnNaming())) {
this.convert = !propertyName.equalsIgnoreCase(this.columnName);
}

ColumnNameStrategy columnNameStrategy = getColumnNameStrategy();

this.convert = !propertyName.equalsIgnoreCase(columnNameStrategy.convert(this.columnName));
if (entity.isTableFieldAnnotationEnable()) {
this.convert = true;
}
this.propertyName = propertyName;
return this;
}

private ColumnNameStrategy getColumnNameStrategy() {
switch (this.entity.getColumnNaming()) {
case underline_to_camel:
return new UnderlineToCamelCaseStrategy();
case no_change:
default:
return new NoChangeStrategy();
}
}

public String getPropertyType() {
if (null != columnType) {
return columnType.getType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baomidou.mybatisplus.generator.config.po;

import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class UnderlineToCamelCaseStrategy implements ColumnNameStrategy {
@Override
public String convert(String columnName) {
return NamingStrategy.underlineToCamel(columnName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static String removeSuffix(String name, Set<String> suffix) {
* @return 转换后的字符串
*/
public static String removeSuffixAndCamel(String name, Set<String> suffix) {
return underlineToCamel(removeSuffix(name, suffix));
return NamingStrategyUtils.underlineToCamel(removeSuffix(name, suffix));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baomidou.mybatisplus.generator.config.rules;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.ConstVal;

import java.util.Arrays;

public class NamingStrategyUtils {
public static String underlineToCamel(String name) {
if (StringUtils.isBlank(name)) {
return StringPool.EMPTY;
}
String tempName = name;
if (StringUtils.isCapitalMode(name) || StringUtils.isMixedMode(name)) {
tempName = name.toLowerCase();
}
StringBuilder result = new StringBuilder();
String[] camels = tempName.split(ConstVal.UNDERLINE);
Arrays.stream(camels).filter(camel -> !StringUtils.isBlank(camel)).forEach(camel -> {
if (result.length() == 0) {
result.append(StringUtils.firstToLowerCase(camel));
} else {
result.append(capitalFirst(camel));
}
});
return result.toString();
}

private static String capitalFirst(String name) {
if (StringUtils.isNotBlank(name)) {
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
return StringPool.EMPTY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baomidou.mybatisplus.generator.jdbc;

public class CommentFormatter {
public String formatComment(String comment) {
// Remove leading and trailing whitespaces
comment = comment.trim();

// Split the comment into lines
String[] lines = comment.split("\\r?\\n");

// Remove leading and trailing whitespaces from each line
for (int i = 0; i < lines.length; i++) {
lines[i] = lines[i].trim();
}

// Reconstruct the formatted comment
StringBuilder formattedComment = new StringBuilder();
formattedComment.append("/**\n");
for (String line : lines) {
formattedComment.append(" * ");
formattedComment.append(line);
formattedComment.append("\n");
}
formattedComment.append(" */\n");

return formattedComment.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public Map<String, Column> getColumnsInfo(String catalog, String schema, String
column.jdbcType = JdbcType.forCode(resultSet.getInt("DATA_TYPE"));
column.length = resultSet.getInt("COLUMN_SIZE");
column.scale = resultSet.getInt("DECIMAL_DIGITS");
column.remarks = formatComment(resultSet.getString("REMARKS"));
CommentFormatter formatter = new CommentFormatter();
column.remarks = formatter.formatComment(resultSet.getString("REMARKS"));
column.defaultValue = resultSet.getString("COLUMN_DEF");
column.nullable = resultSet.getInt("NULLABLE") == DatabaseMetaData.columnNullable;
try {
Expand All @@ -125,9 +126,7 @@ public Map<String, Column> getColumnsInfo(String catalog, String schema, String
}
}

public String formatComment(String comment) {
return StringUtils.isBlank(comment) ? StringPool.EMPTY : comment.replaceAll("\r\n", "\t");
}


public Table getTableInfo(String tableName) {
return getTableInfo(this.catalog, this.schema, tableName);
Expand All @@ -144,7 +143,8 @@ public List<Table> getTables(String catalog, String schemaPattern, String tableN
while (resultSet.next()) {
table = new Table();
table.name = resultSet.getString("TABLE_NAME");
table.remarks = formatComment(resultSet.getString("REMARKS"));
CommentFormatter formatter = new CommentFormatter();
table.remarks = formatter.formatComment(resultSet.getString("REMARKS"));
table.tableType = resultSet.getString("TABLE_TYPE");
tables.add(table);
}
Expand All @@ -160,7 +160,8 @@ public Table getTableInfo(String catalog, String schema, String tableName) {
try (ResultSet resultSet = databaseMetaData.getTables(catalog, schema, tableName, new String[]{"TABLE", "VIEW"})) {
table.name = tableName;
while (resultSet.next()) {
table.remarks = formatComment(resultSet.getString("REMARKS"));
CommentFormatter formatter = new CommentFormatter();
table.remarks = formatter.formatComment(resultSet.getString("REMARKS"));
table.tableType = resultSet.getString("TABLE_TYPE");
}
} catch (SQLException e) {
Expand Down