Bläddra i källkod

增加 kotlin lambda 支持

yangyuhan 6 år sedan
förälder
incheckning
ff75a74479

+ 32 - 9
build.gradle

@@ -1,3 +1,33 @@
+buildscript {
+    repositories {
+        maven { url "https://repo.spring.io/plugins-release" }
+    }
+    dependencies {
+        classpath("org.springframework.build.gradle:propdeps-plugin:0.0.7")
+        classpath("com.netflix.nebula:gradle-extra-configurations-plugin:4.0.1")
+    }
+}
+
+plugins {
+    id "org.jetbrains.kotlin.jvm" version "1.3.0"
+}
+repositories {
+    mavenCentral()
+}
+dependencies {
+    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
+}
+compileKotlin {
+    kotlinOptions {
+        jvmTarget = "1.8"
+    }
+}
+compileTestKotlin {
+    kotlinOptions {
+        jvmTarget = "1.8"
+    }
+}
+
 ext {
     configuration = [
         javaVersion = JavaVersion.VERSION_1_8
@@ -12,6 +42,8 @@ ext {
     ]
 
     dependencies = [
+        "commons-collections"               : "commons-collections:commons-collections:3.2.2",
+        "kotlin-reflect"                    : "org.jetbrains.kotlin:kotlin-reflect:1.2.71",
         "jsqlparser"                        : "com.github.jsqlparser:jsqlparser:${jsqlparserVersion}",
         "mybatis-spring"                    : "org.mybatis:mybatis-spring:${mybatisSpringVersion}",
         "mybatis"                           : "org.mybatis:mybatis:${mybatisVersion}",
@@ -63,15 +95,6 @@ allprojects {
 
 description = "Mybatis 增强工具包 - 只做增强不做改变,简化CRUD操作"
 
-buildscript {
-    repositories {
-        maven { url "https://repo.spring.io/plugins-release" }
-    }
-    dependencies {
-        classpath("org.springframework.build.gradle:propdeps-plugin:0.0.7")
-        classpath("com.netflix.nebula:gradle-extra-configurations-plugin:4.0.1")
-    }
-}
 
 configure(subprojects) {
     apply plugin: 'propdeps'

+ 5 - 0
mybatis-plus-core/build.gradle

@@ -1,3 +1,6 @@
+plugins {
+    id "org.jetbrains.kotlin.jvm"
+}
 apply plugin: 'java'
 
 sourceCompatibility = 1.8
@@ -7,6 +10,8 @@ dependencies {
     compile rootProject.ext.dependencies["mybatis"]
     compile rootProject.ext.dependencies["jsqlparser"]
     compile rootProject.ext.dependencies["mybatis-spring"]
+    compile rootProject.ext.dependencies["kotlin-reflect"]
+    compile rootProject.ext.dependencies["commons-collections"]
 
     provided rootProject.ext.dependencies["cglib"]
     provided rootProject.ext.dependencies["spring-aop"]

+ 38 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractLambdaWrapperKt.kt

@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.baomidou.mybatisplus.core.conditions
+
+import com.baomidou.mybatisplus.core.toolkit.LAMBDA_KT_CACHE
+import kotlin.reflect.KProperty
+
+/**
+ *
+ *
+ * Lambda 语法使用 Wrapper
+ * 统一处理解析 lambda 获取 column
+ *
+ *
+ * @author yangyuhan
+ * @since 2018-11-07
+ */
+abstract class AbstractLambdaWrapperKt<T, This : AbstractLambdaWrapperKt<T, This>> : AbstractWrapper<T, KProperty<*>, This>() {
+
+
+    override fun columnToString(kProperty: KProperty<*>): String {
+      return  LAMBDA_KT_CACHE[kProperty]!!
+    }
+
+}

+ 116 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/query/LambdaQueryWrapperkt.kt

@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.baomidou.mybatisplus.core.conditions.query
+
+import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapperKt
+import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments
+import com.baomidou.mybatisplus.core.metadata.TableFieldInfo
+import com.baomidou.mybatisplus.core.toolkit.ArrayUtils
+import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper
+import java.util.concurrent.atomic.AtomicInteger
+import java.util.function.Predicate
+import kotlin.reflect.KProperty
+
+/**
+ *
+ *
+ * Kotlin Lambda 语法使用 Wrapper
+ *
+ *
+ * @author yangyuhan
+ * @since 2018-11-02
+ */
+class LambdaQueryWrapperkt<T> : AbstractLambdaWrapperKt<T, LambdaQueryWrapperkt<T>> {
+
+    /**
+     * 查询字段
+     */
+    private var sqlSelect: String? = null
+
+    @JvmOverloads constructor(entity: T? = null) {
+        this.entity = entity
+        this.initEntityClass()
+        this.initNeed()
+    }
+
+    internal constructor(entity: T, entityClass: Class<T>?, sqlSelect: String?, paramNameSeq: AtomicInteger, paramNameValuePairs: Map<String, Any>,
+                         mergeSegments: MergeSegments) {
+        this.entity = entity
+        this.paramNameSeq = paramNameSeq
+        this.paramNameValuePairs = paramNameValuePairs
+        this.expression = mergeSegments
+        this.sqlSelect = sqlSelect
+        this.entityClass = entityClass
+    }
+
+    /**
+     *
+     *
+     * SELECT 部分 SQL 设置
+     *
+     *
+     * @param columns 查询字段
+     */
+    @SafeVarargs
+    fun select(vararg columns: KProperty<*>): LambdaQueryWrapperkt<T> {
+        if (ArrayUtils.isNotEmpty(columns)) {
+            this.sqlSelect = this.columnsToString(*columns)
+        }
+        return typedThis
+    }
+
+    fun select(predicate: Predicate<TableFieldInfo>): LambdaQueryWrapperkt<T> {
+        return select(entityClass, predicate)
+    }
+
+    /**
+     *
+     *
+     * 过滤查询的字段信息(主键除外!)
+     *
+     *
+     *
+     * 例1: 只要 java 字段名以 "test" 开头的              -> select(i -> i.getProperty().startsWith("test"))
+     * 例2: 只要 java 字段属性是 CharSequence 类型的       -> select(TableFieldInfo::isCharSequence)
+     * 例3: 只要 java 字段没有填充策略的                   -> select(i -> i.getFieldFill == FieldFill.DEFAULT)
+     * 例4: 要全部字段                                   -> select(i -> true)
+     * 例5: 只要主键字段                                 -> select(i -> false)
+     *
+     *
+     * @param predicate 过滤方式
+     * @return this
+     */
+    fun select(entityClass: Class<T>, predicate: Predicate<TableFieldInfo>): LambdaQueryWrapperkt<T> {
+        this.entityClass = entityClass
+        this.sqlSelect = TableInfoHelper.getTableInfo(checkEntityClass).chooseSelect(predicate)
+        return typedThis
+    }
+
+    override fun getSqlSelect(): String? {
+        return sqlSelect
+    }
+
+    /**
+     *
+     *
+     * 用于生成嵌套 sql
+     * 故 sqlSelect 不向下传递
+     *
+     */
+    override fun instance(paramNameSeq: AtomicInteger, paramNameValuePairs: Map<String, Any>): LambdaQueryWrapperkt<T> {
+        return LambdaQueryWrapperkt(entity, entityClass, null, paramNameSeq, paramNameValuePairs, MergeSegments())
+    }
+}

+ 12 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/query/QueryWrapper.java

@@ -31,7 +31,7 @@ import java.util.function.Predicate;
  * Entity 对象封装操作类
  * </p>
  *
- * @author hubin miemie HCL
+ * @author hubin miemie HCL yyh
  * @since 2018-05-25
  */
 @SuppressWarnings("serial")
@@ -112,6 +112,17 @@ public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>>
         return new LambdaQueryWrapper<>(entity, entityClass, sqlSelect, paramNameSeq, paramNameValuePairs, expression);
     }
 
+
+    /**
+     * <p>
+     * 返回一个支持 Kotlin lambda 函数写法的 wrapper
+     * </p>
+     */
+    public LambdaQueryWrapperkt<T> lambdaKt() {
+        return new LambdaQueryWrapperkt<>(entity, entityClass, sqlSelect, paramNameSeq, paramNameValuePairs, expression);
+    }
+
+
     @Override
     public String getSqlSelect() {
         return sqlSelect;

+ 72 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/update/LambdaUpdateWrapperKt.kt

@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.baomidou.mybatisplus.core.conditions.update
+
+import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapperKt
+import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils
+import com.baomidou.mybatisplus.core.toolkit.StringPool
+import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils
+import java.util.concurrent.atomic.AtomicInteger
+import java.util.stream.Collectors.joining
+import kotlin.reflect.KProperty
+
+
+/**
+ *
+ *
+ * Kotlin Lambda 更新封装
+ *
+ *
+ * @author yangyuhan
+ * @since 2018-11-02
+ */
+class LambdaUpdateWrapperKt<T> internal constructor(entity: T, paramNameSeq: AtomicInteger, paramNameValuePairs: Map<String, Any>,
+                                                    mergeSegments: MergeSegments) : AbstractLambdaWrapperKt<T, LambdaUpdateWrapperKt<T>>() {
+
+    /**
+     * SQL 更新字段内容,例如:name='1',age=2
+     */
+    private val sqlSet = ArrayList<String>()
+
+    init {
+        this.entity = entity
+        this.paramNameSeq = paramNameSeq
+        this.paramNameValuePairs = paramNameValuePairs
+        this.expression = mergeSegments
+    }
+
+    override fun getSqlSet(): String? {
+        return if (CollectionUtils.isEmpty(sqlSet)) {
+            null
+        } else SqlUtils.stripSqlInjection(sqlSet.stream().collect(joining(StringPool.COMMA)))
+    }
+
+    operator fun set(column: KProperty<*>, `val`: Any): LambdaUpdateWrapperKt<T> {
+        return this.set(true, column, `val`)
+    }
+
+    operator fun set(condition: Boolean, column: KProperty<*>, `val`: Any): LambdaUpdateWrapperKt<T> {
+        if (condition) {
+            sqlSet.add(String.format("%s=%s", columnToString(column), formatSql("{0}", `val`)))
+        }
+        return typedThis
+    }
+
+    override fun instance(paramNameSeq: AtomicInteger, paramNameValuePairs: Map<String, Any>): LambdaUpdateWrapperKt<T> {
+        return LambdaUpdateWrapperKt(entity, paramNameSeq, paramNameValuePairs, MergeSegments())
+    }
+}

+ 11 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/update/UpdateWrapper.java

@@ -30,7 +30,7 @@ import java.util.concurrent.atomic.AtomicInteger;
  * Update 条件封装
  * </p>
  *
- * @author hubin miemie HCL
+ * @author hubin miemie HCL yyh
  * @since 2018-05-30
  */
 @SuppressWarnings("serial")
@@ -70,6 +70,16 @@ public class UpdateWrapper<T> extends AbstractWrapper<T, String, UpdateWrapper<T
         return new LambdaUpdateWrapper<>(entity, sqlSet, paramNameSeq, paramNameValuePairs, expression);
     }
 
+
+    /**
+     * <p>
+     * 返回一个支持 kotlin  lambda 函数写法的 wrapper
+     * </p>
+     */
+    public LambdaUpdateWrapperKt<T> lambdaKt() {
+        return new LambdaUpdateWrapperKt<>(entity, paramNameSeq, paramNameValuePairs, expression);
+    }
+
     @Override
     public String getSqlSet() {
         if (CollectionUtils.isEmpty(sqlSet)) {

+ 3 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java

@@ -73,7 +73,9 @@ public final class LambdaUtils {
      * @param tableInfo 表信息
      */
     public static void createCache(Class clazz, TableInfo tableInfo) {
-        LAMBDA_CACHE.put(clazz.getName(), createLambdaMap(tableInfo, clazz));
+        Map<String, String> lambdaMap = createLambdaMap(tableInfo, clazz);
+        LAMBDA_CACHE.put(clazz.getName(), lambdaMap);
+        LambdaUtilsKt.createCatch(clazz,lambdaMap);
     }
 
     /**

+ 16 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.kt

@@ -0,0 +1,16 @@
+package com.baomidou.mybatisplus.core.toolkit
+
+import org.apache.commons.collections.MapUtils
+import java.util.concurrent.ConcurrentHashMap
+import kotlin.reflect.KProperty
+import kotlin.reflect.full.memberProperties
+
+val LAMBDA_KT_CACHE = ConcurrentHashMap<KProperty<*>, String>()
+
+
+fun createCatch(clazz: Class<*>, source: Map<String, String>)  {
+    for (memberProperty in clazz.kotlin.memberProperties) {
+        LAMBDA_KT_CACHE[memberProperty]= MapUtils.getString(source, memberProperty.name)
+    }
+}
+