mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
feat:1.添加buildBootstrap方法;2.适配版本编译
This commit is contained in:
parent
da10a6beca
commit
b203d07eff
@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.kotlin.db
|
||||
|
||||
import java.util.NoSuchElementException
|
||||
|
||||
@ -1,9 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.kotlin.entry
|
||||
|
||||
import com.mybatisflex.core.dialect.DialectFactory
|
||||
import com.mybatisflex.core.row.Db.*
|
||||
import com.mybatisflex.core.table.TableInfoFactory
|
||||
import com.mybatisflex.core.util.ArrayUtil
|
||||
import com.mybatisflex.kotlin.extensions.db.DB
|
||||
import java.io.Serializable
|
||||
/**
|
||||
* 实体类父类,继承该类后将赋予实体简单增删改操作
|
||||
@ -24,7 +39,7 @@ open class Entry :Serializable{
|
||||
tableInfo.invokeOnInsertListener(this)
|
||||
val values = tableInfo.buildInsertSqlArgs(this, ignoreNulls)
|
||||
val sql = DialectFactory.getDialect().forInsertEntity(tableInfo, this, ignoreNulls)
|
||||
return DB.insertBySql(sql, *values) == 1
|
||||
return insertBySql(sql, *values) == 1
|
||||
}
|
||||
|
||||
fun update(ignoreNulls: Boolean = true): Boolean {
|
||||
@ -35,7 +50,7 @@ open class Entry :Serializable{
|
||||
val primaryValues = tableInfo.buildPkSqlArgs(this)
|
||||
val tenantIdArgs = tableInfo.buildTenantIdArgs()
|
||||
val sql = DialectFactory.getDialect().forUpdateEntity(tableInfo, this, ignoreNulls)
|
||||
return DB.updateBySql(sql, *ArrayUtil.concat(updateValues, primaryValues, tenantIdArgs)) == 1
|
||||
return updateBySql(sql, *ArrayUtil.concat(updateValues, primaryValues, tenantIdArgs)) == 1
|
||||
}
|
||||
|
||||
fun deleteById(): Boolean {
|
||||
@ -43,7 +58,7 @@ open class Entry :Serializable{
|
||||
val primaryValues = tableInfo.buildPkSqlArgs(this)
|
||||
val allValues = ArrayUtil.concat(primaryValues, tableInfo.buildTenantIdArgs())
|
||||
val sql = DialectFactory.getDialect().forDeleteEntityById(tableInfo)
|
||||
return DB.deleteBySql(sql, *allValues) == 1
|
||||
return deleteBySql(sql, *allValues) == 1
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,460 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.kotlin.extensions.db
|
||||
|
||||
import com.mybatisflex.core.BaseMapper
|
||||
import com.mybatisflex.core.MybatisFlexBootstrap
|
||||
import com.mybatisflex.core.paginate.Page
|
||||
import com.mybatisflex.core.query.QueryColumn
|
||||
import com.mybatisflex.core.query.QueryCondition
|
||||
import com.mybatisflex.core.query.QueryWrapper
|
||||
import com.mybatisflex.core.row.*
|
||||
import com.mybatisflex.core.row.Db.selectListByQuery
|
||||
import com.mybatisflex.core.row.Db.selectOneByQuery
|
||||
import com.mybatisflex.core.row.Row
|
||||
import com.mybatisflex.core.table.TableDef
|
||||
import com.mybatisflex.core.table.TableInfoFactory
|
||||
import com.mybatisflex.core.transaction.Propagation
|
||||
import com.mybatisflex.kotlin.extensions.entry.filter
|
||||
import com.mybatisflex.kotlin.extensions.entry.toEntities
|
||||
import com.mybatisflex.kotlin.scope.QueryScope
|
||||
import com.mybatisflex.kotlin.scope.queryScope
|
||||
import java.util.function.BiConsumer
|
||||
import java.util.function.Supplier
|
||||
|
||||
|
||||
/**
|
||||
* 数据库操作对象
|
||||
* 数据库简单操作扩展
|
||||
* @author 卡莫sama(yuanjiashuai)
|
||||
* @date 2023/8/7
|
||||
*/
|
||||
object DB {
|
||||
|
||||
fun invoker(): RowMapperInvoker = Db.invoker()
|
||||
|
||||
fun invoker(environmentId: String): RowMapperInvoker = Db.invoker(environmentId)
|
||||
|
||||
inline fun <reified M : BaseMapper<*>> mapper(): M = MybatisFlexBootstrap.getInstance().getMapper(M::class.java)
|
||||
|
||||
inline fun <reified T : Any> queryOne(
|
||||
vararg columns: QueryColumn,
|
||||
schema: String? = null,
|
||||
tableName: String? = null,
|
||||
noinline init: QueryScope.() -> Unit
|
||||
): T = queryRow(schema = schema, tableName = tableName, columns = columns, init = init).toEntity(T::class.java)
|
||||
|
||||
|
||||
fun queryRow(
|
||||
vararg columns: QueryColumn?,
|
||||
schema: String? = null,
|
||||
tableName: String? = null,
|
||||
init: QueryScope.() -> Unit
|
||||
): Row =
|
||||
selectOneByQuery(
|
||||
schema = schema,
|
||||
tableName = tableName,
|
||||
queryWrapper = queryScope(columns = columns, init = init)
|
||||
)
|
||||
|
||||
|
||||
inline fun <reified T> query(
|
||||
vararg columns: QueryColumn?,
|
||||
schema: String? = null,
|
||||
tableName: String? = null,
|
||||
noinline init: QueryScope.() -> Unit
|
||||
): List<T> =
|
||||
queryRows(schema = schema, tableName = tableName, columns = columns, init = init)
|
||||
.toEntities()
|
||||
inline fun <reified M : BaseMapper<*>> mapper(): M = MybatisFlexBootstrap.getInstance().getMapper(M::class.java)
|
||||
|
||||
fun queryRows(
|
||||
vararg columns: QueryColumn?,
|
||||
schema: String? = null,
|
||||
tableName: String? = null,
|
||||
init: QueryScope.() -> Unit
|
||||
): List<Row> = selectListByQuery(
|
||||
schema = schema, tableName = tableName,
|
||||
queryWrapper = queryScope(columns = columns, init = init)
|
||||
)
|
||||
inline fun <reified T : Any> queryOne(
|
||||
vararg columns: QueryColumn,
|
||||
schema: String? = null,
|
||||
tableName: String? = null,
|
||||
noinline init: QueryScope.() -> Unit
|
||||
): T = queryRow(schema = schema, tableName = tableName, columns = columns, init = init).toEntity(T::class.java)
|
||||
|
||||
// filter-----------
|
||||
inline fun <reified E> filter(
|
||||
tableName: String,
|
||||
schema: String,
|
||||
vararg columns: QueryColumn?,
|
||||
queryCondition: QueryCondition = QueryCondition.createEmpty()
|
||||
): List<E> = selectListByQuery(
|
||||
|
||||
fun queryRow(
|
||||
vararg columns: QueryColumn?,
|
||||
schema: String? = null,
|
||||
tableName: String? = null,
|
||||
init: QueryScope.() -> Unit
|
||||
): Row =
|
||||
selectOneByQuery(
|
||||
schema,
|
||||
tableName,
|
||||
queryScope(*columns).where(queryCondition)
|
||||
).toEntities()
|
||||
|
||||
inline fun <reified E> filter(
|
||||
vararg columns: QueryColumn?,
|
||||
init: () -> QueryCondition
|
||||
): List<E> {
|
||||
val tableInfo = TableInfoFactory.ofEntityClass(E::class.java)
|
||||
return filter<E>(
|
||||
columns = columns,
|
||||
schema = tableInfo.schema,
|
||||
tableName = tableInfo.tableName,
|
||||
queryCondition = init()
|
||||
)
|
||||
}
|
||||
|
||||
inline fun <reified E, T : TableDef> filter(
|
||||
tableDef: T,
|
||||
vararg columns: QueryColumn?,
|
||||
init: T.() -> QueryCondition
|
||||
): List<E> = tableDef.filter(columns = columns, init = init)
|
||||
|
||||
|
||||
// ----------------------
|
||||
fun insert(schema: String?, tableName: String?, row: Row?): Int {
|
||||
return Db.insert(schema, tableName, row)
|
||||
}
|
||||
|
||||
fun insert(tableName: String?, row: Row?): Int {
|
||||
return Db.insert(null as String?, tableName, row)
|
||||
}
|
||||
|
||||
fun insertBySql(sql: String?, vararg args: Any?): Int {
|
||||
return Db.insertBySql(sql, *args)
|
||||
}
|
||||
|
||||
fun insertBatch(schema: String?, tableName: String?, rows: Collection<Row?>, batchSize: Int = rows.size): IntArray {
|
||||
return Db.insertBatch(schema, tableName, rows, batchSize)
|
||||
}
|
||||
|
||||
|
||||
fun insertBatchWithFirstRowColumns(schema: String?, tableName: String?, rows: List<Row?>?): Int {
|
||||
return Db.insertBatchWithFirstRowColumns(schema, tableName, rows)
|
||||
}
|
||||
|
||||
fun deleteBySql(sql: String?, vararg args: Any?): Int {
|
||||
return Db.deleteBySql(sql, *args)
|
||||
}
|
||||
|
||||
fun deleteById(schema: String?, tableName: String?, row: Row?): Int {
|
||||
return Db.deleteById(schema, tableName, row)
|
||||
}
|
||||
|
||||
fun deleteById(tableName: String?, row: Row?): Int {
|
||||
return Db.deleteById(null as String?, tableName, row)
|
||||
}
|
||||
|
||||
fun deleteById(schema: String?, tableName: String?, primaryKey: String?, id: Any?): Int {
|
||||
return Db.deleteById(schema, tableName, primaryKey, id)
|
||||
}
|
||||
|
||||
fun deleteById(tableName: String?, primaryKey: String?, id: Any?): Int {
|
||||
return Db.deleteById(null as String?, tableName, primaryKey, id)
|
||||
}
|
||||
|
||||
fun deleteBatchByIds(schema: String?, tableName: String?, primaryKey: String?, ids: Collection<*>?): Int {
|
||||
return Db.deleteBatchByIds(schema, tableName, primaryKey, ids)
|
||||
}
|
||||
|
||||
|
||||
fun deleteByMap(schema: String?, tableName: String?, whereColumns: Map<String?, Any?>?): Int {
|
||||
return Db.deleteByQuery(
|
||||
schema, tableName, QueryWrapper()
|
||||
.where(whereColumns)
|
||||
)
|
||||
}
|
||||
|
||||
fun deleteByCondition(schema: String?, tableName: String?, condition: QueryCondition?): Int {
|
||||
return Db.deleteByQuery(
|
||||
schema, tableName, QueryWrapper()
|
||||
.where(condition)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fun deleteByQuery(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): Int {
|
||||
return Db.deleteByQuery(schema, tableName, queryWrapper)
|
||||
}
|
||||
|
||||
fun updateBySql(sql: String?, vararg args: Any?): Int {
|
||||
return Db.updateBySql(sql, *args)
|
||||
}
|
||||
|
||||
fun updateBatch(sql: String?, batchArgsSetter: BatchArgsSetter): IntArray {
|
||||
return Db.updateBatch(sql, batchArgsSetter)
|
||||
}
|
||||
|
||||
|
||||
fun updateById(schema: String?, tableName: String?, row: Row?): Int {
|
||||
return Db.updateById(schema, tableName, row)
|
||||
}
|
||||
|
||||
fun updateByMap(schema: String?, tableName: String?, data: Row?, whereColumns: Map<String?, Any?>?): Int {
|
||||
return Db.updateByQuery(
|
||||
schema, tableName, data, QueryWrapper()
|
||||
.where(whereColumns)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fun updateByCondition(schema: String?, tableName: String?, data: Row?, condition: QueryCondition?): Int {
|
||||
return Db.updateByQuery(
|
||||
schema, tableName, data, QueryWrapper()
|
||||
.where(condition)
|
||||
)
|
||||
}
|
||||
|
||||
fun updateByQuery(schema: String?, tableName: String?, data: Row?, queryWrapper: QueryWrapper?): Int {
|
||||
return Db.updateByQuery(schema, tableName, data, queryWrapper)
|
||||
}
|
||||
|
||||
fun updateBatchById(schema: String?, tableName: String?, rows: List<Row?>?): Int {
|
||||
return Db.updateBatchById(schema, tableName, rows)
|
||||
}
|
||||
|
||||
|
||||
fun <T> updateEntitiesBatch(entities: Collection<T>?, batchSize: Int): Int {
|
||||
return Db.updateEntitiesBatch(entities, batchSize)
|
||||
}
|
||||
|
||||
fun <T> updateEntitiesBatch(entities: Collection<T>?): Int {
|
||||
return updateEntitiesBatch(entities, 1000)
|
||||
}
|
||||
|
||||
fun updateNumberAddByQuery(
|
||||
schema: String?,
|
||||
tableName: String?,
|
||||
fieldName: String?,
|
||||
value: Number?,
|
||||
queryWrapper: QueryWrapper?
|
||||
): Int {
|
||||
return Db.updateNumberAddByQuery(schema, tableName, fieldName, value, queryWrapper)
|
||||
}
|
||||
|
||||
fun <M> executeBatch(
|
||||
totalSize: Int,
|
||||
batchSize: Int,
|
||||
mapperClass: Class<M>?,
|
||||
consumer: BiConsumer<M, Int?>?
|
||||
): IntArray {
|
||||
return Db.executeBatch(totalSize, batchSize, mapperClass, consumer)
|
||||
}
|
||||
|
||||
fun selectOneBySql(sql: String?, vararg args: Any?): Row {
|
||||
return Db.selectOneBySql(sql, *args)
|
||||
}
|
||||
|
||||
|
||||
fun selectOneById(schema: String?, tableName: String?, row: Row?): Row {
|
||||
return Db.selectOneById(schema, tableName, row)
|
||||
}
|
||||
|
||||
fun selectOneById(schema: String?, tableName: String?, primaryKey: String?, id: Any?): Row {
|
||||
return Db.selectOneById(schema, tableName, primaryKey, id)
|
||||
}
|
||||
|
||||
fun selectOneByMap(schema: String?, tableName: String?, whereColumns: Map<String, Any>?): Row {
|
||||
return Db.selectOneByQuery(
|
||||
schema, tableName, QueryWrapper()
|
||||
.where(whereColumns).limit(1)
|
||||
)
|
||||
}
|
||||
|
||||
fun selectOneByCondition(schema: String?, tableName: String?, condition: QueryCondition?): Row {
|
||||
return Db.selectOneByQuery(
|
||||
schema, tableName, QueryWrapper()
|
||||
.where(condition).limit(1)
|
||||
)
|
||||
}
|
||||
|
||||
fun selectOneByQuery(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): Row {
|
||||
return Db.selectOneByQuery(schema, tableName, queryWrapper)
|
||||
}
|
||||
|
||||
fun selectOneByQuery(queryWrapper: QueryWrapper?): Row {
|
||||
return Db.selectOneByQuery(queryWrapper)
|
||||
}
|
||||
|
||||
fun selectListBySql(sql: String?, vararg args: Any?): List<Row> {
|
||||
return Db.selectListBySql(sql, *args)
|
||||
}
|
||||
|
||||
fun selectListByMap(schema: String?, tableName: String?, whereColumns: Map<String?, Any?>?): List<Row> {
|
||||
return Db.selectListByQuery(
|
||||
schema, tableName, QueryWrapper()
|
||||
.where(whereColumns)
|
||||
)
|
||||
}
|
||||
|
||||
fun selectListByMap(tableName: String?, whereColumns: Map<String?, Any?>?): List<Row> {
|
||||
return Db.selectListByMap(tableName, whereColumns)
|
||||
}
|
||||
|
||||
fun selectListByMap(schema: String?, tableName: String?, whereColumns: Map<String?, Any?>?, count: Int): List<Row> {
|
||||
return Db.selectListByMap(schema, tableName, whereColumns, count)
|
||||
}
|
||||
|
||||
fun selectListByMap(tableName: String?, whereColumns: Map<String?, Any?>?, count: Int): List<Row> {
|
||||
return Db.selectListByMap(tableName, whereColumns, count)
|
||||
}
|
||||
|
||||
fun selectListByCondition(schema: String?, tableName: String?, condition: QueryCondition?): List<Row> {
|
||||
return Db.selectListByCondition(schema, tableName, condition)
|
||||
}
|
||||
|
||||
fun selectListByCondition(tableName: String?, condition: QueryCondition?): List<Row> {
|
||||
return Db.selectListByQuery(
|
||||
null as String?, tableName, QueryWrapper()
|
||||
.where(condition)
|
||||
)
|
||||
}
|
||||
|
||||
fun selectListByCondition(schema: String?, tableName: String?, condition: QueryCondition?, count: Int): List<Row> {
|
||||
return Db.selectListByQuery(
|
||||
schema, tableName, QueryWrapper()
|
||||
.where(condition).limit(count)
|
||||
)
|
||||
}
|
||||
|
||||
fun selectListByQuery(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): List<Row> {
|
||||
return Db.selectListByQuery(schema, tableName, queryWrapper)
|
||||
}
|
||||
|
||||
fun selectListByQuery(queryWrapper: QueryWrapper?): List<Row> {
|
||||
return Db.selectListByQuery(queryWrapper)
|
||||
}
|
||||
|
||||
fun selectAll(schema: String?, tableName: String?): List<Row> {
|
||||
return Db.selectAll(schema, tableName)
|
||||
}
|
||||
|
||||
|
||||
fun selectObject(sql: String?, vararg args: Any?): Any {
|
||||
return Db.selectObject(sql, *args)
|
||||
}
|
||||
|
||||
fun selectObject(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): Any {
|
||||
return Db.selectObject(schema, tableName, queryWrapper)
|
||||
}
|
||||
|
||||
fun selectObject(queryWrapper: QueryWrapper?): Any {
|
||||
return Db.selectObject(queryWrapper)
|
||||
}
|
||||
|
||||
fun selectObjectList(sql: String?, vararg args: Any?): List<Any> {
|
||||
return Db.selectObjectList(sql, *args)
|
||||
}
|
||||
|
||||
fun selectObjectList(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): Any {
|
||||
return Db.selectObjectList(schema, tableName, queryWrapper)
|
||||
}
|
||||
|
||||
fun selectObjectList(tableName: String?, queryWrapper: QueryWrapper?): Any {
|
||||
return Db.selectObjectList(tableName, queryWrapper)
|
||||
}
|
||||
|
||||
fun selectObjectList(queryWrapper: QueryWrapper?): Any {
|
||||
return Db.selectObjectList(queryWrapper)
|
||||
}
|
||||
|
||||
fun selectCount(sql: String?, vararg args: Any?): Long {
|
||||
return Db.selectCount(sql, *args)
|
||||
}
|
||||
|
||||
fun selectCountByCondition(schema: String?, tableName: String?, condition: QueryCondition?): Long {
|
||||
return Db.selectCountByQuery(
|
||||
schema, tableName, QueryWrapper()
|
||||
.where(condition)
|
||||
)
|
||||
}
|
||||
|
||||
fun selectCountByCondition(tableName: String?, condition: QueryCondition?): Long {
|
||||
return Db.selectCountByCondition(
|
||||
tableName, condition
|
||||
)
|
||||
}
|
||||
|
||||
fun selectCountByQuery(schema: String?, tableName: String?, queryWrapper: QueryWrapper?): Long {
|
||||
return Db.selectCountByQuery(schema, tableName, queryWrapper)
|
||||
}
|
||||
|
||||
fun selectCountByQuery(queryWrapper: QueryWrapper?): Long = Db.selectCountByQuery(queryWrapper)
|
||||
|
||||
|
||||
fun paginate(
|
||||
schema: String?,
|
||||
tableName: String?,
|
||||
pageNumber: Int,
|
||||
pageSize: Int,
|
||||
condition: QueryCondition?
|
||||
): Page<Row> = Db.paginate(schema, tableName, Page(pageNumber, pageSize), QueryWrapper.create().where(condition))
|
||||
|
||||
|
||||
fun paginate(
|
||||
tableName: String?,
|
||||
pageNumber: Int,
|
||||
pageSize: Int,
|
||||
condition: QueryCondition?
|
||||
): Page<Row> = Db.paginate(tableName, pageNumber, pageSize, condition)
|
||||
|
||||
|
||||
fun paginate(
|
||||
schema: String?,
|
||||
tableName: String?,
|
||||
pageNumber: Int,
|
||||
pageSize: Int,
|
||||
totalRow: Int,
|
||||
condition: QueryCondition?
|
||||
): Page<Row> = Db.paginate(schema, tableName, pageNumber, pageSize, totalRow, condition)
|
||||
|
||||
|
||||
fun paginate(
|
||||
tableName: String?,
|
||||
pageNumber: Int,
|
||||
pageSize: Int,
|
||||
totalRow: Int,
|
||||
condition: QueryCondition?
|
||||
): Page<Row> = Db.paginate(tableName, pageNumber, pageSize, totalRow, condition)
|
||||
|
||||
fun paginate(
|
||||
schema: String?,
|
||||
tableName: String?,
|
||||
pageNumber: Int,
|
||||
pageSize: Int,
|
||||
queryWrapper: QueryWrapper?
|
||||
): Page<Row> = Db.paginate(schema, tableName, pageNumber, pageSize, queryWrapper)
|
||||
|
||||
fun paginate(tableName: String?,
|
||||
pageNumber: Int,
|
||||
pageSize: Int,
|
||||
queryWrapper: QueryWrapper?
|
||||
): Page<Row> = Db.paginate(tableName, pageNumber, pageSize, queryWrapper)
|
||||
|
||||
fun paginate(
|
||||
schema: String?,
|
||||
tableName: String?,
|
||||
pageNumber: Int,
|
||||
pageSize: Int,
|
||||
totalRow: Int,
|
||||
queryWrapper: QueryWrapper?
|
||||
): Page<Row> = Db.paginate(schema, tableName, pageNumber, pageSize, totalRow, queryWrapper)
|
||||
|
||||
|
||||
fun paginate(
|
||||
tableName: String?,
|
||||
pageNumber: Int,
|
||||
pageSize: Int,
|
||||
totalRow: Int,
|
||||
queryWrapper: QueryWrapper?
|
||||
): Page<Row> = Db.paginate(tableName, pageNumber, pageSize, totalRow, queryWrapper)
|
||||
|
||||
|
||||
fun paginate(schema: String?, tableName: String?, page: Page<Row?>?, queryWrapper: QueryWrapper?): Page<Row> = Db.paginate(schema, tableName, page, queryWrapper)
|
||||
|
||||
fun paginate(tableName: String?, page: Page<Row?>?, queryWrapper: QueryWrapper?): Page<Row> = Db.paginate(tableName, page, queryWrapper)
|
||||
|
||||
fun tx(supplier: Supplier<Boolean?>?): Boolean = tx(supplier, Propagation.REQUIRED)
|
||||
|
||||
fun tx(supplier: Supplier<Boolean?>?, propagation: Propagation?): Boolean = Db.tx(supplier, propagation)
|
||||
|
||||
fun <T> txWithResult(supplier: Supplier<T>?): T =txWithResult(supplier, Propagation.REQUIRED)
|
||||
|
||||
fun <T> txWithResult(supplier: Supplier<T>?, propagation: Propagation?): T = Db.txWithResult(supplier, propagation)
|
||||
|
||||
queryScope(columns = columns, init = init)
|
||||
)
|
||||
|
||||
|
||||
inline fun <reified T> query(
|
||||
vararg columns: QueryColumn?,
|
||||
schema: String? = null,
|
||||
tableName: String? = null,
|
||||
noinline init: QueryScope.() -> Unit
|
||||
): List<T> =
|
||||
queryRows(schema = schema, tableName = tableName, columns = columns, init = init)
|
||||
.toEntities()
|
||||
|
||||
fun queryRows(
|
||||
vararg columns: QueryColumn?,
|
||||
schema: String? = null,
|
||||
tableName: String? = null,
|
||||
init: QueryScope.() -> Unit
|
||||
): List<Row> = selectListByQuery(
|
||||
schema,tableName,queryScope(columns = columns, init = init)
|
||||
)
|
||||
|
||||
// filter-----------
|
||||
inline fun <reified E> filter(
|
||||
tableName: String,
|
||||
schema: String,
|
||||
vararg columns: QueryColumn?,
|
||||
queryCondition: QueryCondition = QueryCondition.createEmpty()
|
||||
): List<E> = selectListByQuery(
|
||||
schema,
|
||||
tableName,
|
||||
queryScope(*columns).where(queryCondition)
|
||||
).toEntities()
|
||||
|
||||
inline fun <reified E> filter(
|
||||
vararg columns: QueryColumn?,
|
||||
init: () -> QueryCondition
|
||||
): List<E> {
|
||||
val tableInfo = TableInfoFactory.ofEntityClass(E::class.java)
|
||||
return filter<E>(
|
||||
columns = columns,
|
||||
schema = tableInfo.schema,
|
||||
tableName = tableInfo.tableName,
|
||||
queryCondition = init()
|
||||
)
|
||||
}
|
||||
|
||||
inline fun <reified E, T : TableDef> filter(
|
||||
tableDef: T,
|
||||
vararg columns: QueryColumn?,
|
||||
init: T.() -> QueryCondition
|
||||
): List<E> = tableDef.filter(columns = columns, init = init)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,16 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.kotlin.extensions.entry
|
||||
|
||||
import com.mybatisflex.core.FlexConsts
|
||||
import com.mybatisflex.core.dialect.DialectFactory
|
||||
import com.mybatisflex.core.query.QueryColumn
|
||||
import com.mybatisflex.core.query.QueryCondition
|
||||
import com.mybatisflex.core.row.Db.*
|
||||
import com.mybatisflex.kotlin.extensions.db.*
|
||||
import com.mybatisflex.core.row.Row
|
||||
import com.mybatisflex.core.row.RowUtil
|
||||
import com.mybatisflex.core.table.TableDef
|
||||
import com.mybatisflex.core.table.TableInfoFactory
|
||||
import com.mybatisflex.core.util.ArrayUtil
|
||||
import com.mybatisflex.kotlin.entry.Entry
|
||||
import com.mybatisflex.kotlin.extensions.db.DB
|
||||
import com.mybatisflex.kotlin.scope.QueryScope
|
||||
import java.util.Arrays
|
||||
|
||||
@ -29,7 +45,7 @@ inline fun <reified E, T : TableDef> T.filter(
|
||||
init: T.() -> QueryCondition
|
||||
): List<E> {
|
||||
val tableInfo = TableInfoFactory.ofEntityClass(E::class.java)
|
||||
return DB.filter<E>(
|
||||
return filter<E>(
|
||||
columns = columns,
|
||||
schema = tableInfo.schema,
|
||||
tableName = tableInfo.tableName,
|
||||
@ -41,7 +57,7 @@ inline fun <reified E> TableDef.query(
|
||||
vararg columns: QueryColumn?,
|
||||
noinline init: QueryScope.() -> Unit
|
||||
): List<E> {
|
||||
return DB.query<E>(
|
||||
return query<E>(
|
||||
columns = columns,
|
||||
schema = this.schema,
|
||||
tableName = this.tableName,
|
||||
@ -49,7 +65,7 @@ inline fun <reified E> TableDef.query(
|
||||
)
|
||||
}
|
||||
|
||||
inline fun <reified E> TableDef.all(): List<E> = DB.selectAll(schema, tableName).toEntities()
|
||||
inline fun <reified E> TableDef.all(): List<E> = selectAll(schema, tableName).toEntities()
|
||||
|
||||
inline fun <reified E> Collection<Row>.toEntities() = map { it to E::class.java }.toList()
|
||||
|
||||
@ -68,7 +84,7 @@ inline fun<reified E:Entry> List<E>.batchInsert(): Boolean {
|
||||
allValues = ArrayUtil.concat(allValues, tableInfo.buildInsertSqlArgs(entity, false))
|
||||
}
|
||||
val sql = DialectFactory.getDialect().forInsertEntityBatch(tableInfo, entities)
|
||||
return DB.insertBySql(sql,*allValues) > 1
|
||||
return insertBySql(sql,*allValues) > 1
|
||||
}
|
||||
|
||||
|
||||
@ -79,5 +95,5 @@ inline fun<reified E:Entry> List<E>. batchDeleteById(): Boolean {
|
||||
val primaryValues = this.map { tableInfo.buildPkSqlArgs(it) }.stream().flatMap(Arrays::stream).toArray()
|
||||
val tenantIdArgs = tableInfo.buildTenantIdArgs()
|
||||
val sql = DialectFactory.getDialect().forDeleteEntityBatchByIds(tableInfo, primaryValues)
|
||||
return DB.deleteBySql(sql,*ArrayUtil.concat(primaryValues, tenantIdArgs)) > 1
|
||||
return deleteBySql(sql,*ArrayUtil.concat(primaryValues, tenantIdArgs)) > 1
|
||||
}
|
||||
|
||||
@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.kotlin.extensions.mapper
|
||||
|
||||
import com.mybatisflex.core.BaseMapper
|
||||
|
||||
@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.kotlin.extensions.sql
|
||||
|
||||
import com.mybatisflex.core.query.Joiner
|
||||
|
||||
@ -0,0 +1,61 @@
|
||||
package com.mybatisflex.kotlin.scope
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.
|
||||
*/
|
||||
|
||||
import com.mybatisflex.core.MybatisFlexBootstrap
|
||||
import javax.sql.DataSource
|
||||
|
||||
class BootstrapScope(private val instant: MybatisFlexBootstrap = MybatisFlexBootstrap.getInstance()) {
|
||||
|
||||
fun dataSources(dataSourceScope: DataSourceScope.() -> Unit) =
|
||||
dataSourceScope(DataSourceScope(instant))
|
||||
|
||||
|
||||
operator fun <T> Class<T>.unaryPlus(): MybatisFlexBootstrap =
|
||||
instant.addMapper(this)
|
||||
|
||||
operator fun DataSource.unaryPlus(): MybatisFlexBootstrap =
|
||||
instant.setDataSource(this)
|
||||
|
||||
infix fun String.of(dataSource: DataSource): MybatisFlexBootstrap =
|
||||
instant.setDataSource(this, dataSource)
|
||||
|
||||
}
|
||||
|
||||
class DataSourceScope(private val bootstrap: MybatisFlexBootstrap) {
|
||||
|
||||
|
||||
fun dataSource(dataSourceKey: String, dataSource: DataSource) =
|
||||
bootstrap.addDataSource(dataSourceKey, dataSource)
|
||||
|
||||
// infix fun String.of(dataSource: DataSource) =
|
||||
// bootstrap.addDataSource(this, dataSource)
|
||||
|
||||
}
|
||||
|
||||
|
||||
fun buildBootstrap(
|
||||
instant: MybatisFlexBootstrap = MybatisFlexBootstrap.getInstance(),
|
||||
scope: BootstrapScope.(MybatisFlexBootstrap) -> Unit
|
||||
): MybatisFlexBootstrap {
|
||||
scope(BootstrapScope(instant), instant)
|
||||
return instant
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.kotlin.scope
|
||||
|
||||
import com.mybatisflex.core.query.QueryColumn
|
||||
@ -29,7 +44,7 @@ fun queryScope(vararg columns: QueryColumn?, init: (QueryScope.() -> Unit)? = nu
|
||||
val builder = QueryScope()
|
||||
|
||||
if (columns.isNotEmpty()) {
|
||||
builder.select(columns)
|
||||
builder.select(*columns)
|
||||
}
|
||||
//用于嵌套查询拿到上层查询包装对象
|
||||
init?.also {
|
||||
|
||||
@ -1,19 +1,34 @@
|
||||
package com.myba
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.kotlintest
|
||||
|
||||
import com.mybatisflex.core.BaseMapper
|
||||
import com.mybatisflex.core.MybatisFlexBootstrap
|
||||
import com.mybatisflex.core.FlexConsts
|
||||
import com.mybatisflex.core.audit.AuditManager
|
||||
import com.mybatisflex.core.audit.ConsoleMessageCollector
|
||||
import com.mybatisflex.kotlin.entry.Entry
|
||||
import com.mybatisflex.kotlin.extensions.db.DB
|
||||
import com.mybatisflex.kotlin.extensions.db.DB.filter
|
||||
import com.mybatisflex.kotlin.extensions.db.*
|
||||
import com.mybatisflex.kotlin.extensions.entry.*
|
||||
import com.mybatisflex.kotlin.extensions.mapper.queryList
|
||||
import com.mybatisflex.kotlin.extensions.sql.*
|
||||
import com.mybatisflex.kotlin.scope.buildBootstrap
|
||||
import com.mybatisflex.kotlintest.entry.Account
|
||||
import com.mybatisflex.kotlintest.entry.table.AccountTableDef
|
||||
import com.mybatisflex.kotlintest.entry.table.AccountTableDef.ACCOUNT
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder
|
||||
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType
|
||||
import javax.sql.DataSource
|
||||
import kotlin.streams.toList
|
||||
@ -28,23 +43,28 @@ fun main() {
|
||||
|
||||
AuditManager.setAuditEnable(true)
|
||||
AuditManager.setMessageCollector(ConsoleMessageCollector())
|
||||
MybatisFlexBootstrap.getInstance()
|
||||
.addMapper(AccountMapper::class.java)
|
||||
.setDataSource(dataSource)
|
||||
.start()
|
||||
|
||||
buildBootstrap {
|
||||
+ AccountMapper::class.java
|
||||
dataSources {
|
||||
// dataSource(FlexConsts.NAME,dataSource)
|
||||
FlexConsts.NAME of dataSource
|
||||
}
|
||||
// + dataSource
|
||||
}.start()
|
||||
|
||||
filter<Account> {
|
||||
ACCOUNT.AGE `=` 12 or
|
||||
`if`(true) { ACCOUNT.ID `in` listOf(1, 2) }
|
||||
}.stream().peek(::println).peek { it.id = it.id.plus(2) }.forEach(Entry::save)
|
||||
//使用表对象filter或者DB对象有两个泛型的filter方法时方法体内this为表对象无需XXX.AA调用,直接AA
|
||||
ACCOUNT.filter<Account,AccountTableDef> {
|
||||
AGE `=` 12 or
|
||||
`if`(true) { ID `in` listOf(1, 2) }
|
||||
}.stream().peek(::println).peek { it.id = it.id.plus(2) }.forEach(Entry::save)
|
||||
// ACCOUNT.filter<Account,AccountTableDef> {
|
||||
// AGE `=` 12 or
|
||||
// `if`(true) { ID `in` listOf(1, 2) }
|
||||
// }.stream().peek(::println).peek { it.id = it.id.plus(6) }.forEach(Entry::save)
|
||||
|
||||
println("保存后————————")
|
||||
DB.mapper<AccountMapper>().findByAge(18,1,2).stream().peek { println(it) }.forEach(Entry::deleteById)
|
||||
mapper<AccountMapper>().findByAge(18,1,2).stream().peek { println(it) }.forEach(Entry::deleteById)
|
||||
|
||||
println("删除后————————")
|
||||
ACCOUNT.all<Account>().stream().peek { println(it) }.map { it.userName = "sa"
|
||||
@ -60,8 +80,8 @@ fun main() {
|
||||
ACCOUNT.all<Account>().stream().peek { println(it) }.toList().filter { it.id.rem(2) == 0 }.batchDeleteById()
|
||||
|
||||
println("批量删除后————————")
|
||||
//使用DB对象查询时需指定from表
|
||||
DB.query<Account> {from(ACCOUNT)}.stream().peek { println(it) }.toList().filter { it.id.rem(3) == 0 }.map { it.userName = "哈哈"
|
||||
//直接使用函数查询时需指定from表
|
||||
query<Account> {from(ACCOUNT)}.stream().peek { println(it) }.toList().filter { it.id.rem(3) == 0 }.map { it.userName = "哈哈"
|
||||
it }.batchUpdate()
|
||||
|
||||
println("批量更新后————————")
|
||||
@ -69,6 +89,8 @@ fun main() {
|
||||
ACCOUNT.query<Account> {}.forEach(::println)
|
||||
}
|
||||
|
||||
|
||||
|
||||
//接口里面写方法时打这个注解才能生成Default方法否则会单独生成一个类导致报错
|
||||
@JvmDefaultWithCompatibility
|
||||
interface AccountMapper : BaseMapper<Account> {
|
||||
@ -76,9 +98,6 @@ interface AccountMapper : BaseMapper<Account> {
|
||||
fun findByAge(age: Int, vararg ids: Int): List<Account> = queryList {
|
||||
select(ACCOUNT.ALL_COLUMNS)
|
||||
from(ACCOUNT)
|
||||
from {
|
||||
select()
|
||||
}
|
||||
where(ACCOUNT) {
|
||||
(AGE `=` age) and `if`(true) {
|
||||
ID `in` ids.asList()
|
||||
|
||||
@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.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.mybatisflex.kotlintest.entry
|
||||
|
||||
import com.mybatisflex.annotation.Column
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user