cn.sylinx.hbatis.db.mapper.ModelBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hbatis-core Show documentation
Show all versions of hbatis-core Show documentation
hbatis is a simple orm framework
The newest version!
package cn.sylinx.hbatis.db.mapper;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import cn.sylinx.hbatis.db.mapper.anno.ColumnDesc;
import cn.sylinx.hbatis.db.mapper.anno.PrimaryKey;
import cn.sylinx.hbatis.db.mapper.anno.Table;
import cn.sylinx.hbatis.kit.StrKit;
import cn.sylinx.hbatis.plugin.model.ModelCacheManager;
import cn.sylinx.hbatis.plugin.model.ModelFabric;
public class ModelBuilder {
public static InsertMapper buildInsertMapper(T t) {
final Class> clz = t.getClass();
final ModelFabric mf = getModelFabric(clz);
final String table = mf.getTableName();
if (StrKit.isBlank(table)) {
throw new IllegalArgumentException("数据库表为空");
}
InsertMapper mapper = new InsertMapper() {
@Override
public String getTableName() {
return table;
}
@Override
public Map getJavaToJdbcMapper() {
return mf.getAttrMapping();
}
};
return mapper;
}
public static UpdateMapper buildUpdateMapper(T t) {
final Class> clz = t.getClass();
final ModelFabric mf = getModelFabric(clz);
final String table = mf.getTableName();
if (StrKit.isBlank(table)) {
throw new IllegalArgumentException("数据库表为空");
}
final PrimaryKey pks = mf.getPrimaryKey();
UpdateMapper mapper = new UpdateMapper() {
@Override
public String getTableName() {
return table;
}
@Override
public Map getJavaToJdbcMapper() {
return mf.getAttrMapping();
}
@Override
public List getPrimaryKeyFieldNameList() {
List pklist = new ArrayList();
if (pks != null) {
String[] pkArray = pks.value();
for (String pk : pkArray) {
if (StrKit.isNotBlank(pk)) {
pklist.add(pk);
}
}
}
if (pklist.isEmpty()) {
pklist.add("id");
}
return pklist;
}
};
return mapper;
}
public static DeleteMapper buildDeleteMapper(T t) {
final Class> clz = t.getClass();
final ModelFabric mf = getModelFabric(clz);
final String table = mf.getTableName();
if (StrKit.isBlank(table)) {
throw new IllegalArgumentException("数据库表为空");
}
final PrimaryKey pks = mf.getPrimaryKey();
DeleteMapper mapper = new DeleteMapper() {
@Override
public String getTableName() {
return table;
}
@Override
public Map getJavaToJdbcMapper() {
return mf.getAttrMapping();
}
@Override
public List getPrimaryKeyFieldNameList() {
List pklist = new ArrayList();
if (pks != null) {
String[] pkArray = pks.value();
for (String pk : pkArray) {
if (StrKit.isNotBlank(pk)) {
pklist.add(pk);
}
}
}
if (pklist.isEmpty()) {
pklist.add("id");
}
return pklist;
}
};
return mapper;
}
/**
* 通过类注解获取QueryMapper对象
*
* @param clz Class
* @return QueryMapper
*/
public static QueryMapper buildQueryMapper(final Class clz) {
final ModelFabric mf = getModelFabric(clz);
if (mf.isMappingEmpty()) {
return null;
}
QueryMapper t = new QueryMapper() {
@Override
public Map getJdbcToJavaMapper() {
return mf.getJdbcMapping();
}
@Override
public Class getValueObjectClass() {
return clz;
}
};
return t;
}
public static final void buildColumnNamesAndTypes(ResultSetMetaData rsmd, String[] labelNames, int[] types)
throws SQLException {
for (int i = 1; i < labelNames.length; i++) {
labelNames[i] = rsmd.getColumnLabel(i);
types[i] = rsmd.getColumnType(i);
}
}
public static List getObjectAllFieldsWithcache(Class> cclz) {
return getModelFabric(cclz).getFields();
}
/**
* 获取乐观锁字段
*
* @param cclz
* @return
*/
public static Field getVersionField(Class> cclz) {
return getModelFabric(cclz).getVersionField();
}
public static Map getObjectAllFieldsMapWithcache(Class> cclz) {
return getModelFabric(cclz).getFieldMap();
}
/**
* 是否忽略的字段
*
* @param f
* @return
*/
private static boolean isIgnoreField(Field f) {
String modifiers = Modifier.toString(f.getModifiers());
if (modifiers.contains("final") || modifiers.contains("static") || modifiers.contains("transient")
|| f.getName().equals("context")) {
return true;
}
ColumnDesc cd = f.getAnnotation(ColumnDesc.class);
if (cd != null && cd.ignore()) {
// 忽略字段
return true;
}
return false;
}
public static List getObjectAllFields(Class> cclz) {
List fields = new ArrayList();
Field[] fs = cclz.getDeclaredFields();
if (fs != null && fs.length > 0) {
for (Field f : fs) {
if (!isIgnoreField(f)) {
fields.add(f);
}
}
}
Class> pclz = cclz.getSuperclass();
if (pclz != null && !pclz.isInterface()) {
List fds1 = getObjectAllFields(pclz);
if (fds1 != null && !fds1.isEmpty()) {
fields.addAll(fds1);
}
}
return fields;
}
public static Map getObjectAllFieldsMap(Class> cclz) {
Map fields = new HashMap();
Field[] fs = cclz.getDeclaredFields();
if (fs != null && fs.length > 0) {
for (Field f : fs) {
if (!isIgnoreField(f)) {
fields.put(f.getName(), f);
fields.put(f.getName().toUpperCase(), f);
}
}
}
Class> pclz = cclz.getSuperclass();
if (pclz != null && !pclz.isInterface()) {
Map fds1 = getObjectAllFieldsMap(pclz);
if (fds1 != null && !fds1.isEmpty()) {
fields.putAll(fds1);
}
}
return fields;
}
public static ModelFabric getModelFabric(String clzStr) {
return ModelCacheManager.get().getModelFabric(clzStr);
}
public static String getModelTable(Class> clz) {
final Table table = clz.getAnnotation(Table.class);
if (table != null) {
return table.value();
}
return StrKit.enCodeUnderlined(clz.getSimpleName());
}
public static ModelFabric getModelFabric(Class> clz) {
return ModelCacheManager.get().getModelFabric(clz);
}
public static String buildColumnsByExcluded(Class> clz, List excludedColumns) {
ModelFabric mf = getModelFabric(clz);
Map attrs = mf.getAttrMapping();
Set columns = new HashSet();
Set> kvsets = attrs.entrySet();
for (Entry entry : kvsets) {
if (excludedColumns.contains(entry.getKey()) || excludedColumns.contains(entry.getValue())
|| excludedColumns.contains(entry.getValue().toLowerCase())) {
continue;
}
columns.add(entry.getValue().toUpperCase());
}
StringBuilder sb = new StringBuilder();
columns.forEach(k -> sb.append(k).append(","));
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}
}