通过注解增加查询的操作方式
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
package com.synebula.gaea.mongo
|
||||
|
||||
import com.synebula.gaea.query.Operator
|
||||
import com.synebula.gaea.query.OrderType
|
||||
import com.synebula.gaea.query.Where
|
||||
import org.springframework.data.domain.Sort
|
||||
import org.springframework.data.mongodb.core.query.Criteria
|
||||
import org.springframework.data.mongodb.core.query.Query
|
||||
import org.springframework.data.mongodb.core.query.isEqualTo
|
||||
import java.lang.reflect.Field
|
||||
|
||||
|
||||
@@ -25,16 +26,40 @@ fun Query.select(fields: Array<String>): Query {
|
||||
*
|
||||
* @param params 参数列表
|
||||
*/
|
||||
fun Query.where(params: Map<String, Any>?): Query {
|
||||
fun Query.where(params: Map<String, Any>?, onWhere: ((v: String) -> Operator) = { Operator.eq }): Query {
|
||||
val criteria = Criteria()
|
||||
if (params != null) {
|
||||
for (param in params) {
|
||||
criteria.and(param.key).isEqualTo(param.value)
|
||||
val where = onWhere(param.key)
|
||||
when (where) {
|
||||
Operator.eq -> criteria.and(param.key).`is`(param.value)
|
||||
Operator.ne -> criteria.and(param.key).ne(param.value)
|
||||
Operator.lt -> criteria.and(param.key).lt(param.value)
|
||||
Operator.gt -> criteria.and(param.key).gt(param.value)
|
||||
Operator.lte -> criteria.and(param.key).lte(param.value)
|
||||
Operator.gte -> criteria.and(param.key).gte(param.value)
|
||||
Operator.like -> criteria.and(param.key).regex(param.value.toString())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return this.addCriteria(criteria)
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据参数获取查询条件
|
||||
*
|
||||
* @param params 参数列表
|
||||
*/
|
||||
fun Query.where(params: Map<String, Any>?, clazz: Class<*>): Query {
|
||||
var field: Field?
|
||||
var where: Where?
|
||||
return this.where(params) { name ->
|
||||
field = clazz.getDeclaredField(name)
|
||||
where = field?.getDeclaredAnnotation(Where::class.java)
|
||||
if (where == null) Operator.eq else where!!.operator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ID查询条件
|
||||
|
||||
@@ -83,7 +83,7 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
|
||||
val fields = this.clazz!!.fields()
|
||||
val query = Query()
|
||||
query.select(fields.toTypedArray())
|
||||
query.where(params)
|
||||
query.where(params, this.clazz!!)
|
||||
this.template.find(query, this.clazz!!, this.collection)
|
||||
} else listOf()
|
||||
}
|
||||
@@ -92,7 +92,7 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
|
||||
this.check()
|
||||
return if (this.clazz != null) {
|
||||
val query = Query()
|
||||
this.template.count(query.where(params), this.collection).toInt()
|
||||
this.template.count(query.where(params, this.clazz!!), this.collection).toInt()
|
||||
} else 0
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
|
||||
val query = Query()
|
||||
val fields = this.clazz!!.fields()
|
||||
val result = PagingData<TView>(params.page, params.size)
|
||||
query.where(params.parameters)
|
||||
query.where(params.parameters, this.clazz!!)
|
||||
result.total = this.count(params.parameters)
|
||||
query.select(fields.toTypedArray())
|
||||
query.with(order(params.orderBy))
|
||||
|
||||
@@ -37,14 +37,14 @@ open class MongoQuery(var repo: MongoTemplate, var logger: ILogger? = null) : IQ
|
||||
override fun <TView> list(params: Map<String, Any>?, clazz: Class<TView>): List<TView> {
|
||||
val fields = clazz.fields()
|
||||
val query = Query()
|
||||
query.where(params)
|
||||
query.where(params, clazz)
|
||||
query.select(fields.toTypedArray())
|
||||
return this.repo.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), this.collection(clazz)).toInt()
|
||||
return this.repo.count(query.where(params, clazz), this.collection(clazz)).toInt()
|
||||
}
|
||||
|
||||
override fun <TView> paging(params: PagingParam, clazz: Class<TView>): PagingData<TView> {
|
||||
@@ -53,7 +53,7 @@ open class MongoQuery(var repo: MongoTemplate, var logger: ILogger? = null) : IQ
|
||||
result.size = params.size
|
||||
result.page = params.page
|
||||
val query = Query()
|
||||
query.where(params.parameters)
|
||||
query.where(params.parameters, clazz)
|
||||
result.total = this.count(params.parameters, clazz)
|
||||
query.select(fields.toTypedArray())
|
||||
query.with(order(params.orderBy))
|
||||
|
||||
Reference in New Issue
Block a user