"...git@gitlab.schbrain.com:panwangnan/schbrain-parent.git" did not exist on "23255c7e36d894a0b90a77b0d11594cabf0fdced"
Commit 96095cb5 authored by liaozan's avatar liaozan 🏀

Update BizId to support any type

parent 714cefae
package com.schbrain.framework.autoconfigure.mybatis.annotation; package com.schbrain.framework.autoconfigure.mybatis.annotation;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdColumnField;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdType; import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdType;
import java.lang.annotation.*; import java.lang.annotation.*;
...@@ -16,7 +17,7 @@ public @interface BizId { ...@@ -16,7 +17,7 @@ public @interface BizId {
/** /**
* 逻辑主键列名,为空时取字段名 * 逻辑主键列名,为空时取字段名
* *
* @see com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField * @see BizIdColumnField
*/ */
String value() default ""; String value() default "";
...@@ -25,4 +26,4 @@ public @interface BizId { ...@@ -25,4 +26,4 @@ public @interface BizId {
*/ */
BizIdType type() default BizIdType.ID_WORKER; BizIdType type() default BizIdType.ID_WORKER;
} }
\ No newline at end of file
...@@ -29,31 +29,31 @@ public interface BaseService<T extends BaseEntity> extends IService<T> { ...@@ -29,31 +29,31 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/** /**
* 根据业务主键获取记录 * 根据业务主键获取记录
*/ */
T getByBizId(String bizId); T getByBizId(Object bizId);
/** /**
* 根据业务主键获取记录 * 根据业务主键获取记录
* *
* @param throwIfNotFound 未获取到记录时是否抛异常 * @param throwIfNotFound 未获取到记录时是否抛异常
*/ */
T getByBizId(String bizId, boolean throwIfNotFound); T getByBizId(Object bizId, boolean throwIfNotFound);
/** /**
* 根据业务主键获取记录 * 根据业务主键获取记录
* *
* @param notFoundSupplier 未获取到记录时是否抛异常 * @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 * 根据 id 更新,null 会被更新为 null
...@@ -70,4 +70,4 @@ public interface BaseService<T extends BaseEntity> extends IService<T> { ...@@ -70,4 +70,4 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
*/ */
boolean updateBatchByIdsWithNull(Collection<T> entityList, int batchSize); boolean updateBatchByIdsWithNull(Collection<T> entityList, int batchSize);
} }
\ No newline at end of file
...@@ -8,8 +8,8 @@ import com.schbrain.common.exception.BaseException; ...@@ -8,8 +8,8 @@ import com.schbrain.common.exception.BaseException;
import com.schbrain.common.util.StreamUtils; import com.schbrain.common.util.StreamUtils;
import com.schbrain.common.util.support.ValidateSupport; import com.schbrain.common.util.support.ValidateSupport;
import com.schbrain.framework.autoconfigure.mybatis.annotation.BizId; 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.biz.BizIdHelper;
import com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField;
import com.schbrain.framework.autoconfigure.mybatis.exception.NoSuchRecordException; import com.schbrain.framework.autoconfigure.mybatis.exception.NoSuchRecordException;
import org.apache.ibatis.binding.MapperMethod.ParamMap; import org.apache.ibatis.binding.MapperMethod.ParamMap;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
...@@ -60,12 +60,12 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte ...@@ -60,12 +60,12 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
} }
@Override @Override
public T getByBizId(String bizId) { public T getByBizId(Object bizId) {
return getByBizId(bizId, false); return getByBizId(bizId, false);
} }
@Override @Override
public T getByBizId(String bizId, boolean throwIfNotFound) { public T getByBizId(Object bizId, boolean throwIfNotFound) {
Supplier<? extends RuntimeException> notFoundSupplier = null; Supplier<? extends RuntimeException> notFoundSupplier = null;
if (throwIfNotFound) { if (throwIfNotFound) {
notFoundSupplier = () -> new NoSuchRecordException(entityClass); notFoundSupplier = () -> new NoSuchRecordException(entityClass);
...@@ -74,7 +74,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte ...@@ -74,7 +74,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
} }
@Override @Override
public T getByBizId(String bizId, Supplier<? extends RuntimeException> notFoundSupplier) { public T getByBizId(Object bizId, Supplier<? extends RuntimeException> notFoundSupplier) {
assertBidColumnFieldExist(); assertBidColumnFieldExist();
T entity = query().eq(bizIdColumnField.getColumnName(), bizId).one(); T entity = query().eq(bizIdColumnField.getColumnName(), bizId).one();
if (entity == null && notFoundSupplier != null) { if (entity == null && notFoundSupplier != null) {
...@@ -84,7 +84,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte ...@@ -84,7 +84,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
} }
@Override @Override
public List<T> listByBizIds(Collection<String> bizIds) { public <K> List<T> listByBizIds(Collection<K> bizIds) {
assertBidColumnFieldExist(); assertBidColumnFieldExist();
if (isEmpty(bizIds)) { if (isEmpty(bizIds)) {
return Collections.emptyList(); return Collections.emptyList();
...@@ -93,7 +93,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte ...@@ -93,7 +93,7 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
} }
@Override @Override
public Map<String, T> getMapByBizIds(Collection<String> bizIds) { public <K> Map<K, T> getMapByBizIds(Collection<K> bizIds) {
assertBidColumnFieldExist(); assertBidColumnFieldExist();
if (isEmpty(bizIds)) { if (isEmpty(bizIds)) {
return Collections.emptyMap(); return Collections.emptyMap();
...@@ -129,9 +129,6 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte ...@@ -129,9 +129,6 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
if (this.bizIdColumnField != null) { if (this.bizIdColumnField != null) {
throw new BaseException(String.format("@BizId can't more than one in Class: \"%s\"", entityClass.getName())); 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); this.bizIdColumnField = new BizIdColumnField(entityClass, bizId);
BizIdHelper.putBizColumnField(entityClass, bizIdColumnField); BizIdHelper.putBizColumnField(entityClass, bizIdColumnField);
}, field -> field.isAnnotationPresent(BizId.class)); }, field -> field.isAnnotationPresent(BizId.class));
...@@ -147,4 +144,4 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte ...@@ -147,4 +144,4 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
return mapperClass.getName() + StringPool.DOT + "alwaysUpdateSomeColumnById"; return mapperClass.getName() + StringPool.DOT + "alwaysUpdateSomeColumnById";
} }
} }
\ No newline at end of file
package com.schbrain.framework.autoconfigure.mybatis.core; package com.schbrain.framework.autoconfigure.mybatis.biz;
import com.schbrain.common.exception.BaseException; import com.schbrain.common.exception.BaseException;
import com.schbrain.framework.autoconfigure.mybatis.annotation.BizId; import com.schbrain.framework.autoconfigure.mybatis.annotation.BizId;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdHelper;
import lombok.Data; import lombok.Data;
import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandle;
...@@ -29,22 +28,25 @@ public class BizIdColumnField { ...@@ -29,22 +28,25 @@ public class BizIdColumnField {
this.annotation = bizIdField.getAnnotation(BizId.class); this.annotation = bizIdField.getAnnotation(BizId.class);
this.columnName = BizIdHelper.getColumnName(entityClass, bizIdField, this.annotation); this.columnName = BizIdHelper.getColumnName(entityClass, bizIdField, this.annotation);
try { try {
this.bizIdFieldGetterMethodHandle = privateLookupIn(entityClass, lookup()).findGetter(entityClass, bizIdField.getName(), String.class); Class<?> fieldType = bizIdField.getType();
this.bizIdFieldSetterMethodHandle = privateLookupIn(entityClass, lookup()).findSetter(entityClass, bizIdField.getName(), String.class); 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) { } catch (NoSuchFieldException | IllegalAccessException e) {
throw new BaseException(e.getMessage(), e); throw new BaseException(e.getMessage(), e);
} }
} }
public <T> String getValue(T entity) { @SuppressWarnings("unchecked")
public <V, T> V getValue(T entity) {
try { try {
return (String) bizIdFieldGetterMethodHandle.invoke(entity); return (V) bizIdFieldGetterMethodHandle.invoke(entity);
} catch (Throwable e) { } catch (Throwable e) {
throw new BaseException(e.getMessage(), 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 { try {
bizIdFieldSetterMethodHandle.invoke(entity, value); bizIdFieldSetterMethodHandle.invoke(entity, value);
} catch (Throwable e) { } catch (Throwable e) {
...@@ -52,4 +54,4 @@ public class BizIdColumnField { ...@@ -52,4 +54,4 @@ public class BizIdColumnField {
} }
} }
} }
\ No newline at end of file
...@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo; ...@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.schbrain.framework.autoconfigure.mybatis.annotation.BizId; import com.schbrain.framework.autoconfigure.mybatis.annotation.BizId;
import com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField;
import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.Configuration;
import java.lang.reflect.Field; import java.lang.reflect.Field;
...@@ -45,4 +44,4 @@ public class BizIdHelper { ...@@ -45,4 +44,4 @@ public class BizIdHelper {
return bizIdField.getName(); return bizIdField.getName();
} }
} }
\ No newline at end of file
package com.schbrain.framework.autoconfigure.mybatis.biz; package com.schbrain.framework.autoconfigure.mybatis.biz;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; 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.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.mapping.SqlCommandType;
...@@ -32,11 +30,11 @@ public class BizIdInjectInterceptor implements InnerInterceptor { ...@@ -32,11 +30,11 @@ public class BizIdInjectInterceptor implements InnerInterceptor {
return; return;
} }
if (bizIdType == BizIdType.ID_WORKER) { if (bizIdType == BizIdType.ID_WORKER) {
String bizIdValue = bizColumnField.getValue(entity); Object bizIdValue = bizColumnField.getValue(entity);
if (StringUtils.isBlank(bizIdValue)) { if (bizIdValue == null) {
bizColumnField.setValue(entity, bizIdType.generateBizId(entity)); bizColumnField.setValue(entity, bizIdType.generateBizId(entity));
} }
} }
} }
} }
\ No newline at end of file
...@@ -28,8 +28,8 @@ public enum BizIdType { ...@@ -28,8 +28,8 @@ public enum BizIdType {
/** /**
* 生成 bizId 的值 * 生成 bizId 的值
*/ */
public String generateBizId(Object entity) { public Object generateBizId(Object entity) {
return generator.generate(entity); return generator.generate(entity);
} }
} }
\ No newline at end of file
package com.schbrain.framework.autoconfigure.mybatis.constraint; package com.schbrain.framework.autoconfigure.mybatis.constraint;
import com.mysql.cj.MysqlType; 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.biz.BizIdHelper;
import com.schbrain.framework.autoconfigure.mybatis.core.BizIdColumnField;
import com.schbrain.framework.autoconfigure.mybatis.exception.TableConstraintException; import com.schbrain.framework.autoconfigure.mybatis.exception.TableConstraintException;
import static com.schbrain.framework.autoconfigure.mybatis.constant.MybatisConstants.*; import static com.schbrain.framework.autoconfigure.mybatis.constant.MybatisConstants.*;
...@@ -141,4 +141,4 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker { ...@@ -141,4 +141,4 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
table.addError(new TableConstraintException(table.getTableName(), columnName, errorMsg)); table.addError(new TableConstraintException(table.getTableName(), columnName, errorMsg));
} }
} }
\ No newline at end of file
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