This commit is contained in:
yulichang 2021-02-01 02:06:14 +08:00
parent 99890d94dd
commit f5d7aff146
12 changed files with 74 additions and 154 deletions

View File

@ -2,7 +2,6 @@ package com.github.mybatisplus.base;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.mybatisplus.wrapper.MyLambdaQueryWrapper;
import com.github.mybatisplus.wrapper.MyWrapper;
import java.util.List;

View File

@ -9,6 +9,7 @@ import java.util.List;
/**
* SQL 注入器
*
* @author yulichang
* @see DefaultSqlInjector
*/
public class MySqlInjector extends DefaultSqlInjector {

View File

@ -30,7 +30,7 @@ public enum SqlMethod {
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>"),
SELECT_JOIN_MAPS_PAGE("selectJoinMapsPage", "返回Map集合并分页",
"<script>\nSELECT %s FROM %s <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>");
"<script>\nSELECT %s FROM %s <if test=\"ew.alias != null and ew.alias != ''\">${ew.alias}</if> <if test=\"ew.from != null and ew.from != ''\">${ew.from}</if> %s %s\n</script>");
private final String method;
private final String desc;

View File

@ -2,6 +2,9 @@ package com.github.mybatisplus.query.interfaces;
import com.github.mybatisplus.toolkit.Constant;
/**
* @author yulichang
*/
public interface MyJoin<Children> {
default Children leftJoin(String joinSql) {
@ -16,7 +19,7 @@ public interface MyJoin<Children> {
return rightJoin(true, joinSql);
}
default Children rightJoin(boolean condition, String joinSql){
default Children rightJoin(boolean condition, String joinSql) {
return join(Constant.RIGHT_JOIN, condition, joinSql);
}
@ -24,7 +27,7 @@ public interface MyJoin<Children> {
return innerJoin(true, joinSql);
}
default Children innerJoin(boolean condition, String joinSql){
default Children innerJoin(boolean condition, String joinSql) {
return join(Constant.INNER_JOIN, condition, joinSql);
}

View File

@ -11,6 +11,8 @@ public interface Constant {
*/
String TABLE_ALIAS = "t";
String AS = " AS ";
String ON = " ON ";
String EQUALS = " = ";
@ -39,4 +41,5 @@ public interface Constant {
* " INNER JOIN "
*/
String INNER_JOIN = StringPool.SPACE + INNER + StringPool.SPACE + JOIN + StringPool.SPACE;
}

View File

@ -1,18 +1,3 @@
/*
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mybatisplus.wrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
@ -33,11 +18,7 @@ import java.util.Map;
import static java.util.stream.Collectors.joining;
/**
* Lambda 语法使用 Wrapper
* <p>统一处理解析 lambda 获取 column</p>
*
* @author hubin miemie HCL
* @since 2017-05-26
* copy {@link com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper}
*/
@SuppressWarnings("serial")
public abstract class MyAbstractLambdaWrapper<T, Children extends MyAbstractLambdaWrapper<T, Children>>

View File

@ -1,18 +1,3 @@
/*
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mybatisplus.wrapper;
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
@ -43,10 +28,7 @@ import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY;
import static java.util.stream.Collectors.joining;
/**
* 查询条件封装
*
* @author hubin miemie HCL
* @since 2017-05-26
* copy {@link com.baomidou.mybatisplus.core.conditions.AbstractWrapper}
*/
@SuppressWarnings({"serial", "unchecked"})
public abstract class MyAbstractWrapper<T, Children extends MyAbstractWrapper<T, Children>> extends MyWrapper<T>

View File

@ -1,18 +1,3 @@
/*
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mybatisplus.wrapper;
import com.baomidou.mybatisplus.core.conditions.SharedString;
@ -25,10 +10,10 @@ import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.mybatisplus.wrapper.interfaces.MyLambdaJoin;
import com.github.mybatisplus.wrapper.interfaces.MySFunctionQuery;
import com.github.mybatisplus.toolkit.Constant;
import com.github.mybatisplus.toolkit.MyLambdaUtils;
import com.github.mybatisplus.wrapper.interfaces.MyLambdaJoin;
import com.github.mybatisplus.wrapper.interfaces.MySFunctionQuery;
import java.util.ArrayList;
import java.util.List;
@ -38,10 +23,7 @@ import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Lambda 语法使用 Wrapper
*
* @author hubin miemie HCL
* @since 2017-05-26
* copy {@link com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper}
*/
@SuppressWarnings("serial")
public class MyLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyLambdaQueryWrapper<T>>
@ -57,7 +39,6 @@ public class MyLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyLambda
*/
private SharedString from = new SharedString();
/**
* 主表别名
*/
@ -118,7 +99,7 @@ public class MyLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyLambda
for (SFunction<S, ?> s : columns) {
Class<S> clazz = MyLambdaUtils.getEntityClass(s);
TableInfo info = TableInfoHelper.getTableInfo(clazz);
selectColumns.add(new SelectColumn(clazz, info.getTableName(), MyLambdaUtils.getColumn(s)));
selectColumns.add(new SelectColumn(clazz, info.getTableName(), MyLambdaUtils.getColumn(s), null));
}
}
return typedThis;
@ -129,15 +110,25 @@ public class MyLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyLambda
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
Assert.notNull(info, "table can not be find");
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
i -> selectColumns.add(new SelectColumn(entityClass, info.getTableName(), i.getColumn())));
i -> selectColumns.add(new SelectColumn(entityClass, info.getTableName(), i.getColumn(), null)));
return typedThis;
}
public final <S, X> MyLambdaQueryWrapper<T> selectAs(SFunction<S, ?> columns, SFunction<X, ?> alias) {
Class<S> clazz = MyLambdaUtils.getEntityClass(columns);
TableInfo info = TableInfoHelper.getTableInfo(clazz);
selectColumns.add(new SelectColumn(clazz, info.getTableName(), MyLambdaUtils.getColumn(columns), MyLambdaUtils.getName(alias)));
return typedThis;
}
@Override
public String getSqlSelect() {
if (StringUtils.isBlank(sqlSelect.getStringValue())) {
String s = selectColumns.stream().map(i -> i.getTableName() + StringPool.DOT + i.getColumnName()).collect(Collectors.joining(StringPool.COMMA));
String s = selectColumns.stream().map(i ->
i.getTableName() + StringPool.DOT + i.getColumnName() +
(StringUtils.isBlank(i.getAlias()) ? StringPool.EMPTY : (Constant.AS + i.getAlias())))
.collect(Collectors.joining(StringPool.COMMA));
sqlSelect.setStringValue(s);
}
return sqlSelect.getStringValue();
@ -198,14 +189,15 @@ public class MyLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyLambda
private String columnName;
private String alias;
public SelectColumn(Class<?> clazz, String tableName, String columnName) {
public SelectColumn(Class<?> clazz, String tableName, String columnName, String alias) {
this.clazz = clazz;
this.tableName = tableName;
this.columnName = columnName;
this.alias = alias;
}
public Class<?> getClazz() {
return clazz;
}
@ -229,5 +221,13 @@ public class MyLambdaQueryWrapper<T> extends MyAbstractLambdaWrapper<T, MyLambda
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
}
}

View File

@ -1,18 +1,3 @@
/*
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mybatisplus.wrapper.interfaces;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
@ -22,25 +7,21 @@ import java.util.Map;
import java.util.function.BiPredicate;
/**
* 查询条件封装
* <p>比较值</p>
*
* @author hubin miemie HCL
* @since 2017-05-26
* {@link com.baomidou.mybatisplus.core.conditions.interfaces.Compare}
*/
public interface MyCompare<Children> extends Serializable {
/**
* ignore
*/
default <R, V> Children allEq(Map<SFunction<R,?>, V> params) {
default <R, V> Children allEq(Map<SFunction<R, ?>, V> params) {
return allEq(params, true);
}
/**
* ignore
*/
default <R, V> Children allEq(Map<SFunction<R,?>, V> params, boolean null2IsNull) {
default <R, V> Children allEq(Map<SFunction<R, ?>, V> params, boolean null2IsNull) {
return allEq(true, params, null2IsNull);
}
@ -52,19 +33,19 @@ public interface MyCompare<Children> extends Serializable {
* @param null2IsNull 是否参数为 null 自动执行 isNull 方法, false 则忽略这个字段\
* @return children
*/
<R, V> Children allEq(boolean condition, Map<SFunction<R,?>, V> params, boolean null2IsNull);
<R, V> Children allEq(boolean condition, Map<SFunction<R, ?>, V> params, boolean null2IsNull);
/**
* ignore
*/
default <R, V> Children allEq(BiPredicate<SFunction<R,?>, V> filter, Map<SFunction<R,?>, V> params) {
default <R, V> Children allEq(BiPredicate<SFunction<R, ?>, V> filter, Map<SFunction<R, ?>, V> params) {
return allEq(filter, params, true);
}
/**
* ignore
*/
default <R, V> Children allEq(BiPredicate<SFunction<R,?>, V> filter, Map<SFunction<R,?>, V> params, boolean null2IsNull) {
default <R, V> Children allEq(BiPredicate<SFunction<R, ?>, V> filter, Map<SFunction<R, ?>, V> params, boolean null2IsNull) {
return allEq(true, filter, params, null2IsNull);
}
@ -77,12 +58,12 @@ public interface MyCompare<Children> extends Serializable {
* @param null2IsNull 是否参数为 null 自动执行 isNull 方法, false 则忽略这个字段
* @return children
*/
<R, V> Children allEq(boolean condition, BiPredicate<SFunction<R,?>, V> filter, Map<SFunction<R,?>, V> params, boolean null2IsNull);
<R, V> Children allEq(boolean condition, BiPredicate<SFunction<R, ?>, V> filter, Map<SFunction<R, ?>, V> params, boolean null2IsNull);
/**
* ignore
*/
default <R> Children eq(SFunction<R,?> column, Object val) {
default <R> Children eq(SFunction<R, ?> column, Object val) {
return eq(true, column, val);
}
@ -94,12 +75,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val
* @return children
*/
<R> Children eq(boolean condition, SFunction<R,?> column, Object val);
<R> Children eq(boolean condition, SFunction<R, ?> column, Object val);
/**
* ignore
*/
default <R> Children ne(SFunction<R,?> column, Object val) {
default <R> Children ne(SFunction<R, ?> column, Object val) {
return ne(true, column, val);
}
@ -111,12 +92,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val
* @return children
*/
<R> Children ne(boolean condition, SFunction<R,?> column, Object val);
<R> Children ne(boolean condition, SFunction<R, ?> column, Object val);
/**
* ignore
*/
default <R> Children gt(SFunction<R,?> column, Object val) {
default <R> Children gt(SFunction<R, ?> column, Object val) {
return gt(true, column, val);
}
@ -128,12 +109,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val
* @return children
*/
<R> Children gt(boolean condition, SFunction<R,?> column, Object val);
<R> Children gt(boolean condition, SFunction<R, ?> column, Object val);
/**
* ignore
*/
default <R> Children ge(SFunction<R,?> column, Object val) {
default <R> Children ge(SFunction<R, ?> column, Object val) {
return ge(true, column, val);
}
@ -145,12 +126,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val
* @return children
*/
<R> Children ge(boolean condition, SFunction<R,?> column, Object val);
<R> Children ge(boolean condition, SFunction<R, ?> column, Object val);
/**
* ignore
*/
default <R> Children lt(SFunction<R,?> column, Object val) {
default <R> Children lt(SFunction<R, ?> column, Object val) {
return lt(true, column, val);
}
@ -162,12 +143,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val
* @return children
*/
<R> Children lt(boolean condition, SFunction<R,?> column, Object val);
<R> Children lt(boolean condition, SFunction<R, ?> column, Object val);
/**
* ignore
*/
default <R> Children le(SFunction<R,?> column, Object val) {
default <R> Children le(SFunction<R, ?> column, Object val) {
return le(true, column, val);
}
@ -179,12 +160,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val
* @return children
*/
<R> Children le(boolean condition, SFunction<R,?> column, Object val);
<R> Children le(boolean condition, SFunction<R, ?> column, Object val);
/**
* ignore
*/
default <R> Children between(SFunction<R,?> column, Object val1, Object val2) {
default <R> Children between(SFunction<R, ?> column, Object val1, Object val2) {
return between(true, column, val1, val2);
}
@ -197,12 +178,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val2 值2
* @return children
*/
<R> Children between(boolean condition, SFunction<R,?> column, Object val1, Object val2);
<R> Children between(boolean condition, SFunction<R, ?> column, Object val1, Object val2);
/**
* ignore
*/
default <R> Children notBetween(SFunction<R,?> column, Object val1, Object val2) {
default <R> Children notBetween(SFunction<R, ?> column, Object val1, Object val2) {
return notBetween(true, column, val1, val2);
}
@ -215,12 +196,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val2 值2
* @return children
*/
<R> Children notBetween(boolean condition, SFunction<R,?> column, Object val1, Object val2);
<R> Children notBetween(boolean condition, SFunction<R, ?> column, Object val1, Object val2);
/**
* ignore
*/
default <R> Children like(SFunction<R,?> column, Object val) {
default <R> Children like(SFunction<R, ?> column, Object val) {
return like(true, column, val);
}
@ -232,12 +213,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val
* @return children
*/
<R> Children like(boolean condition, SFunction<R,?> column, Object val);
<R> Children like(boolean condition, SFunction<R, ?> column, Object val);
/**
* ignore
*/
default <R> Children notLike(SFunction<R,?> column, Object val) {
default <R> Children notLike(SFunction<R, ?> column, Object val) {
return notLike(true, column, val);
}
@ -249,12 +230,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val
* @return children
*/
<R> Children notLike(boolean condition, SFunction<R,?> column, Object val);
<R> Children notLike(boolean condition, SFunction<R, ?> column, Object val);
/**
* ignore
*/
default <R> Children likeLeft(SFunction<R,?> column, Object val) {
default <R> Children likeLeft(SFunction<R, ?> column, Object val) {
return likeLeft(true, column, val);
}
@ -266,12 +247,12 @@ public interface MyCompare<Children> extends Serializable {
* @param val
* @return children
*/
<R> Children likeLeft(boolean condition, SFunction<R,?> column, Object val);
<R> Children likeLeft(boolean condition, SFunction<R, ?> column, Object val);
/**
* ignore
*/
default <R> Children likeRight(SFunction<R,?> column, Object val) {
default <R> Children likeRight(SFunction<R, ?> column, Object val) {
return likeRight(true, column, val);
}
@ -283,5 +264,5 @@ public interface MyCompare<Children> extends Serializable {
* @param val
* @return children
*/
<R> Children likeRight(boolean condition, SFunction<R,?> column, Object val);
<R> Children likeRight(boolean condition, SFunction<R, ?> column, Object val);
}

View File

@ -1,18 +1,3 @@
/*
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mybatisplus.wrapper.interfaces;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
@ -26,10 +11,7 @@ import java.util.function.Consumer;
import static java.util.stream.Collectors.toList;
/**
* 查询条件封装
*
* @author hubin miemie HCL
* @since 2017-05-26
* copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Func}
*/
@SuppressWarnings("unchecked")
public interface MyFunc<Children> extends Serializable {

View File

@ -3,6 +3,9 @@ package com.github.mybatisplus.wrapper.interfaces;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.mybatisplus.toolkit.Constant;
/**
* @author yulichang
*/
public interface MyLambdaJoin<Children> {
default <T, X> Children leftJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {

View File

@ -1,18 +1,3 @@
/*
* Copyright (c) 2011-2021, baomidou (jobob@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.mybatisplus.wrapper.interfaces;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;