通过注解增加查询的操作方式

This commit is contained in:
2020-05-22 22:15:05 +08:00
parent 2d0de09816
commit b400ac6482
5 changed files with 76 additions and 9 deletions

View File

@@ -1,10 +1,11 @@
package com.synebula.gaea.mongo package com.synebula.gaea.mongo
import com.synebula.gaea.query.Operator
import com.synebula.gaea.query.OrderType import com.synebula.gaea.query.OrderType
import com.synebula.gaea.query.Where
import org.springframework.data.domain.Sort import org.springframework.data.domain.Sort
import org.springframework.data.mongodb.core.query.Criteria import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query import org.springframework.data.mongodb.core.query.Query
import org.springframework.data.mongodb.core.query.isEqualTo
import java.lang.reflect.Field import java.lang.reflect.Field
@@ -25,16 +26,40 @@ fun Query.select(fields: Array<String>): Query {
* *
* @param params 参数列表 * @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() val criteria = Criteria()
if (params != null) { if (params != null) {
for (param in params) { 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) 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查询条件 * 获取ID查询条件

View File

@@ -83,7 +83,7 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
val fields = this.clazz!!.fields() val fields = this.clazz!!.fields()
val query = Query() val query = Query()
query.select(fields.toTypedArray()) query.select(fields.toTypedArray())
query.where(params) query.where(params, this.clazz!!)
this.template.find(query, this.clazz!!, this.collection) this.template.find(query, this.clazz!!, this.collection)
} else listOf() } else listOf()
} }
@@ -92,7 +92,7 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
this.check() this.check()
return if (this.clazz != null) { return if (this.clazz != null) {
val query = Query() 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 } else 0
} }
@@ -102,7 +102,7 @@ open class MongoGenericQuery<TView>(var template: MongoTemplate, var logger: ILo
val query = Query() val query = Query()
val fields = this.clazz!!.fields() val fields = this.clazz!!.fields()
val result = PagingData<TView>(params.page, params.size) val result = PagingData<TView>(params.page, params.size)
query.where(params.parameters) query.where(params.parameters, this.clazz!!)
result.total = this.count(params.parameters) result.total = this.count(params.parameters)
query.select(fields.toTypedArray()) query.select(fields.toTypedArray())
query.with(order(params.orderBy)) query.with(order(params.orderBy))

View File

@@ -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> { override fun <TView> list(params: Map<String, Any>?, clazz: Class<TView>): List<TView> {
val fields = clazz.fields() val fields = clazz.fields()
val query = Query() val query = Query()
query.where(params) query.where(params, clazz)
query.select(fields.toTypedArray()) query.select(fields.toTypedArray())
return this.repo.find(query, clazz, this.collection(clazz)) return this.repo.find(query, clazz, this.collection(clazz))
} }
override fun <TView> count(params: Map<String, Any>?, clazz: Class<TView>): Int { override fun <TView> count(params: Map<String, Any>?, clazz: Class<TView>): Int {
val query = Query() 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> { 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.size = params.size
result.page = params.page result.page = params.page
val query = Query() val query = Query()
query.where(params.parameters) query.where(params.parameters, clazz)
result.total = this.count(params.parameters, clazz) result.total = this.count(params.parameters, clazz)
query.select(fields.toTypedArray()) query.select(fields.toTypedArray())
query.with(order(params.orderBy)) query.with(order(params.orderBy))

View File

@@ -0,0 +1,38 @@
package com.synebula.gaea.query
enum class Operator {
/**
* 等于
*/
eq,
/**
* 不等于
*/
ne,
/**
* 小于
*/
lt,
/**
* 大于
*/
gt,
/**
* 小于或等于
*/
lte,
/**
* 大于或等于
*/
gte,
/**
* 模糊匹配
*/
like
}

View File

@@ -0,0 +1,4 @@
package com.synebula.gaea.query
@Target(AnnotationTarget.FIELD)
annotation class Where(val operator: Operator)