001/* 002 * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.mybatisflex.core.table; 017 018import com.mybatisflex.core.FlexGlobalConfig; 019import com.mybatisflex.core.mask.CompositeMaskTypeHandler; 020import com.mybatisflex.core.mask.MaskTypeHandler; 021import com.mybatisflex.core.util.ArrayUtil; 022import com.mybatisflex.core.util.StringUtil; 023import org.apache.ibatis.session.Configuration; 024import org.apache.ibatis.type.*; 025 026import java.sql.Time; 027import java.sql.Timestamp; 028import java.time.*; 029import java.time.chrono.JapaneseDate; 030import java.util.Date; 031 032public class ColumnInfo { 033 034 private static final Class<?>[] needGetTypeHandlerTypes = { 035 Date.class, java.sql.Date.class, Time.class, Timestamp.class, 036 Instant.class, LocalDate.class, LocalDateTime.class, LocalTime.class, OffsetDateTime.class, OffsetTime.class, ZonedDateTime.class, 037 Year.class, Month.class, YearMonth.class, JapaneseDate.class, 038 byte[].class, Byte[].class, Byte.class, 039 }; 040 041 /** 042 * 数据库列名。 043 */ 044 protected String column; 045 046 /** 047 * 列的别名。 048 */ 049 protected String[] alias; 050 051 /** 052 * java entity 定义的属性名称(field name)。 053 */ 054 protected String property; 055 056 /** 057 * 属性类型。 058 */ 059 protected Class<?> propertyType; 060 061 /** 062 * 该列对应的 jdbcType。 063 */ 064 protected JdbcType jdbcType; 065 066 /** 067 * 自定义 TypeHandler。 068 */ 069 protected TypeHandler<?> typeHandler; 070 071 /** 072 * 最终使用和构建出来的 typeHandler 073 */ 074 protected TypeHandler<?> buildTypeHandler; 075 076 /** 077 * 数据脱敏类型。 078 */ 079 protected String maskType; 080 081 /** 082 * 是否忽略 083 */ 084 protected boolean ignore; 085 086 087 public String getColumn() { 088 return column; 089 } 090 091 public void setColumn(String column) { 092 this.column = column; 093 } 094 095 public String[] getAlias() { 096 return alias; 097 } 098 099 public void setAlias(String[] alias) { 100 this.alias = alias; 101 } 102 103 public String getProperty() { 104 return property; 105 } 106 107 public void setProperty(String property) { 108 this.property = property; 109 } 110 111 public Class<?> getPropertyType() { 112 return propertyType; 113 } 114 115 public void setPropertyType(Class<?> propertyType) { 116 this.propertyType = propertyType; 117 } 118 119 public JdbcType getJdbcType() { 120 return jdbcType; 121 } 122 123 public void setJdbcType(JdbcType jdbcType) { 124 this.jdbcType = jdbcType; 125 } 126 127 public TypeHandler<?> buildTypeHandler(Configuration configuration) { 128 129 if (buildTypeHandler != null) { 130 return buildTypeHandler; 131 } 132 133 //脱敏规则配置 134 else if (StringUtil.isNotBlank(maskType)) { 135 if (typeHandler != null) { 136 //noinspection unchecked 137 buildTypeHandler = new CompositeMaskTypeHandler(maskType, (TypeHandler<Object>) typeHandler); 138 } else { 139 buildTypeHandler = new MaskTypeHandler(maskType); 140 } 141 } 142 143 //用户自定义的 typeHandler 144 else if (typeHandler != null) { 145 buildTypeHandler = typeHandler; 146 } 147 148 //枚举 149 else if (propertyType.isEnum() || ArrayUtil.contains(needGetTypeHandlerTypes, propertyType)) { 150 if (configuration == null) { 151 configuration = FlexGlobalConfig.getDefaultConfig().getConfiguration(); 152 } 153 if (configuration != null) { 154 buildTypeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(propertyType); 155 } 156 } 157 158 return buildTypeHandler; 159 } 160 161 public void setTypeHandler(TypeHandler<?> typeHandler) { 162 this.typeHandler = typeHandler; 163 } 164 165 public String getMaskType() { 166 return maskType; 167 } 168 169 public void setMaskType(String maskType) { 170 this.maskType = maskType; 171 } 172 173 174 public boolean isIgnore() { 175 return ignore; 176 } 177 178 public void setIgnore(boolean ignore) { 179 this.ignore = ignore; 180 } 181}