精简代码:删除Specific相关代码
修改mongo query获取字段范围值类型
This commit is contained in:
@@ -30,7 +30,7 @@ open class MongoQuery(var template: MongoTemplate, var logger: ILogger? = null)
|
||||
val fields = this.fields(clazz)
|
||||
val query = Query()
|
||||
query.where(params, clazz)
|
||||
query.select(fields.toTypedArray())
|
||||
query.select(fields)
|
||||
return this.template.find(query, clazz, this.collection(clazz))
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ open class MongoQuery(var template: MongoTemplate, var logger: ILogger? = null)
|
||||
params.page -= 1
|
||||
result.page -= 1
|
||||
}
|
||||
query.select(fields.toTypedArray())
|
||||
query.select(fields)
|
||||
query.where(params.parameters, clazz)
|
||||
query.with(order(params.orders))
|
||||
query.skip(params.index).limit(params.size)
|
||||
@@ -61,7 +61,7 @@ open class MongoQuery(var template: MongoTemplate, var logger: ILogger? = null)
|
||||
return this.template.findOne(whereId(id), clazz, this.collection(clazz))
|
||||
}
|
||||
|
||||
fun <TView> fields(clazz: Class<TView>): List<String> {
|
||||
fun <TView> fields(clazz: Class<TView>): Array<String> {
|
||||
val fields = mutableListOf<String>()
|
||||
fields.addAll(clazz.fieldNames())
|
||||
var parent = clazz.superclass
|
||||
@@ -69,7 +69,7 @@ open class MongoQuery(var template: MongoTemplate, var logger: ILogger? = null)
|
||||
fields.addAll(clazz.superclass.fieldNames())
|
||||
parent = parent.superclass
|
||||
}
|
||||
return fields
|
||||
return fields.toTypedArray()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,146 +0,0 @@
|
||||
package com.synebula.gaea.mongo.query
|
||||
|
||||
import com.synebula.gaea.ext.fieldNames
|
||||
import com.synebula.gaea.ext.firstCharLowerCase
|
||||
import com.synebula.gaea.log.ILogger
|
||||
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.ISpecificQuery
|
||||
import com.synebula.gaea.query.Page
|
||||
import com.synebula.gaea.query.Params
|
||||
import com.synebula.gaea.query.annotation.Table
|
||||
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 MongoSpecificQuery<TView>(
|
||||
var template: MongoTemplate,
|
||||
var clazz: Class<TView>? = null,
|
||||
var logger: ILogger? = null
|
||||
) : ISpecificQuery<TView, String> {
|
||||
|
||||
/**
|
||||
* 使用View解析是collection时是否校验存在,默认不校验
|
||||
*/
|
||||
var validViewCollection = false
|
||||
|
||||
private var _collection = ""
|
||||
|
||||
/**
|
||||
* 查询的集合名称
|
||||
*/
|
||||
var collection: String
|
||||
set(value) {
|
||||
this._collection = value
|
||||
}
|
||||
get() = if (this._collection.isNotEmpty())
|
||||
this._collection
|
||||
else {
|
||||
this.collection(this.clazz)
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*
|
||||
* @param collection 查询的集合名称
|
||||
* @param query MongoRepo对象
|
||||
*/
|
||||
constructor(collection: String, query: MongoTemplate)
|
||||
: this(query) {
|
||||
this.collection = collection
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*
|
||||
* @param collection 查询的集合名称
|
||||
* @param clazz 视图对象类型
|
||||
* @param query MongoRepo对象
|
||||
*/
|
||||
constructor(collection: String, clazz: Class<TView>, query: MongoTemplate)
|
||||
: this(query, clazz) {
|
||||
this.collection = collection
|
||||
}
|
||||
|
||||
|
||||
override fun list(params: Map<String, Any>?): List<TView> {
|
||||
this.check()
|
||||
return if (this.clazz != null) {
|
||||
val fields = this.clazz!!.fieldNames()
|
||||
val query = Query()
|
||||
query.select(fields.toTypedArray())
|
||||
query.where(params, this.clazz!!)
|
||||
this.template.find(query, this.clazz!!, this.collection)
|
||||
} else listOf()
|
||||
}
|
||||
|
||||
override fun count(params: Map<String, Any>?): Int {
|
||||
this.check()
|
||||
return if (this.clazz != null) {
|
||||
val query = Query()
|
||||
this.template.count(query.where(params, this.clazz!!), this.collection).toInt()
|
||||
} else 0
|
||||
}
|
||||
|
||||
override fun paging(param: Params): Page<TView> {
|
||||
this.check()
|
||||
return if (this.clazz != null) {
|
||||
val query = Query()
|
||||
val fields = this.clazz!!.fieldNames()
|
||||
val result = Page<TView>(param.page, param.size)
|
||||
result.total = this.count(param.parameters)
|
||||
//如果总数和索引相同,说明该页没有数据,直接跳到上一页
|
||||
if (result.total == result.index) {
|
||||
param.page -= 1
|
||||
result.page -= 1
|
||||
}
|
||||
query.select(fields.toTypedArray())
|
||||
query.where(param.parameters, this.clazz!!)
|
||||
query.with(order(param.orders))
|
||||
query.skip(param.index).limit(param.size)
|
||||
result.data = this.template.find(query, this.clazz!!, this.collection)
|
||||
result
|
||||
} else Page(1, 10)
|
||||
}
|
||||
|
||||
override fun get(id: String): TView? {
|
||||
this.check()
|
||||
return if (this.clazz != null) {
|
||||
val view = this.template.findOne(whereId(id), this.clazz!!, this.collection)
|
||||
view
|
||||
} else null
|
||||
}
|
||||
|
||||
protected fun check() {
|
||||
if (this.clazz == null)
|
||||
throw RuntimeException("[${this.javaClass.name}] 没有声明查询View的类型")
|
||||
if (this._collection.isEmpty())
|
||||
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 = clazz.getDeclaredAnnotation(Table::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]的集合")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package com.synebula.gaea.mongo.repository
|
||||
|
||||
import com.synebula.gaea.domain.model.IAggregateRoot
|
||||
import com.synebula.gaea.domain.repository.ISpecificRepository
|
||||
import com.synebula.gaea.mongo.where
|
||||
import com.synebula.gaea.mongo.whereId
|
||||
import org.springframework.data.mongodb.core.MongoTemplate
|
||||
import org.springframework.data.mongodb.core.query.Query
|
||||
|
||||
/**
|
||||
* 实现IAggregateRoot的mongo仓储类
|
||||
* @param repo MongoRepo对象
|
||||
*/
|
||||
class MongoSpecificRepository<TAggregateRoot : IAggregateRoot<String>>(private var repo: MongoTemplate) :
|
||||
ISpecificRepository<TAggregateRoot, String> {
|
||||
|
||||
/**
|
||||
* 仓储的对象类
|
||||
*/
|
||||
override var clazz: Class<TAggregateRoot>? = null
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param clazz 仓储Domain对象
|
||||
* @param repo MongoRepo对象
|
||||
*/
|
||||
constructor(clazz: Class<TAggregateRoot>, repo: MongoTemplate) : this(repo) {
|
||||
this.clazz = clazz
|
||||
}
|
||||
|
||||
override fun add(obj: TAggregateRoot) {
|
||||
this.repo.save(obj)
|
||||
}
|
||||
|
||||
override fun update(obj: TAggregateRoot) {
|
||||
this.repo.save(obj)
|
||||
}
|
||||
|
||||
override fun remove(id: String) {
|
||||
this.repo.remove(whereId(id), this.clazz!!)
|
||||
}
|
||||
|
||||
override fun get(id: String): TAggregateRoot {
|
||||
return this.repo.findOne(whereId(id), clazz!!) as TAggregateRoot
|
||||
}
|
||||
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(
|
||||
id: TKey,
|
||||
clazz: Class<TAggregateRoot>
|
||||
): TAggregateRoot {
|
||||
return this.repo.findOne(whereId(id.toString()), clazz) as TAggregateRoot
|
||||
}
|
||||
|
||||
|
||||
override fun <TAggregateRoot> count(params: Map<String, Any>?): Int {
|
||||
val query = Query()
|
||||
return this.repo.count(query.where(params, this.clazz!!), this.clazz!!).toInt()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user