All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zcj.util.db.bean.ToBeanConfig Maven / Gradle / Ivy

package com.zcj.util.db.bean;

import com.zcj.util.db.util.Utils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ToBeanConfig {

    private final String packageName;
    private final String tablePrefix;
    private final Set excludeColumns;
    private final Set includeTables;
    private final Map columnJavaTypeMap;

    private ToBeanConfig(TableToBeanBuilder builder) {
        packageName = builder.packageName;
        tablePrefix = builder.tablePrefix;
        excludeColumns = builder.excludeColumns;
        includeTables = builder.includeTables;
        columnJavaTypeMap = builder.columnJavaTypeMap;
    }

    public static ToBeanConfig defaultConfig() {
        return ToBeanConfig.builder().build();
    }

    public static TableToBeanBuilder builder() {
        return new TableToBeanBuilder();
    }

    public static class TableToBeanBuilder {

        private String packageName = "";
        private String tablePrefix = "";
        private Set excludeColumns = new HashSet<>();
        private Set includeTables = new HashSet<>();
        private Map columnJavaTypeMap = new HashMap<>();

        // 设置表属性类型到 JAVA 类型的映射
        public TableToBeanBuilder addColumnJavaTypeMap(String sqlTypeName, String javaTypeSimpleName) {
            if (Utils.isNotBlank(sqlTypeName) && Utils.isNotBlank(javaTypeSimpleName)) {
                this.columnJavaTypeMap.put(sqlTypeName.toLowerCase(), javaTypeSimpleName);
            }
            return this;
        }

        // 设置表属性类型到 JAVA 类型的映射
        public TableToBeanBuilder addColumnJavaTypeMap(String sqlTypeName, Class javaType) {
            if (Utils.isNotBlank(sqlTypeName) && javaType != null) {
                this.columnJavaTypeMap.put(sqlTypeName.toLowerCase(), javaType.getSimpleName());
            }
            return this;
        }

        // 设置包路径
        public TableToBeanBuilder packageName(String packageName) {
            if (packageName != null) {
                this.packageName = packageName;
            }
            return this;
        }

        // 设置表名的前缀(不区分大小写)
        public TableToBeanBuilder tablePrefix(String tablePrefix) {
            if (tablePrefix != null) {
                this.tablePrefix = tablePrefix.toLowerCase();
            }
            return this;
        }

        // 设置需要排除的表属性(不区分大小写)
        public TableToBeanBuilder excludeColumn(String... columns) {
            if (columns != null) {
                for (String column : columns) {
                    if (Utils.isNotBlank(column)) {
                        this.excludeColumns.add(column.toLowerCase());
                    }
                }
            }
            return this;
        }

        // 只转化指定的表
        public TableToBeanBuilder includeTable(String... tables) {
            if (tables != null) {
                for (String table : tables) {
                    if (Utils.isNotBlank(table)) {
                        this.includeTables.add(table);
                    }
                }
            }
            return this;
        }

        public ToBeanConfig build() {
            return new ToBeanConfig(this);
        }
    }

    public String getPackageName() {
        return packageName;
    }

    public String getTablePrefix() {
        return tablePrefix;
    }

    public Set getExcludeColumns() {
        return excludeColumns;
    }

    public Set getIncludeTables() {
        return includeTables;
    }

    public Map getColumnJavaTypeMap() {
        return columnJavaTypeMap;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy