Skip to content

Commit 816180d

Browse files
committed
Example支持TypeHandler配置
1 parent 86b5d4e commit 816180d

File tree

6 files changed

+226
-27
lines changed

6 files changed

+226
-27
lines changed

mapper/src/main/java/io/mybatis/mapper/example/Example.java

+58-21
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.mybatis.mapper.fn.Fn;
2121
import io.mybatis.provider.EntityColumn;
2222
import io.mybatis.provider.EntityTable;
23+
import org.apache.ibatis.type.TypeHandler;
2324

2425
import java.util.*;
2526
import java.util.function.Supplier;
@@ -474,7 +475,8 @@ public Example<T> set(String setSql) {
474475
* @param value 值
475476
*/
476477
public Example<T> set(Fn<T, Object> fn, Object value) {
477-
this.setValues.add(new Criterion(fn.toColumn(), value));
478+
EntityColumn column = fn.toEntityColumn();
479+
this.setValues.add(new Criterion(column.column(), value, column.typeHandler()));
478480
return this;
479481
}
480482

@@ -511,6 +513,10 @@ private String column(Fn<T, Object> fn) {
511513
return fn.toColumn();
512514
}
513515

516+
private Class<? extends TypeHandler> typehandler(Fn<T, Object> fn) {
517+
return fn.toEntityColumn().typeHandler();
518+
}
519+
514520
/**
515521
* 是否使用该条件
516522
* <br/>
@@ -539,13 +545,27 @@ protected void addCriterion(String condition, Object value) {
539545
criteria.add(new Criterion(condition, value));
540546
}
541547

548+
protected void addCriterion(String condition, Object value, Class<? extends TypeHandler> typeHandler) {
549+
if (value == null) {
550+
throw new RuntimeException("Value for " + condition + " cannot be null");
551+
}
552+
criteria.add(new Criterion(condition, value, typeHandler));
553+
}
554+
542555
protected void addCriterion(String condition, Object value1, Object value2) {
543556
if (value1 == null || value2 == null) {
544557
throw new RuntimeException("Between values for " + condition + " cannot be null");
545558
}
546559
criteria.add(new Criterion(condition, value1, value2));
547560
}
548561

562+
protected void addCriterion(String condition, Object value1, Object value2, Class<? extends TypeHandler> typeHandler) {
563+
if (value1 == null || value2 == null) {
564+
throw new RuntimeException("Between values for " + condition + " cannot be null");
565+
}
566+
criteria.add(new Criterion(condition, value1, value2, typeHandler));
567+
}
568+
549569
public Criteria<T> andIsNull(boolean useCondition, Fn<T, Object> fn) {
550570
return useCondition ? andIsNull(fn) : (Criteria<T>) this;
551571
}
@@ -570,7 +590,7 @@ public Criteria<T> andEqualTo(boolean useCondition, Fn<T, Object> fn, Object val
570590

571591
public Criteria<T> andEqualTo(Fn<T, Object> fn, Object value) {
572592
if (useCriterion(value)) {
573-
addCriterion(column(fn) + " =", value);
593+
addCriterion(column(fn) + " =", value, typehandler(fn));
574594
}
575595
return (Criteria<T>) this;
576596
}
@@ -581,7 +601,7 @@ public Criteria<T> andNotEqualTo(boolean useCondition, Fn<T, Object> fn, Object
581601

582602
public Criteria<T> andNotEqualTo(Fn<T, Object> fn, Object value) {
583603
if (useCriterion(value)) {
584-
addCriterion(column(fn) + " <>", value);
604+
addCriterion(column(fn) + " <>", value, typehandler(fn));
585605
}
586606
return (Criteria<T>) this;
587607
}
@@ -592,7 +612,7 @@ public Criteria<T> andGreaterThan(boolean useCondition, Fn<T, Object> fn, Object
592612

593613
public Criteria<T> andGreaterThan(Fn<T, Object> fn, Object value) {
594614
if (useCriterion(value)) {
595-
addCriterion(column(fn) + " >", value);
615+
addCriterion(column(fn) + " >", value, typehandler(fn));
596616
}
597617
return (Criteria<T>) this;
598618
}
@@ -603,7 +623,7 @@ public Criteria<T> andGreaterThanOrEqualTo(boolean useCondition, Fn<T, Object> f
603623

604624
public Criteria<T> andGreaterThanOrEqualTo(Fn<T, Object> fn, Object value) {
605625
if (useCriterion(value)) {
606-
addCriterion(column(fn) + " >=", value);
626+
addCriterion(column(fn) + " >=", value, typehandler(fn));
607627
}
608628
return (Criteria<T>) this;
609629
}
@@ -614,7 +634,7 @@ public Criteria<T> andLessThan(boolean useCondition, Fn<T, Object> fn, Object va
614634

615635
public Criteria<T> andLessThan(Fn<T, Object> fn, Object value) {
616636
if (useCriterion(value)) {
617-
addCriterion(column(fn) + " <", value);
637+
addCriterion(column(fn) + " <", value, typehandler(fn));
618638
}
619639
return (Criteria<T>) this;
620640
}
@@ -625,7 +645,7 @@ public Criteria<T> andLessThanOrEqualTo(boolean useCondition, Fn<T, Object> fn,
625645

626646
public Criteria<T> andLessThanOrEqualTo(Fn<T, Object> fn, Object value) {
627647
if (useCriterion(value)) {
628-
addCriterion(column(fn) + " <=", value);
648+
addCriterion(column(fn) + " <=", value, typehandler(fn));
629649
}
630650
return (Criteria<T>) this;
631651
}
@@ -637,7 +657,7 @@ public Criteria<T> andIn(boolean useCondition, Fn<T, Object> fn, Iterable values
637657
@SuppressWarnings("rawtypes")
638658
public Criteria<T> andIn(Fn<T, Object> fn, Iterable values) {
639659
if (useCriterion(values)) {
640-
addCriterion(column(fn) + " IN", values);
660+
addCriterion(column(fn) + " IN", values, typehandler(fn));
641661
}
642662
return (Criteria<T>) this;
643663
}
@@ -649,7 +669,7 @@ public Criteria<T> andNotIn(boolean useCondition, Fn<T, Object> fn, Iterable val
649669
@SuppressWarnings("rawtypes")
650670
public Criteria<T> andNotIn(Fn<T, Object> fn, Iterable values) {
651671
if (useCriterion(values)) {
652-
addCriterion(column(fn) + " NOT IN", values);
672+
addCriterion(column(fn) + " NOT IN", values, typehandler(fn));
653673
}
654674
return (Criteria<T>) this;
655675
}
@@ -660,7 +680,7 @@ public Criteria<T> andBetween(boolean useCondition, Fn<T, Object> fn, Object val
660680

661681
public Criteria<T> andBetween(Fn<T, Object> fn, Object value1, Object value2) {
662682
if (useCriterion(value1) && useCriterion(value2)) {
663-
addCriterion(column(fn) + " BETWEEN", value1, value2);
683+
addCriterion(column(fn) + " BETWEEN", value1, value2, typehandler(fn));
664684
}
665685
return (Criteria<T>) this;
666686
}
@@ -671,29 +691,29 @@ public Criteria<T> andNotBetween(boolean useCondition, Fn<T, Object> fn, Object
671691

672692
public Criteria<T> andNotBetween(Fn<T, Object> fn, Object value1, Object value2) {
673693
if (useCriterion(value1) && useCriterion(value2)) {
674-
addCriterion(column(fn) + " NOT BETWEEN", value1, value2);
694+
addCriterion(column(fn) + " NOT BETWEEN", value1, value2, typehandler(fn));
675695
}
676696
return (Criteria<T>) this;
677697
}
678698

679-
public Criteria<T> andLike(boolean useCondition, Fn<T, Object> fn, String value) {
699+
public Criteria<T> andLike(boolean useCondition, Fn<T, Object> fn, Object value) {
680700
return useCondition ? andLike(fn, value) : (Criteria<T>) this;
681701
}
682702

683-
public Criteria<T> andLike(Fn<T, Object> fn, String value) {
703+
public Criteria<T> andLike(Fn<T, Object> fn, Object value) {
684704
if (useCriterion(value)) {
685-
addCriterion(column(fn) + " LIKE", value);
705+
addCriterion(column(fn) + " LIKE", value, typehandler(fn));
686706
}
687707
return (Criteria<T>) this;
688708
}
689709

690-
public Criteria<T> andNotLike(boolean useCondition, Fn<T, Object> fn, String value) {
710+
public Criteria<T> andNotLike(boolean useCondition, Fn<T, Object> fn, Object value) {
691711
return useCondition ? andNotLike(fn, value) : (Criteria<T>) this;
692712
}
693713

694-
public Criteria<T> andNotLike(Fn<T, Object> fn, String value) {
714+
public Criteria<T> andNotLike(Fn<T, Object> fn, Object value) {
695715
if (useCriterion(value)) {
696-
addCriterion(column(fn) + " NOT LIKE", value);
716+
addCriterion(column(fn) + " NOT LIKE", value, typehandler(fn));
697717
}
698718
return (Criteria<T>) this;
699719
}
@@ -857,13 +877,13 @@ public OrCriteria<T> andNotBetween(Fn<T, Object> fn, Object value1, Object value
857877
}
858878

859879
@Override
860-
public OrCriteria<T> andLike(Fn<T, Object> fn, String value) {
880+
public OrCriteria<T> andLike(Fn<T, Object> fn, Object value) {
861881
super.andLike(fn, value);
862882
return this;
863883
}
864884

865885
@Override
866-
public OrCriteria<T> andNotLike(Fn<T, Object> fn, String value) {
886+
public OrCriteria<T> andNotLike(Fn<T, Object> fn, Object value) {
867887
super.andNotLike(fn, value);
868888
return this;
869889
}
@@ -1311,6 +1331,8 @@ public static class Criterion {
13111331

13121332
private Object secondValue;
13131333

1334+
private String typeHandler;
1335+
13141336
private boolean noValue;
13151337

13161338
private boolean singleValue;
@@ -1331,10 +1353,11 @@ protected Criterion(String condition) {
13311353
this.noValue = true;
13321354
}
13331355

1334-
protected Criterion(String condition, Object value, String typeHandler) {
1356+
protected Criterion(String condition, Object value, Class<? extends TypeHandler> typeHandler) {
13351357
super();
13361358
this.condition = condition;
13371359
this.value = value;
1360+
this.typeHandler = typeHandler != null ? typeHandler.getName() : null;
13381361
if (value instanceof Collection<?>) {
13391362
if (condition != null) {
13401363
this.listValue = true;
@@ -1346,18 +1369,32 @@ protected Criterion(String condition, Object value, String typeHandler) {
13461369
}
13471370
}
13481371

1349-
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
1372+
protected Criterion(String condition, Object value, Object secondValue, Class<? extends TypeHandler> typeHandler) {
13501373
super();
13511374
this.condition = condition;
13521375
this.value = value;
13531376
this.secondValue = secondValue;
1377+
this.typeHandler = typeHandler != null ? typeHandler.getName() : null;
13541378
this.betweenValue = true;
13551379
}
13561380

13571381
protected Criterion(String condition, Object value, Object secondValue) {
13581382
this(condition, value, secondValue, null);
13591383
}
13601384

1385+
public String variables(String prefix, String field) {
1386+
StringBuilder variables = new StringBuilder();
1387+
variables.append("#{");
1388+
if(prefix != null && !prefix.isEmpty()) {
1389+
variables.append(prefix).append(".");
1390+
}
1391+
variables.append(field);
1392+
if (typeHandler != null && !typeHandler.isEmpty()) {
1393+
variables.append(",typeHandler=").append(typeHandler);
1394+
}
1395+
return variables.append("}").toString();
1396+
}
1397+
13611398
public String getCondition() {
13621399
return condition;
13631400
}

mapper/src/main/java/io/mybatis/mapper/example/ExampleProvider.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class ExampleProvider {
3737
" ${setValue.condition},\n" +
3838
" </when>\n" +
3939
" <when test=\"setValue.singleValue\">\n" +
40-
" ${setValue.condition} = #{setValue.value},\n" +
40+
" ${setValue.condition} = ${setValue.variables('setValue', 'value')},\n" +
4141
" </when>\n" +
4242
" </choose>\n" +
4343
" </foreach>\n" +
@@ -48,17 +48,17 @@ public class ExampleProvider {
4848
" AND ${criterion.condition}\n" +
4949
" </when>\n" +
5050
" <when test=\"criterion.singleValue\">\n" +
51-
" AND ${criterion.condition} #{criterion.value}\n" +
51+
" AND ${criterion.condition} ${criterion.variables('criterion', 'value')}\n" +
5252
" </when>\n" +
5353
" <when test=\"criterion.betweenValue\">\n" +
54-
" AND ${criterion.condition} #{criterion.value} AND\n" +
55-
" #{criterion.secondValue}\n" +
54+
" AND ${criterion.condition} ${criterion.variables('criterion', 'value')} AND\n" +
55+
" ${criterion.variables('criterion', 'secondValue')}\n" +
5656
" </when>\n" +
5757
" <when test=\"criterion.listValue\">\n" +
5858
" AND ${criterion.condition}\n" +
5959
" <foreach close=\")\" collection=\"criterion.value\" item=\"listItem\"\n" +
6060
" open=\"(\" separator=\",\">\n" +
61-
" #{listItem}\n" +
61+
" ${criterion.variables(null, 'listItem')}\n" +
6262
" </foreach>\n" +
6363
" </when>\n";
6464

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2020-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.mybatis.mapper;
18+
19+
import io.mybatis.mapper.model.UserAuto;
20+
21+
public interface UserAutoBaseMapper extends BaseMapper<UserAuto, Long>{
22+
23+
}

0 commit comments

Comments
 (0)