فهرست منبع

支持在 Kotlin 中以 Lambda 的形式使用链式查询

Cat73 6 سال پیش
والد
کامیت
3d4cd24fe6

+ 46 - 0
mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/IServiceEx.kt

@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2011-2016, 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>
+ * https://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.extension.kotlin
+
+import com.baomidou.mybatisplus.extension.kotlin.chain.KtQueryChainWrapper
+import com.baomidou.mybatisplus.extension.kotlin.chain.KtUpdateChainWrapper
+import com.baomidou.mybatisplus.extension.service.IService
+import kotlin.reflect.jvm.javaType
+
+/**
+ * 获取 IService 的实现类 ServiceImpl 上的第二个范型参数的 Class
+ *
+ * @return 获取到的 Class
+ */
+@Suppress("UNCHECKED_CAST")
+private fun <T : Any> IService<T>.entityClass(): Class<T> =
+        // TODO cache
+        // TODO ServiceImpl 不一定是直接超类
+        (this::class.supertypes[0].arguments[1].type?.javaType) as Class<T>
+
+/**
+ * Kotlin 用的 Lambda 式链式查询
+ *
+ * **如实现类并未直接继承 ServiceImpl,本方法会报错,这个问题将在近期解决**
+ */
+fun <T : Any> IService<T>.ktQuery() = KtQueryChainWrapper(this.baseMapper, this.entityClass())
+
+/**
+ * Kotlin 用的 Lambda 式链式更改
+ *
+ * **如实现类并未直接继承 ServiceImpl,本方法会报错,这个问题将在近期解决**
+ */
+fun <T : Any> IService<T>.ktUpdate() = KtUpdateChainWrapper(this.baseMapper, this.entityClass())

+ 40 - 0
mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/chain/KtQueryChainWrapper.kt

@@ -0,0 +1,40 @@
+/*
+ * 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>
+ * https://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.extension.kotlin.chain
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper
+import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
+import com.baomidou.mybatisplus.extension.service.additional.AbstractChainWrapper
+import com.baomidou.mybatisplus.extension.service.additional.query.ChainQuery
+import kotlin.reflect.KProperty
+
+/**
+ * Kotlin Lambda 语法使用 Wrapper,并定义了查询方法
+ *
+ * @author Cat73
+ * @since 2019-02-28
+ */
+class KtQueryChainWrapper<T : Any>(private val mapper: BaseMapper<T>, clazz: Class<T>) :
+        AbstractChainWrapper<T, KProperty<*>, KtQueryChainWrapper<T>, KtQueryWrapper<T>>(),
+        ChainQuery<T> {
+    init {
+        super.wrapperChildren = KtQueryWrapper(clazz)
+    }
+
+    override fun getWrapper() = super.wrapperChildren
+
+    override fun getBaseMapper() = this.mapper
+}

+ 40 - 0
mybatis-plus-extension/src/main/kotlin/com/baomidou/mybatisplus/extension/kotlin/chain/KtUpdateChainWrapper.kt

@@ -0,0 +1,40 @@
+/*
+ * 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>
+ * https://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.extension.kotlin.chain
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper
+import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
+import com.baomidou.mybatisplus.extension.service.additional.AbstractChainWrapper
+import com.baomidou.mybatisplus.extension.service.additional.update.ChainUpdate
+import kotlin.reflect.KProperty
+
+/**
+ * Kotlin Lambda 语法使用 Wrapper,并定义了更新方法
+ *
+ * @author Cat73
+ * @since 2019-02-28
+ */
+class KtUpdateChainWrapper<T : Any>(private val mapper: BaseMapper<T>, clazz: Class<T>) :
+        AbstractChainWrapper<T, KProperty<*>, KtUpdateChainWrapper<T>, KtQueryWrapper<T>>(),
+        ChainUpdate<T> {
+    init {
+        super.wrapperChildren = KtQueryWrapper(clazz)
+    }
+
+    override fun getWrapper() = super.wrapperChildren
+
+    override fun getBaseMapper() = this.mapper
+}