重构Gaea服务, 升级版本
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package com.synebula.gaea.mongo
|
||||
|
||||
import com.synebula.gaea.data.date.DateTime
|
||||
import com.synebula.gaea.query.Operator
|
||||
import com.synebula.gaea.query.OrderType
|
||||
import com.synebula.gaea.query.Where
|
||||
import com.synebula.gaea.query.annotation.Where
|
||||
import com.synebula.gaea.query.type.Operator
|
||||
import com.synebula.gaea.query.type.Order
|
||||
import org.springframework.data.domain.Sort
|
||||
import org.springframework.data.mongodb.core.query.Criteria
|
||||
import org.springframework.data.mongodb.core.query.Query
|
||||
|
||||
@@ -3,11 +3,14 @@ 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.*
|
||||
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.IQuery
|
||||
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
|
||||
|
||||
@@ -36,20 +39,20 @@ open class MongoQuery(var template: MongoTemplate, var logger: ILogger? = null)
|
||||
return this.template.count(query.where(params, clazz), this.collection(clazz)).toInt()
|
||||
}
|
||||
|
||||
override fun <TView> paging(param: Params, clazz: Class<TView>): Page<TView> {
|
||||
override fun <TView> paging(params: Params, clazz: Class<TView>): Page<TView> {
|
||||
val query = Query()
|
||||
val fields = clazz.fieldNames()
|
||||
val result = Page<TView>(param.page, param.size)
|
||||
result.total = this.count(param.parameters, clazz)
|
||||
val result = Page<TView>(params.page, params.size)
|
||||
result.total = this.count(params.parameters, clazz)
|
||||
//如果总数和索引相同,说明该页没有数据,直接跳到上一页
|
||||
if (result.total == result.index) {
|
||||
param.page -= 1
|
||||
params.page -= 1
|
||||
result.page -= 1
|
||||
}
|
||||
query.select(fields.toTypedArray())
|
||||
query.where(param.parameters, clazz)
|
||||
query.with(order(param.orders))
|
||||
query.skip(param.index).limit(param.size)
|
||||
query.where(params.parameters, clazz)
|
||||
query.with(order(params.orders))
|
||||
query.skip(params.index).limit(params.size)
|
||||
result.data = this.template.find(query, clazz, this.collection(clazz))
|
||||
return result
|
||||
}
|
||||
@@ -61,17 +64,19 @@ open class MongoQuery(var template: MongoTemplate, var logger: ILogger? = null)
|
||||
/**
|
||||
* 获取collection
|
||||
*/
|
||||
protected fun <TView> collection(clazz: Class<TView>): String {
|
||||
val collection: Collection? = clazz.getDeclaredAnnotation(Collection::class.java)
|
||||
return if (collection != null)
|
||||
return collection.name
|
||||
fun <TView> collection(clazz: Class<TView>): String {
|
||||
val table: Table? = clazz.getDeclaredAnnotation(
|
||||
Table::class.java
|
||||
)
|
||||
return if (table != null)
|
||||
return table.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]的集合")
|
||||
throw RuntimeException("找不到名为[$table]的集合")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ 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.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
|
||||
|
||||
@@ -20,10 +20,11 @@ import org.springframework.data.mongodb.core.query.Query
|
||||
* @param template MongoRepo对象
|
||||
* @param logger 日志组件
|
||||
*/
|
||||
open class MongoGenericQuery<TView>(
|
||||
var template: MongoTemplate,
|
||||
var clazz: Class<TView>? = null,
|
||||
var logger: ILogger? = null) : IGenericQuery<TView, String> {
|
||||
open class MongoSpecificQuery<TView>(
|
||||
var template: MongoTemplate,
|
||||
var clazz: Class<TView>? = null,
|
||||
var logger: ILogger? = null
|
||||
) : ISpecificQuery<TView, String> {
|
||||
|
||||
/**
|
||||
* 使用View解析是collection时是否校验存在,默认不校验
|
||||
@@ -129,7 +130,7 @@ open class MongoGenericQuery<TView>(
|
||||
*/
|
||||
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)
|
||||
val collection = clazz.getDeclaredAnnotation(Table::class.java)
|
||||
return if (collection != null)
|
||||
return collection.name
|
||||
else {
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.synebula.gaea.mongo.repository
|
||||
|
||||
import com.synebula.gaea.domain.model.IAggregateRoot
|
||||
import com.synebula.gaea.domain.repository.IGenericRepository
|
||||
import com.synebula.gaea.domain.repository.ISpecificRepository
|
||||
import com.synebula.gaea.mongo.whereId
|
||||
import org.springframework.data.mongodb.core.MongoTemplate
|
||||
|
||||
@@ -9,8 +9,8 @@ import org.springframework.data.mongodb.core.MongoTemplate
|
||||
* 实现IAggregateRoot的mongo仓储类
|
||||
* @param repo MongoRepo对象
|
||||
*/
|
||||
class MongoGenericRepository<TAggregateRoot : IAggregateRoot<String>>(private var repo: MongoTemplate) :
|
||||
IGenericRepository<TAggregateRoot, String> {
|
||||
class MongoSpecificRepository<TAggregateRoot : IAggregateRoot<String>>(private var repo: MongoTemplate) :
|
||||
ISpecificRepository<TAggregateRoot, String> {
|
||||
|
||||
/**
|
||||
* 仓储的对象类
|
||||
Reference in New Issue
Block a user