mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
optimize CustomKeyGenerator.java
This commit is contained in:
parent
53e4980d7c
commit
82fd193aee
@ -21,7 +21,7 @@ import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.exception.FlexExceptions;
|
||||
import com.mybatisflex.core.table.IdInfo;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
import com.mybatisflex.core.util.ConvertUtil;
|
||||
import org.apache.ibatis.executor.Executor;
|
||||
import org.apache.ibatis.executor.keygen.KeyGenerator;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
@ -70,7 +70,7 @@ public class CustomKeyGenerator implements KeyGenerator {
|
||||
Object generateId = keyGenerator.generate(entity, idInfo.getColumn());
|
||||
try {
|
||||
Invoker setInvoker = tableInfo.getReflector().getSetInvoker(idInfo.getProperty());
|
||||
Object id = convert(generateId, setInvoker.getType());
|
||||
Object id = ConvertUtil.convert(generateId, setInvoker.getType());
|
||||
setInvoker.invoke(entity, new Object[]{id});
|
||||
} catch (Exception e) {
|
||||
throw FlexExceptions.wrap(e);
|
||||
@ -78,51 +78,6 @@ public class CustomKeyGenerator implements KeyGenerator {
|
||||
}
|
||||
|
||||
|
||||
public Object convert(Object value, Class<?> targetClass) {
|
||||
if (value == null || (value.getClass() == String.class && StringUtil.isBlank((String) value)
|
||||
&& targetClass != String.class)) {
|
||||
return null;
|
||||
}
|
||||
if (value.getClass().isAssignableFrom(targetClass)) {
|
||||
return value;
|
||||
}
|
||||
if (targetClass == String.class) {
|
||||
return value.toString();
|
||||
} else if (targetClass == Long.class || targetClass == long.class) {
|
||||
if (value instanceof Number) {
|
||||
return ((Number) value).longValue();
|
||||
}
|
||||
return Long.parseLong(value.toString());
|
||||
} else if (targetClass == java.math.BigInteger.class) {
|
||||
return new java.math.BigInteger(value.toString());
|
||||
} else if (targetClass == java.math.BigDecimal.class) {
|
||||
return new java.math.BigDecimal(value.toString());
|
||||
} else if (targetClass == Integer.class || targetClass == int.class) {
|
||||
if (value instanceof Number) {
|
||||
return ((Number) value).intValue();
|
||||
}
|
||||
return Integer.parseInt(value.toString());
|
||||
} else if (targetClass == Double.class || targetClass == double.class) {
|
||||
if (value instanceof Number) {
|
||||
return ((Number) value).doubleValue();
|
||||
}
|
||||
return Double.parseDouble(value.toString());
|
||||
} else if (targetClass == Float.class || targetClass == float.class) {
|
||||
if (value instanceof Number) {
|
||||
return ((Number) value).floatValue();
|
||||
}
|
||||
return Float.parseFloat(value.toString());
|
||||
} else if (targetClass == Short.class || targetClass == short.class) {
|
||||
if (value instanceof Number) {
|
||||
return ((Number) value).shortValue();
|
||||
}
|
||||
return Short.parseShort(value.toString());
|
||||
}
|
||||
|
||||
throw FlexExceptions.wrap("\"%s\" can not be parsed for primary key.", value);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void processAfter(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
|
||||
//do nothing
|
||||
|
||||
@ -17,6 +17,6 @@ package com.mybatisflex.core.keygen;
|
||||
|
||||
public interface IKeyGenerator {
|
||||
|
||||
Object generate(Object entity,String keyColumn);
|
||||
Object generate(Object entity, String keyColumn);
|
||||
|
||||
}
|
||||
|
||||
@ -30,7 +30,6 @@ public class KeyGeneratorFactory {
|
||||
* {@link com.mybatisflex.annotation.Id}
|
||||
*/
|
||||
register("uuid", new UUIDKeyGenerator());
|
||||
register("flex", new FlexIDKeyGenerator());
|
||||
register("flexId", new FlexIDKeyGenerator());
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user