001package cn.mybatis.mp.generator.config;
002
003import cn.mybatis.mp.generator.database.meta.ColumnInfo;
004import cn.mybatis.mp.generator.strategy.NamingStrategy;
005import lombok.Getter;
006import org.apache.ibatis.type.JdbcType;
007
008import java.math.BigDecimal;
009import java.time.LocalDateTime;
010import java.time.LocalTime;
011import java.time.OffsetDateTime;
012import java.time.OffsetTime;
013import java.util.HashMap;
014import java.util.Map;
015import java.util.function.Consumer;
016import java.util.function.Function;
017
018@Getter
019public class EntityConfig {
020
021    /**
022     * 数据库类型的java映射
023     */
024    private final Map<JdbcType, Class<?>> typeMapping = new HashMap<>();
025    /**
026     * 实体类父类
027     */
028    private String superClass;
029    /**
030     * 是否使用 lombok
031     */
032    private boolean lombok = true;
033    /**
034     * 注解上是否加上schema
035     */
036    private boolean schema = false;
037    /**
038     * 默认TableId代码,数据库非自增时生效
039     */
040    private String defaultTableIdCode;
041    /**
042     * 实体类包名
043     */
044    private String packageName = "DO";
045    /**
046     * 实体类名字转换器
047     */
048    private Function<String, String> nameConvert;
049    /**
050     * 字段名策略
051     */
052    private NamingStrategy fieldNamingStrategy = NamingStrategy.UNDERLINE_TO_CAMEL;
053    /**
054     * 字段名转换器
055     */
056    private Function<ColumnInfo, String> fieldNameConverter;
057    /**
058     * 备注转换器
059     */
060    private Function<ColumnInfo, String> remarksConverter;
061
062    {
063        typeMapping.put(JdbcType.BIT, Boolean.class);
064        typeMapping.put(JdbcType.TINYINT, Byte.class);
065        typeMapping.put(JdbcType.SMALLINT, Integer.class);
066        typeMapping.put(JdbcType.INTEGER, Integer.class);
067        typeMapping.put(JdbcType.BIGINT, Long.class);
068        typeMapping.put(JdbcType.FLOAT, Double.class);
069        typeMapping.put(JdbcType.REAL, Double.class);
070        typeMapping.put(JdbcType.DOUBLE, Double.class);
071        typeMapping.put(JdbcType.NUMERIC, BigDecimal.class);
072        typeMapping.put(JdbcType.DECIMAL, BigDecimal.class);
073        typeMapping.put(JdbcType.CHAR, Character.class);
074        typeMapping.put(JdbcType.VARCHAR, String.class);
075        typeMapping.put(JdbcType.LONGVARCHAR, String.class);
076        typeMapping.put(JdbcType.DATE, LocalDateTime.class);
077        typeMapping.put(JdbcType.TIME, LocalTime.class);
078        typeMapping.put(JdbcType.TIMESTAMP, LocalTime.class);
079        typeMapping.put(JdbcType.BINARY, byte[].class);
080        typeMapping.put(JdbcType.VARBINARY, byte[].class);
081        typeMapping.put(JdbcType.LONGVARBINARY, byte[].class);
082        typeMapping.put(JdbcType.BLOB, byte[].class);
083        typeMapping.put(JdbcType.CLOB, String.class);
084        typeMapping.put(JdbcType.BOOLEAN, Boolean.class);
085        typeMapping.put(JdbcType.NVARCHAR, String.class);
086        typeMapping.put(JdbcType.NCHAR, String.class);
087        typeMapping.put(JdbcType.NCLOB, String.class);
088        typeMapping.put(JdbcType.LONGNVARCHAR, String.class);
089        typeMapping.put(JdbcType.NCLOB, String.class);
090        typeMapping.put(JdbcType.DATETIMEOFFSET, OffsetDateTime.class);
091        typeMapping.put(JdbcType.TIME_WITH_TIMEZONE, OffsetTime.class);
092        typeMapping.put(JdbcType.TIMESTAMP_WITH_TIMEZONE, OffsetDateTime.class);
093    }
094
095
096    /**
097     * 实体类的父类
098     *
099     * @param superClass
100     * @return
101     */
102    public EntityConfig superClass(String superClass) {
103        this.superClass = superClass;
104        return this;
105    }
106
107    /**
108     * 设置是否使用 lombok
109     */
110    public EntityConfig lombok(boolean lombok) {
111        this.lombok = lombok;
112        return this;
113    }
114
115    /**
116     * 设置是否生成 schema
117     *
118     * @param schema
119     * @return
120     */
121    public EntityConfig schema(boolean schema) {
122        this.schema = schema;
123        return this;
124    }
125
126    /**
127     * 包名设置
128     *
129     * @param packageName
130     * @return
131     */
132    public EntityConfig packageName(String packageName) {
133        this.packageName = packageName;
134        return this;
135    }
136
137    /**
138     * 字段类型映射
139     *
140     * @param consumer
141     * @return
142     */
143    public EntityConfig typeMapping(Consumer<Map<JdbcType, Class<?>>> consumer) {
144        consumer.accept(this.typeMapping);
145        return this;
146    }
147
148
149    /**
150     * 实体类名字转换器
151     */
152    public EntityConfig nameConvert(Function<String, String> nameConvert) {
153        this.nameConvert = nameConvert;
154        return this;
155    }
156
157    /**
158     * 字段名字策略
159     */
160    public EntityConfig fieldNamingStrategy(NamingStrategy fieldNamingStrategy) {
161        this.fieldNamingStrategy = fieldNamingStrategy;
162        return this;
163    }
164
165    /**
166     * 字段名字转换器
167     */
168    public EntityConfig fieldNameConverter(Function<ColumnInfo, String> fieldNameConverter) {
169        this.fieldNameConverter = fieldNameConverter;
170        return this;
171    }
172
173    /**
174     * 备注转换器
175     */
176    public EntityConfig remarksConverter(Function<ColumnInfo, String> remarksConverter) {
177        this.remarksConverter = remarksConverter;
178        return this;
179    }
180
181    /**
182     * 默认TableId代码,数据库非自增时生效
183     *
184     * @param defaultTableIdCode
185     * @return
186     */
187    public EntityConfig defaultTableIdCode(String defaultTableIdCode) {
188        this.defaultTableIdCode = defaultTableIdCode;
189        return this;
190    }
191}