修改分页组件签名
This commit is contained in:
@@ -3,26 +3,32 @@ package com.synebula.gaea.mongo.query
|
||||
import com.synebula.gaea.extension.fieldNames
|
||||
import com.synebula.gaea.extension.firstCharLowerCase
|
||||
import com.synebula.gaea.log.ILogger
|
||||
import com.synebula.gaea.mongo.Collection
|
||||
import com.synebula.gaea.mongo.order
|
||||
import com.synebula.gaea.mongo.select
|
||||
import com.synebula.gaea.mongo.where
|
||||
import com.synebula.gaea.mongo.whereId
|
||||
import com.synebula.gaea.query.IGenericQuery
|
||||
import com.synebula.gaea.query.PagingData
|
||||
import com.synebula.gaea.query.PagingParam
|
||||
import com.synebula.gaea.query.Page
|
||||
import com.synebula.gaea.query.Params
|
||||
import org.springframework.data.mongodb.core.MongoTemplate
|
||||
import org.springframework.data.mongodb.core.query.Query
|
||||
|
||||
/**
|
||||
* 实现IQuery的Mongo查询类
|
||||
* @param clazz 查询的对象类
|
||||
* @param template MongoRepo对象
|
||||
* @param logger 日志组件
|
||||
*/
|
||||
open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILogger? = null) : IGenericQuery<TView, String> {
|
||||
open class MongoGenericQuery<TView>(
|
||||
var template: MongoTemplate,
|
||||
var clazz: Class<TView>? = null,
|
||||
var logger: ILogger? = null) : IGenericQuery<TView, String> {
|
||||
|
||||
/**
|
||||
* 查询的对象类
|
||||
* 使用View解析是collection时是否校验存在,默认不校验
|
||||
*/
|
||||
var clazz: Class<TView>? = null
|
||||
var validViewCollection = false
|
||||
|
||||
private var _collection = ""
|
||||
|
||||
@@ -36,23 +42,9 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
|
||||
get() = if (this._collection.isNotEmpty())
|
||||
this._collection
|
||||
else {
|
||||
if (this.clazz != null)
|
||||
this.clazz!!.simpleName.removeSuffix("View").firstCharLowerCase()
|
||||
else
|
||||
""
|
||||
this.collection(this.clazz)
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*
|
||||
* @param clazz 视图对象类型
|
||||
* @param query MongoRepo对象
|
||||
*/
|
||||
constructor(clazz: Class<TView>, query: MongoTemplate)
|
||||
: this(query) {
|
||||
this.clazz = clazz
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*
|
||||
@@ -72,7 +64,7 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
|
||||
* @param query MongoRepo对象
|
||||
*/
|
||||
constructor(collection: String, clazz: Class<TView>, query: MongoTemplate)
|
||||
: this(clazz, query) {
|
||||
: this(query, clazz) {
|
||||
this.collection = collection
|
||||
}
|
||||
|
||||
@@ -96,12 +88,12 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
|
||||
} else 0
|
||||
}
|
||||
|
||||
override fun paging(param: PagingParam): PagingData<TView> {
|
||||
override fun paging(param: Params): Page<TView> {
|
||||
this.check()
|
||||
return if (this.clazz != null) {
|
||||
val query = Query()
|
||||
val fields = this.clazz!!.fieldNames()
|
||||
val result = PagingData<TView>(param.page, param.size)
|
||||
val result = Page<TView>(param.page, param.size)
|
||||
result.total = this.count(param.parameters)
|
||||
//如果总数和索引相同,说明该页没有数据,直接跳到上一页
|
||||
if (result.total == result.index) {
|
||||
@@ -114,7 +106,7 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
|
||||
query.skip(param.index).limit(param.size)
|
||||
result.data = this.template.find(query, this.clazz!!, this.collection)
|
||||
result
|
||||
} else PagingData(1, 10)
|
||||
} else Page(1, 10)
|
||||
}
|
||||
|
||||
override fun get(key: String): TView? {
|
||||
@@ -132,4 +124,22 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
|
||||
this.logger?.warn(this, "查询集合参数[collection]值为空, 尝试使用View<${this.clazz?.name}>名称解析集合")
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取collection
|
||||
*/
|
||||
protected fun <TView> collection(clazz: Class<TView>?): String {
|
||||
if (clazz == null) throw java.lang.RuntimeException("[${this.javaClass}]没有指定查询实体类型[clazz]")
|
||||
val collection: Collection? = clazz.getDeclaredAnnotation(Collection::class.java)
|
||||
return if (collection != null)
|
||||
return collection.name
|
||||
else {
|
||||
this.logger?.info(this, "视图类没有标记[Collection]注解,无法获取Collection名称。尝试使用View<${clazz.name}>名称解析集合")
|
||||
val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase()
|
||||
if (!validViewCollection || this.template.collectionExists(name))
|
||||
name
|
||||
else {
|
||||
throw RuntimeException("找不到名为[$collection]的集合")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,17 @@ import com.synebula.gaea.log.ILogger
|
||||
import com.synebula.gaea.mongo.*
|
||||
import com.synebula.gaea.mongo.Collection
|
||||
import com.synebula.gaea.query.IQuery
|
||||
import com.synebula.gaea.query.PagingData
|
||||
import com.synebula.gaea.query.PagingParam
|
||||
import com.synebula.gaea.query.Page
|
||||
import com.synebula.gaea.query.Params
|
||||
import org.springframework.data.mongodb.core.MongoTemplate
|
||||
import org.springframework.data.mongodb.core.query.Query
|
||||
|
||||
/**
|
||||
* 实现IQuery的Mongo查询类
|
||||
* @param repo MongoRepo对象
|
||||
* @param template MongoRepo对象
|
||||
*/
|
||||
|
||||
open class MongoQuery(var repo: MongoTemplate, var logger: ILogger? = null) : IQuery {
|
||||
open class MongoQuery(var template: MongoTemplate, var logger: ILogger? = null) : IQuery {
|
||||
|
||||
/**
|
||||
* 使用View解析是collection时是否校验存在,默认不校验
|
||||
@@ -28,18 +28,18 @@ open class MongoQuery(var repo: MongoTemplate, var logger: ILogger? = null) : IQ
|
||||
val query = Query()
|
||||
query.where(params, clazz)
|
||||
query.select(fields.toTypedArray())
|
||||
return this.repo.find(query, clazz, this.collection(clazz))
|
||||
return this.template.find(query, clazz, this.collection(clazz))
|
||||
}
|
||||
|
||||
override fun <TView> count(params: Map<String, Any>?, clazz: Class<TView>): Int {
|
||||
val query = Query()
|
||||
return this.repo.count(query.where(params, clazz), this.collection(clazz)).toInt()
|
||||
return this.template.count(query.where(params, clazz), this.collection(clazz)).toInt()
|
||||
}
|
||||
|
||||
override fun <TView> paging(param: PagingParam, clazz: Class<TView>): PagingData<TView> {
|
||||
override fun <TView> paging(param: Params, clazz: Class<TView>): Page<TView> {
|
||||
val query = Query()
|
||||
val fields = clazz.fieldNames()
|
||||
val result = PagingData<TView>(param.page, param.size)
|
||||
val result = Page<TView>(param.page, param.size)
|
||||
result.total = this.count(param.parameters, clazz)
|
||||
//如果总数和索引相同,说明该页没有数据,直接跳到上一页
|
||||
if (result.total == result.index) {
|
||||
@@ -50,12 +50,12 @@ open class MongoQuery(var repo: MongoTemplate, var logger: ILogger? = null) : IQ
|
||||
query.where(param.parameters, clazz)
|
||||
query.with(order(param.orderBy))
|
||||
query.skip(param.index).limit(param.size)
|
||||
result.data = this.repo.find(query, clazz, this.collection(clazz))
|
||||
result.data = this.template.find(query, clazz, this.collection(clazz))
|
||||
return result
|
||||
}
|
||||
|
||||
override fun <TView, TKey> get(key: TKey, clazz: Class<TView>): TView? {
|
||||
return this.repo.findOne(whereId(key), clazz, this.collection(clazz))
|
||||
return this.template.findOne(whereId(key), clazz, this.collection(clazz))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +68,7 @@ open class MongoQuery(var repo: MongoTemplate, var logger: ILogger? = null) : IQ
|
||||
else {
|
||||
this.logger?.info(this, "视图类没有标记[Collection]注解,无法获取Collection名称。尝试使用View<${clazz.name}>名称解析集合")
|
||||
val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase()
|
||||
if (!validViewCollection || this.repo.collectionExists(name))
|
||||
if (!validViewCollection || this.template.collectionExists(name))
|
||||
name
|
||||
else {
|
||||
throw RuntimeException("找不到名为[$collection]的集合")
|
||||
|
||||
Reference in New Issue
Block a user