Commit 96095cb5 authored by liaozan's avatar liaozan 🏀

Update BizId to support any type

parent 714cefae
package com.schbrain.framework.autoconfigure.mybatis.annotation;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdColumnField;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdType;
import java.lang.annotation.*;
......@@ -16,7 +17,7 @@ public @interface BizId {
/**
* 逻辑主键列名,为空时取字段名
*
* @see com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField
* @see BizIdColumnField
*/
String value() default "";
......
......@@ -29,31 +29,31 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
* 根据业务主键获取记录
*/
T getByBizId(String bizId);
T getByBizId(Object bizId);
/**
* 根据业务主键获取记录
*
* @param throwIfNotFound 未获取到记录时是否抛异常
*/
T getByBizId(String bizId, boolean throwIfNotFound);
T getByBizId(Object bizId, boolean throwIfNotFound);
/**
* 根据业务主键获取记录
*
* @param notFoundSupplier 未获取到记录时是否抛异常
*/
T getByBizId(String bizId, Supplier<? extends RuntimeException> notFoundSupplier);
T getByBizId(Object bizId, Supplier<? extends RuntimeException> notFoundSupplier);
/**
* 根据业务主键获取
*/
List<T> listByBizIds(Collection<String> bizIds);
<K> List<T> listByBizIds(Collection<K> bizIds);
/**
* 根据业务主键获取
*/
Map<String, T> getMapByBizIds(Collection<String> bizIds);
<K> Map<K, T> getMapByBizIds(Collection<K> bizIds);
/**
* 根据 id 更新,null 会被更新为 null
......
......@@ -8,8 +8,8 @@ import com.schbrain.common.exception.BaseException;
import com.schbrain.common.util.StreamUtils;
import com.schbrain.common.util.support.ValidateSupport;
import com.schbrain.framework.autoconfigure.mybatis.annotation.BizId;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdColumnField;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdHelper;
import com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField;
import com.schbrain.framework.autoconfigure.mybatis.exception.NoSuchRecordException;
import org.apache.ibatis.binding.MapperMethod.ParamMap;
import org.springframework.beans.factory.InitializingBean;
......@@ -60,12 +60,12 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public T getByBizId(String bizId) {
public T getByBizId(Object bizId) {
return getByBizId(bizId, false);
}
@Override
public T getByBizId(String bizId, boolean throwIfNotFound) {
public T getByBizId(Object bizId, boolean throwIfNotFound) {
Supplier<? extends RuntimeException> notFoundSupplier = null;
if (throwIfNotFound) {
notFoundSupplier = () -> new NoSuchRecordException(entityClass);
......@@ -74,7 +74,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public T getByBizId(String bizId, Supplier<? extends RuntimeException> notFoundSupplier) {
public T getByBizId(Object bizId, Supplier<? extends RuntimeException> notFoundSupplier) {
assertBidColumnFieldExist();
T entity = query().eq(bizIdColumnField.getColumnName(), bizId).one();
if (entity == null && notFoundSupplier != null) {
......@@ -84,7 +84,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public List<T> listByBizIds(Collection<String> bizIds) {
public <K> List<T> listByBizIds(Collection<K> bizIds) {
assertBidColumnFieldExist();
if (isEmpty(bizIds)) {
return Collections.emptyList();
......@@ -93,7 +93,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public Map<String, T> getMapByBizIds(Collection<String> bizIds) {
public <K> Map<K, T> getMapByBizIds(Collection<K> bizIds) {
assertBidColumnFieldExist();
if (isEmpty(bizIds)) {
return Collections.emptyMap();
......@@ -129,9 +129,6 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
if (this.bizIdColumnField != null) {
throw new BaseException(String.format("@BizId can't more than one in Class: \"%s\"", entityClass.getName()));
}
if (bizId.getType() != String.class) {
throw new BaseException("@BizId only support String field");
}
this.bizIdColumnField = new BizIdColumnField(entityClass, bizId);
BizIdHelper.putBizColumnField(entityClass, bizIdColumnField);
}, field -> field.isAnnotationPresent(BizId.class));
......
package com.schbrain.framework.autoconfigure.mybatis.core;
package com.schbrain.framework.autoconfigure.mybatis.biz;
import com.schbrain.common.exception.BaseException;
import com.schbrain.framework.autoconfigure.mybatis.annotation.BizId;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdHelper;
import lombok.Data;
import java.lang.invoke.MethodHandle;
......@@ -29,22 +28,25 @@ public class BizIdColumnField {
this.annotation = bizIdField.getAnnotation(BizId.class);
this.columnName = BizIdHelper.getColumnName(entityClass, bizIdField, this.annotation);
try {
this.bizIdFieldGetterMethodHandle = privateLookupIn(entityClass, lookup()).findGetter(entityClass, bizIdField.getName(), String.class);
this.bizIdFieldSetterMethodHandle = privateLookupIn(entityClass, lookup()).findSetter(entityClass, bizIdField.getName(), String.class);
Class<?> fieldType = bizIdField.getType();
Lookup lookup = privateLookupIn(entityClass, lookup());
this.bizIdFieldGetterMethodHandle = lookup.findGetter(entityClass, bizIdField.getName(), fieldType);
this.bizIdFieldSetterMethodHandle = lookup.findSetter(entityClass, bizIdField.getName(), fieldType);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new BaseException(e.getMessage(), e);
}
}
public <T> String getValue(T entity) {
@SuppressWarnings("unchecked")
public <V, T> V getValue(T entity) {
try {
return (String) bizIdFieldGetterMethodHandle.invoke(entity);
return (V) bizIdFieldGetterMethodHandle.invoke(entity);
} catch (Throwable e) {
throw new BaseException(e.getMessage(), e);
}
}
public <T> void setValue(T entity, String value) {
public <V, T> void setValue(T entity, V value) {
try {
bizIdFieldSetterMethodHandle.invoke(entity, value);
} catch (Throwable e) {
......
......@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.schbrain.framework.autoconfigure.mybatis.annotation.BizId;
import com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField;
import org.apache.ibatis.session.Configuration;
import java.lang.reflect.Field;
......
package com.schbrain.framework.autoconfigure.mybatis.biz;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
......@@ -32,8 +30,8 @@ public class BizIdInjectInterceptor implements InnerInterceptor {
return;
}
if (bizIdType == BizIdType.ID_WORKER) {
String bizIdValue = bizColumnField.getValue(entity);
if (StringUtils.isBlank(bizIdValue)) {
Object bizIdValue = bizColumnField.getValue(entity);
if (bizIdValue == null) {
bizColumnField.setValue(entity, bizIdType.generateBizId(entity));
}
}
......
......@@ -28,7 +28,7 @@ public enum BizIdType {
/**
* 生成 bizId 的值
*/
public String generateBizId(Object entity) {
public Object generateBizId(Object entity) {
return generator.generate(entity);
}
......
package com.schbrain.framework.autoconfigure.mybatis.constraint;
import com.mysql.cj.MysqlType;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdColumnField;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdHelper;
import com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField;
import com.schbrain.framework.autoconfigure.mybatis.exception.TableConstraintException;
import static com.schbrain.framework.autoconfigure.mybatis.constant.MybatisConstants.*;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment