修改分页组件签名

This commit is contained in:
2020-05-29 01:00:35 +08:00
parent 736d81119f
commit ab83138580
9 changed files with 57 additions and 47 deletions

View File

@@ -3,7 +3,7 @@ package com.synebula.gaea.app.query
import com.synebula.gaea.app.component.HttpMessage
import com.synebula.gaea.data.message.Status
import com.synebula.gaea.query.IGenericQuery
import com.synebula.gaea.query.PagingParam
import com.synebula.gaea.query.Params
/**
* 应用类接口提供实现Query服务的接口
@@ -51,7 +51,7 @@ interface IQueryGenericApp<TView, TKey> : IQueryApp<TView, TKey> {
*/
override fun doPaging(size: Int, page: Int, params: MutableMap<String, Any>): HttpMessage {
return this.doQuery("获取${this.name}分页数据[条数:$size,页码:$page]失败") {
val data = PagingParam(page, size)
val data = Params(page, size)
data.parameters = params
this.query!!.paging(data)
}

View File

@@ -3,7 +3,7 @@ package com.synebula.gaea.app.query
import com.synebula.gaea.app.component.HttpMessage
import com.synebula.gaea.data.message.Status
import com.synebula.gaea.query.IQuery
import com.synebula.gaea.query.PagingParam
import com.synebula.gaea.query.Params
/**
* 应用类接口提供实现Query服务的接口.
@@ -38,7 +38,7 @@ interface IQueryTypedApp<TView, TKey> : IQueryApp<TView, TKey> {
override fun doPaging(size: Int, page: Int, params: MutableMap<String, Any>): HttpMessage {
return this.doQuery("获取${this.name}分页数据[条数:$size,页码:$page]失败") {
val data = PagingParam(page, size)
val data = Params(page, size)
data.parameters = params
this.query!!.paging(data, clazz)
}

View File

@@ -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]的集合")
}
}
}
}

View File

@@ -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]的集合")

View File

@@ -38,5 +38,5 @@ interface IGenericQuery<TView, TKey> {
* @param param 分页条件
* @return 分页数据
*/
fun paging(param: PagingParam): PagingData<TView>
fun paging(param: Params): Page<TView>
}

View File

@@ -36,5 +36,5 @@ interface IQuery {
* @param param 分页条件
* @return 分页数据
*/
fun <TView> paging(param: PagingParam, clazz: Class<TView>): PagingData<TView>
fun <TView> paging(param: Params, clazz: Class<TView>): Page<TView>
}

View File

@@ -38,5 +38,5 @@ interface IQueryComplex<TView, TKey, TSecond> {
* @param param 分页条件
* @return
*/
fun paging(param: PagingParam): PagingData<TView>
fun paging(param: Params): Page<TView>
}

View File

@@ -6,7 +6,7 @@ package com.synebula.gaea.query
*
* @author alex
*/
class PagingData<T> {
class Page<T> {
/**
* 页码从1开始
*/

View File

@@ -1,13 +1,13 @@
package com.synebula.gaea.query
/**
* class PagingParam
* class 分页参数信息
*
* @author alex
* @version 0.1
* @since 2020-05-15
*/
data class PagingParam(var page: Int = 1, var size: Int = 10) {
data class Params(var page: Int = 1, var size: Int = 10) {
/**
* 数据索引从0开始表示数据在总量的第几条index = (page - 1) * size
@@ -29,7 +29,7 @@ data class PagingParam(var page: Int = 1, var size: Int = 10) {
/**
* 添加查询条件
*/
fun addParameter(field: String, value: Any): PagingParam {
fun addParameter(field: String, value: Any): Params {
parameters[field] = value
return this
}
@@ -37,7 +37,7 @@ data class PagingParam(var page: Int = 1, var size: Int = 10) {
/**
* 添加排序条件
*/
fun addOrderBy(field: String, type: OrderType = OrderType.ASC): PagingParam {
fun addOrderBy(field: String, type: OrderType = OrderType.ASC): Params {
orderBy[field] = type
return this
}