拆分query typed两种类型
This commit is contained in:
@@ -11,7 +11,7 @@ import java.lang.reflect.Field
|
||||
/**
|
||||
* 声明了Mongo查询的特有方法
|
||||
*/
|
||||
interface IMongoQuery<TView> {
|
||||
interface IMongoQuery {
|
||||
/**
|
||||
* 日志组件
|
||||
*/
|
||||
@@ -34,14 +34,14 @@ interface IMongoQuery<TView> {
|
||||
*
|
||||
* @param id 业务ID
|
||||
*/
|
||||
fun idQuery(id: String): Query = Query(Criteria("_id").isEqualTo(id))
|
||||
fun <TKey> idQuery(id: TKey): Query = Query(Criteria("_id").isEqualTo(id))
|
||||
|
||||
/**
|
||||
* 获取视图对象的字段列表
|
||||
*
|
||||
* @param clazz 视图对象类型
|
||||
*/
|
||||
fun fields(clazz: Class<TView>): List<String> {
|
||||
fun <TView> fields(clazz: Class<TView>): List<String> {
|
||||
return analyseFields(clazz.declaredFields)
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ interface IMongoQuery<TView> {
|
||||
* @param params 参数列表
|
||||
* @param clazz 视图类对象
|
||||
*/
|
||||
fun where(query: Query, params: Map<String, Any>?, clazz: Class<TView>): Query {
|
||||
fun <TView> where(query: Query, params: Map<String, Any>?, clazz: Class<TView>): Query {
|
||||
val criteria = Criteria()
|
||||
if (params != null) {
|
||||
for (param in params) {
|
||||
@@ -83,7 +83,7 @@ interface IMongoQuery<TView> {
|
||||
*/
|
||||
fun order(orders: Map<String, OrderType>?): Sort {
|
||||
val orderList = mutableListOf<Sort.Order>()
|
||||
orders?.forEach() {
|
||||
orders?.forEach {
|
||||
orderList.add(Sort.Order(Sort.Direction.valueOf(it.value.name), it.key))
|
||||
}
|
||||
return if (orderList.size == 0)
|
||||
@@ -116,7 +116,7 @@ interface IMongoQuery<TView> {
|
||||
* @param value 字段值
|
||||
* @param clazz 视图类对象
|
||||
*/
|
||||
fun changeFieldType(key: String, value: Any, clazz: Class<TView>): Any? {
|
||||
fun <TView> changeFieldType(key: String, value: Any, clazz: Class<TView>): Any? {
|
||||
val getter = clazz.getMethod("get${key.substring(0, 1).toUpperCase()}${key.substring(1)}")
|
||||
return this.convertType(value.toString(), getter.returnType)
|
||||
}
|
||||
@@ -150,4 +150,4 @@ interface IMongoQuery<TView> {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,32 @@
|
||||
package com.synebula.gaea.query.mongo
|
||||
|
||||
import com.synebula.gaea.extension.firstCharLowerCase
|
||||
import com.synebula.gaea.log.ILogger
|
||||
import com.synebula.gaea.query.IQuery
|
||||
import com.synebula.gaea.query.PagingData
|
||||
import com.synebula.gaea.query.PagingParam
|
||||
import org.springframework.data.mongodb.core.MongoTemplate
|
||||
import org.springframework.data.mongodb.core.query.Query
|
||||
import com.synebula.gaea.extension.*
|
||||
|
||||
/**
|
||||
* 实现IQuery的Mongo查询类
|
||||
* @param repo MongoRepo对象
|
||||
*/
|
||||
open class MongoQuery<TView>(var repo: MongoTemplate, override var logger: ILogger? = null) : IQuery<TView, String>, IMongoQuery<TView> {
|
||||
open class MongoQuery<TView>(var repo: MongoTemplate, override var logger: ILogger? = null) :
|
||||
IQuery<TView, String>, IMongoQuery {
|
||||
/**
|
||||
* 查询的对象类
|
||||
*/
|
||||
var clazz: Class<TView>? = null
|
||||
|
||||
private var _collection = ""
|
||||
|
||||
/**
|
||||
* 查询的集合名称
|
||||
*/
|
||||
var collection: String = ""
|
||||
var collection: String
|
||||
set(value) {
|
||||
field = value
|
||||
this._collection = value
|
||||
}
|
||||
get() = if (this._collection.isNotEmpty())
|
||||
this._collection
|
||||
|
||||
@@ -1,23 +1,20 @@
|
||||
package com.synebula.gaea.query.mongo
|
||||
|
||||
import com.synebula.gaea.log.ILogger
|
||||
import com.synebula.gaea.query.*
|
||||
import org.springframework.data.domain.Sort
|
||||
import com.synebula.gaea.query.IQueryTyped
|
||||
import com.synebula.gaea.query.PagingData
|
||||
import com.synebula.gaea.query.PagingParam
|
||||
import org.springframework.data.mongodb.core.MongoTemplate
|
||||
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
|
||||
|
||||
/**
|
||||
* 实现IQuery的Mongo查询类
|
||||
* @param repo MongoRepo对象
|
||||
*/
|
||||
|
||||
open class MongoTypedQuery<TView>(var repo: MongoTemplate) : ITypedQuery<TView, String>, IMongoQuery<TView> {
|
||||
override var logger: ILogger? = null
|
||||
open class MongoQueryTyped(var repo: MongoTemplate, override var logger: ILogger? = null) : IQueryTyped, IMongoQuery {
|
||||
|
||||
override fun list(params: Map<String, Any>?, clazz: Class<TView>): List<TView> {
|
||||
override fun <TView, TKey> list(params: Map<String, Any>?, clazz: Class<TView>): List<TView> {
|
||||
val viewFields = this.fields(clazz)
|
||||
val query = Query()
|
||||
this.where(query, params, clazz)
|
||||
@@ -25,12 +22,12 @@ open class MongoTypedQuery<TView>(var repo: MongoTemplate) : ITypedQuery<TView,
|
||||
return this.repo.find(query, clazz, clazz.simpleName)
|
||||
}
|
||||
|
||||
override fun count(params: Map<String, Any>?, clazz: Class<TView>): Int {
|
||||
override fun <TView, TKey> count(params: Map<String, Any>?, clazz: Class<TView>): Int {
|
||||
val query = Query()
|
||||
return this.repo.count(this.where(query, params, clazz), clazz.simpleName).toInt()
|
||||
}
|
||||
|
||||
override fun paging(params: PagingParam, clazz: Class<TView>): PagingData<TView> {
|
||||
override fun <TView, TKey> paging(params: PagingParam, clazz: Class<TView>): PagingData<TView> {
|
||||
val viewFields = this.fields(clazz)
|
||||
val result = PagingData<TView>(1, 10)
|
||||
result.size = params.size
|
||||
@@ -45,8 +42,7 @@ open class MongoTypedQuery<TView>(var repo: MongoTemplate) : ITypedQuery<TView,
|
||||
return result
|
||||
}
|
||||
|
||||
override fun get(key: String, clazz: Class<TView>): TView? {
|
||||
override fun <TView, TKey> get(key: TKey, clazz: Class<TView>): TView? {
|
||||
return this.repo.findOne(idQuery(key), clazz, clazz.simpleName)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.synebula.gaea.repository.mongo
|
||||
|
||||
import org.springframework.data.mongodb.core.query.Criteria
|
||||
import org.springframework.data.mongodb.core.query.Query
|
||||
import org.springframework.data.mongodb.core.query.isEqualTo
|
||||
|
||||
interface IMongoRepository {
|
||||
/**
|
||||
* 获取ID查询条件
|
||||
*
|
||||
* @param id 业务ID
|
||||
*/
|
||||
fun <TKey> idQuery(id: TKey): Query = Query(Criteria("_id").isEqualTo(id))
|
||||
}
|
||||
@@ -12,46 +12,39 @@ import org.springframework.data.mongodb.core.query.isEqualTo
|
||||
* @param repo MongoRepo对象
|
||||
*/
|
||||
class MongoRepository<TAggregateRoot : IAggregateRoot<String>>(private var repo: MongoTemplate)
|
||||
: IRepository<TAggregateRoot, String> {
|
||||
: IRepository<TAggregateRoot, String>, IMongoRepository {
|
||||
|
||||
/**
|
||||
* 仓储的对象类
|
||||
*/
|
||||
override var clazz: Class<TAggregateRoot>? = null
|
||||
/**
|
||||
* 仓储的对象类
|
||||
*/
|
||||
override var clazz: Class<TAggregateRoot>? = null
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param clazz 仓储Domain对象
|
||||
* @param repo MongoRepo对象
|
||||
*/
|
||||
constructor(clazz: Class<TAggregateRoot>, repo: MongoTemplate) : this(repo) {
|
||||
this.clazz = clazz
|
||||
}
|
||||
/**
|
||||
* 构造
|
||||
* @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 add(obj: TAggregateRoot) {
|
||||
this.repo.save(obj)
|
||||
}
|
||||
|
||||
override fun update(obj: TAggregateRoot) {
|
||||
this.repo.save(obj)
|
||||
}
|
||||
override fun update(obj: TAggregateRoot) {
|
||||
this.repo.save(obj)
|
||||
}
|
||||
|
||||
override fun remove(id: String) {
|
||||
this.repo.remove(idQuery(id), this.clazz!!)
|
||||
}
|
||||
override fun remove(id: String) {
|
||||
this.repo.remove(idQuery(id), this.clazz!!)
|
||||
}
|
||||
|
||||
override fun get(id: String): TAggregateRoot {
|
||||
return this.repo.findOne(idQuery(id), clazz!!) as TAggregateRoot
|
||||
}
|
||||
override fun get(id: String): TAggregateRoot {
|
||||
return this.repo.findOne(idQuery(id), clazz!!) as TAggregateRoot
|
||||
}
|
||||
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot {
|
||||
return this.repo.findOne(idQuery(id.toString()), clazz) as TAggregateRoot
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ID查询条件
|
||||
*
|
||||
* @param id 业务ID
|
||||
*/
|
||||
fun idQuery(id: String): Query = Query(Criteria("_id").isEqualTo(id))
|
||||
}
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot {
|
||||
return this.repo.findOne(idQuery(id.toString()), clazz) as TAggregateRoot
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,28 +12,23 @@ import org.springframework.data.mongodb.core.query.isEqualTo
|
||||
* @param repo MongoRepo对象
|
||||
*/
|
||||
open class MongoRepositoryTyped(private var repo: MongoTemplate)
|
||||
: IRepositoryTyped {
|
||||
: IRepositoryTyped, IMongoRepository {
|
||||
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> remove(id: TKey, clazz: Class<TAggregateRoot>) {
|
||||
this.repo.remove(idQuery(id), clazz)
|
||||
}
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> remove(id: TKey, clazz: Class<TAggregateRoot>) {
|
||||
this.repo.remove(idQuery(id), clazz)
|
||||
}
|
||||
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot {
|
||||
return this.repo.findOne(idQuery(id), clazz) as TAggregateRoot
|
||||
}
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> get(id: TKey, clazz: Class<TAggregateRoot>): TAggregateRoot {
|
||||
return this.repo.findOne(idQuery(id), clazz) as TAggregateRoot
|
||||
}
|
||||
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> update(obj: TAggregateRoot, clazz: Class<TAggregateRoot>) {
|
||||
this.repo.save(obj)
|
||||
}
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> update(obj: TAggregateRoot, clazz: Class<TAggregateRoot>) {
|
||||
this.repo.save(obj)
|
||||
}
|
||||
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> add(obj: TAggregateRoot, clazz: Class<TAggregateRoot>) {
|
||||
this.repo.save(obj)
|
||||
}
|
||||
override fun <TAggregateRoot : IAggregateRoot<TKey>, TKey> add(obj: TAggregateRoot, clazz: Class<TAggregateRoot>) {
|
||||
this.repo.save(obj)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取ID查询条件
|
||||
*
|
||||
* @param id 业务ID
|
||||
*/
|
||||
fun <TKey> idQuery(id: TKey): Query = Query(Criteria("_id").isEqualTo(id))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user