Skip to content

Commit

Permalink
feat: 解决typeHandler实例化参数无实际类型的问题 close #113
Browse files Browse the repository at this point in the history
  • Loading branch information
L1yp committed Jun 25, 2024
1 parent a1a7c82 commit 56a6e78
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions mapper/src/main/java/io/mybatis/mapper/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public Example<T> set(String setSql) {
*/
public Example<T> set(Fn<T, Object> fn, Object value) {
EntityColumn column = fn.toEntityColumn();
this.setValues.add(new Criterion(column.column(), value, column.typeHandler()));
this.setValues.add(new Criterion(column.column(), value, column));
return this;
}

Expand Down Expand Up @@ -545,11 +545,11 @@ protected void addCriterion(String condition, Object value) {
criteria.add(new Criterion(condition, value));
}

protected void addCriterion(String condition, Object value, Class<? extends TypeHandler> typeHandler) {
protected void addCriterion(String condition, Object value, EntityColumn column) {
if (value == null) {
throw new RuntimeException("Value for " + condition + " cannot be null");
}
criteria.add(new Criterion(condition, value, typeHandler));
criteria.add(new Criterion(condition, value, column));
}

protected void addCriterion(String condition, Object value1, Object value2) {
Expand All @@ -559,11 +559,11 @@ protected void addCriterion(String condition, Object value1, Object value2) {
criteria.add(new Criterion(condition, value1, value2));
}

protected void addCriterion(String condition, Object value1, Object value2, Class<? extends TypeHandler> typeHandler) {
protected void addCriterion(String condition, Object value1, Object value2, EntityColumn column) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + condition + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2, typeHandler));
criteria.add(new Criterion(condition, value1, value2, column));
}

public Criteria<T> andIsNull(boolean useCondition, Fn<T, Object> fn) {
Expand All @@ -590,7 +590,7 @@ public Criteria<T> andEqualTo(boolean useCondition, Fn<T, Object> fn, Object val

public Criteria<T> andEqualTo(Fn<T, Object> fn, Object value) {
if (useCriterion(value)) {
addCriterion(column(fn) + " =", value, typehandler(fn));
addCriterion(column(fn) + " =", value, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -601,7 +601,7 @@ public Criteria<T> andNotEqualTo(boolean useCondition, Fn<T, Object> fn, Object

public Criteria<T> andNotEqualTo(Fn<T, Object> fn, Object value) {
if (useCriterion(value)) {
addCriterion(column(fn) + " <>", value, typehandler(fn));
addCriterion(column(fn) + " <>", value, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -612,7 +612,7 @@ public Criteria<T> andGreaterThan(boolean useCondition, Fn<T, Object> fn, Object

public Criteria<T> andGreaterThan(Fn<T, Object> fn, Object value) {
if (useCriterion(value)) {
addCriterion(column(fn) + " >", value, typehandler(fn));
addCriterion(column(fn) + " >", value, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -623,7 +623,7 @@ public Criteria<T> andGreaterThanOrEqualTo(boolean useCondition, Fn<T, Object> f

public Criteria<T> andGreaterThanOrEqualTo(Fn<T, Object> fn, Object value) {
if (useCriterion(value)) {
addCriterion(column(fn) + " >=", value, typehandler(fn));
addCriterion(column(fn) + " >=", value, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -634,7 +634,7 @@ public Criteria<T> andLessThan(boolean useCondition, Fn<T, Object> fn, Object va

public Criteria<T> andLessThan(Fn<T, Object> fn, Object value) {
if (useCriterion(value)) {
addCriterion(column(fn) + " <", value, typehandler(fn));
addCriterion(column(fn) + " <", value, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -645,7 +645,7 @@ public Criteria<T> andLessThanOrEqualTo(boolean useCondition, Fn<T, Object> fn,

public Criteria<T> andLessThanOrEqualTo(Fn<T, Object> fn, Object value) {
if (useCriterion(value)) {
addCriterion(column(fn) + " <=", value, typehandler(fn));
addCriterion(column(fn) + " <=", value, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -657,7 +657,7 @@ public Criteria<T> andIn(boolean useCondition, Fn<T, Object> fn, Iterable values
@SuppressWarnings("rawtypes")
public Criteria<T> andIn(Fn<T, Object> fn, Iterable values) {
if (useCriterion(values)) {
addCriterion(column(fn) + " IN", values, typehandler(fn));
addCriterion(column(fn) + " IN", values, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -669,7 +669,7 @@ public Criteria<T> andNotIn(boolean useCondition, Fn<T, Object> fn, Iterable val
@SuppressWarnings("rawtypes")
public Criteria<T> andNotIn(Fn<T, Object> fn, Iterable values) {
if (useCriterion(values)) {
addCriterion(column(fn) + " NOT IN", values, typehandler(fn));
addCriterion(column(fn) + " NOT IN", values, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -680,7 +680,7 @@ public Criteria<T> andBetween(boolean useCondition, Fn<T, Object> fn, Object val

public Criteria<T> andBetween(Fn<T, Object> fn, Object value1, Object value2) {
if (useCriterion(value1) && useCriterion(value2)) {
addCriterion(column(fn) + " BETWEEN", value1, value2, typehandler(fn));
addCriterion(column(fn) + " BETWEEN", value1, value2, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -691,7 +691,7 @@ public Criteria<T> andNotBetween(boolean useCondition, Fn<T, Object> fn, Object

public Criteria<T> andNotBetween(Fn<T, Object> fn, Object value1, Object value2) {
if (useCriterion(value1) && useCriterion(value2)) {
addCriterion(column(fn) + " NOT BETWEEN", value1, value2, typehandler(fn));
addCriterion(column(fn) + " NOT BETWEEN", value1, value2, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -702,7 +702,7 @@ public Criteria<T> andLike(boolean useCondition, Fn<T, Object> fn, Object value)

public Criteria<T> andLike(Fn<T, Object> fn, Object value) {
if (useCriterion(value)) {
addCriterion(column(fn) + " LIKE", value, typehandler(fn));
addCriterion(column(fn) + " LIKE", value, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand All @@ -713,7 +713,7 @@ public Criteria<T> andNotLike(boolean useCondition, Fn<T, Object> fn, Object val

public Criteria<T> andNotLike(Fn<T, Object> fn, Object value) {
if (useCriterion(value)) {
addCriterion(column(fn) + " NOT LIKE", value, typehandler(fn));
addCriterion(column(fn) + " NOT LIKE", value, fn.toEntityColumn());
}
return (Criteria<T>) this;
}
Expand Down Expand Up @@ -1331,6 +1331,8 @@ public static class Criterion {

private Object secondValue;

private String javaType;

private String typeHandler;

private boolean noValue;
Expand All @@ -1353,11 +1355,12 @@ protected Criterion(String condition) {
this.noValue = true;
}

protected Criterion(String condition, Object value, Class<? extends TypeHandler> typeHandler) {
protected Criterion(String condition, Object value, EntityColumn column) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler != null ? typeHandler.getName() : null;
this.javaType = value != null ? column.javaType().getName() : null;
this.typeHandler = typeHandler != null ? column.typeHandler().getName() : null;
if (value instanceof Collection<?>) {
if (condition != null) {
this.listValue = true;
Expand All @@ -1369,12 +1372,13 @@ protected Criterion(String condition, Object value, Class<? extends TypeHandler>
}
}

protected Criterion(String condition, Object value, Object secondValue, Class<? extends TypeHandler> typeHandler) {
protected Criterion(String condition, Object value, Object secondValue, EntityColumn column) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler != null ? typeHandler.getName() : null;
this.javaType = value != null ? column.javaType().getName() : null;
this.typeHandler = typeHandler != null ? column.typeHandler().getName() : null;
this.betweenValue = true;
}

Expand All @@ -1385,6 +1389,9 @@ protected Criterion(String condition, Object value, Object secondValue) {
public String variables(String field) {
StringBuilder variables = new StringBuilder();
variables.append("#{").append(field);
if (javaType != null && !javaType.isEmpty()) {
variables.append(",javaType=").append(javaType);
}
if (typeHandler != null && !typeHandler.isEmpty()) {
variables.append(",typeHandler=").append(typeHandler);
}
Expand Down

0 comments on commit 56a6e78

Please sign in to comment.