1.4.0 增加jpa的代理模块
This commit is contained in:
@@ -3,11 +3,3 @@ dependencies {
|
||||
api project(":src:gaea.spring")
|
||||
api("org.springframework.boot:spring-boot-starter-data-mongodb:$spring_version")
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
publish(MavenPublication) {
|
||||
from components.java
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ fun Query.select(fields: Array<String>): Query {
|
||||
* @param onWhere 获取字段查询方式的方法
|
||||
*/
|
||||
fun Query.where(
|
||||
params: Map<String, Any>?,
|
||||
params: Map<String, String>?,
|
||||
onWhere: ((v: String) -> Where?) = { null },
|
||||
onFieldType: ((v: String) -> Class<*>?) = { null }
|
||||
): Query {
|
||||
@@ -38,15 +38,15 @@ fun Query.where(
|
||||
if (params != null) {
|
||||
for (param in params) {
|
||||
val key = param.key
|
||||
var value = param.value
|
||||
var value: Any = param.value
|
||||
|
||||
//日期类型特殊处理为String类型
|
||||
val fieldType = onFieldType(key)
|
||||
if (fieldType != null && value.javaClass != fieldType) {
|
||||
when (fieldType) {
|
||||
Date::class.java -> value = DateTime(value.toString(), "yyyy-MM-ddTHH:mm:ss").date
|
||||
Int::class.java -> value = value.toString().toInt()
|
||||
Integer::class.java -> value = value.toString().toInt()
|
||||
Date::class.java -> value = DateTime(param.value, "yyyy-MM-ddTHH:mm:ss").date
|
||||
Int::class.java -> value = param.value.toInt()
|
||||
Integer::class.java -> value = param.value.toInt()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,14 +58,15 @@ fun Query.where(
|
||||
val field = where.children.ifEmpty { key }
|
||||
var criteria = Criteria.where(field)
|
||||
criteria = when (where.operator) {
|
||||
Operator.eq -> criteria.`is`(value)
|
||||
Operator.ne -> criteria.ne(value)
|
||||
Operator.lt -> criteria.lt(value)
|
||||
Operator.gt -> criteria.gt(value)
|
||||
Operator.lte -> criteria.lte(value)
|
||||
Operator.gte -> criteria.gte(value)
|
||||
Operator.like -> criteria.regex(value.toString(), if (where.sensitiveCase) "" else "i")
|
||||
Operator.default -> tryRangeWhere(param.key, value, onFieldType)
|
||||
Operator.Eq -> criteria.`is`(value)
|
||||
Operator.Ne -> criteria.ne(value)
|
||||
Operator.Lt -> criteria.lt(value)
|
||||
Operator.Gt -> criteria.gt(value)
|
||||
Operator.Lte -> criteria.lte(value)
|
||||
Operator.Gte -> criteria.gte(value)
|
||||
Operator.Like -> criteria.regex(value.toString(), if (where.sensitiveCase) "" else "i")
|
||||
Operator.Range -> tryRangeWhere(param.key, value, onFieldType)
|
||||
Operator.Default -> tryRangeWhere(param.key, value, onFieldType)
|
||||
}
|
||||
list.add(if (where.children.isEmpty()) criteria else Criteria.where(key).elemMatch(criteria))
|
||||
}
|
||||
@@ -103,7 +104,7 @@ private fun tryRangeWhere(key: String, value: Any, onFieldType: ((v: String) ->
|
||||
*
|
||||
* @param params 参数列表
|
||||
*/
|
||||
fun Query.where(params: Map<String, Any>?, clazz: Class<*>): Query {
|
||||
fun Query.where(params: Map<String, String>?, clazz: Class<*>): Query {
|
||||
var field: Field?
|
||||
return this.where(params, { name ->
|
||||
field = clazz.declaredFields.find { it.name == name }
|
||||
@@ -4,11 +4,11 @@ import com.synebula.gaea.spring.autoconfig.Factory
|
||||
import com.synebula.gaea.spring.autoconfig.Proxy
|
||||
import org.springframework.beans.factory.BeanFactory
|
||||
|
||||
class MongodbRepoFactory(
|
||||
class MongodbRepositoryFactory(
|
||||
supertype: Class<*>,
|
||||
var beanFactory: BeanFactory,
|
||||
) : Factory(supertype) {
|
||||
override fun createProxy(): Proxy {
|
||||
return MongodbRepoProxy(supertype, this.beanFactory)
|
||||
return MongodbRepositoryProxy(supertype, this.beanFactory)
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import org.springframework.beans.factory.BeanFactory
|
||||
import org.springframework.data.mongodb.core.MongoTemplate
|
||||
import java.lang.reflect.Method
|
||||
|
||||
class MongodbRepoProxy(
|
||||
class MongodbRepositoryProxy(
|
||||
private var supertype: Class<*>, private var beanFactory: BeanFactory
|
||||
) : Proxy() {
|
||||
|
||||
@@ -9,14 +9,14 @@ import org.springframework.beans.factory.support.GenericBeanDefinition
|
||||
import org.springframework.core.annotation.AnnotationAttributes
|
||||
import org.springframework.core.type.AnnotationMetadata
|
||||
|
||||
class MongodbRepoRegister : Register() {
|
||||
class MongodbRepositoryRegister : Register() {
|
||||
override fun scan(metadata: AnnotationMetadata): Map<String, BeanDefinition> {
|
||||
val result = mutableMapOf<String, BeanDefinition>()
|
||||
|
||||
// 获取注解参数信息:basePackages
|
||||
val attributes = AnnotationAttributes(
|
||||
metadata.getAnnotationAttributes(
|
||||
MongodbRepoScan::class.java.name
|
||||
MongodbRepositoryScan::class.java.name
|
||||
) ?: mapOf()
|
||||
)
|
||||
val basePackages = attributes.getStringArray("basePackages")
|
||||
@@ -44,7 +44,7 @@ class MongodbRepoRegister : Register() {
|
||||
builder.addConstructorArgValue(beanClazz)
|
||||
builder.addConstructorArgValue(this._beanFactory)
|
||||
val definition = builder.rawBeanDefinition as GenericBeanDefinition
|
||||
definition.beanClass = MongodbRepoFactory::class.java
|
||||
definition.beanClass = MongodbRepositoryFactory::class.java
|
||||
definition.autowireMode = GenericBeanDefinition.AUTOWIRE_BY_TYPE
|
||||
result[beanClazz.name] = definition
|
||||
}
|
||||
@@ -59,7 +59,7 @@ class MongodbRepoRegister : Register() {
|
||||
builder.addConstructorArgValue(this._beanFactory)
|
||||
builder.addConstructorArgValue(emptyArray<String>())
|
||||
val definition = builder.rawBeanDefinition as GenericBeanDefinition
|
||||
definition.beanClass = MongodbRepoFactory::class.java
|
||||
definition.beanClass = MongodbRepositoryFactory::class.java
|
||||
definition.autowireMode = GenericBeanDefinition.AUTOWIRE_BY_TYPE
|
||||
result[IRepository::class.java.name] = definition
|
||||
}
|
||||
@@ -8,5 +8,5 @@ import java.lang.annotation.Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
@MustBeDocumented
|
||||
@Inherited
|
||||
@Import(MongodbRepoRegister::class)
|
||||
annotation class MongodbRepoScan(val basePackages: Array<String> = [])
|
||||
@Import(MongodbRepositoryRegister::class)
|
||||
annotation class MongodbRepositoryScan(val basePackages: Array<String> = [])
|
||||
@@ -31,7 +31,7 @@ open class MongodbQuery<TView, ID>(override var clazz: Class<TView>, var templat
|
||||
return this.template.findOne(whereId(id), clazz, this.collection(clazz))
|
||||
}
|
||||
|
||||
override fun list(params: Map<String, Any>?): List<TView> {
|
||||
override fun list(params: Map<String, String>?): List<TView> {
|
||||
val fields = this.fields(clazz)
|
||||
val query = Query()
|
||||
query.where(params, clazz)
|
||||
@@ -39,7 +39,7 @@ open class MongodbQuery<TView, ID>(override var clazz: Class<TView>, var templat
|
||||
return this.find(query, clazz)
|
||||
}
|
||||
|
||||
override fun count(params: Map<String, Any>?): Int {
|
||||
override fun count(params: Map<String, String>?): Int {
|
||||
val query = Query()
|
||||
return this.template.count(query.where(params, clazz), this.collection(clazz)).toInt()
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ open class MongodbUniversalQuery(var template: MongoTemplate) : IUniversalQuery
|
||||
return this.template.findOne(whereId(id), clazz, this.collection(clazz))
|
||||
}
|
||||
|
||||
override fun <TView> list(params: Map<String, Any>?, clazz: Class<TView>): List<TView> {
|
||||
override fun <TView> list(params: Map<String, String>?, clazz: Class<TView>): List<TView> {
|
||||
val fields = this.fields(clazz)
|
||||
val query = Query()
|
||||
query.where(params, clazz)
|
||||
@@ -38,7 +38,7 @@ open class MongodbUniversalQuery(var template: MongoTemplate) : IUniversalQuery
|
||||
return this.find(query, clazz)
|
||||
}
|
||||
|
||||
override fun <TView> count(params: Map<String, Any>?, clazz: Class<TView>): Int {
|
||||
override fun <TView> count(params: Map<String, String>?, clazz: Class<TView>): Int {
|
||||
val query = Query()
|
||||
return this.template.count(query.where(params, clazz), this.collection(clazz)).toInt()
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ open class MongodbRepository<TAggregateRoot : IAggregateRoot<ID>, ID>(
|
||||
this.repo.save(list)
|
||||
}
|
||||
|
||||
override fun count(params: Map<String, Any>?): Int {
|
||||
override fun count(params: Map<String, String>?): Int {
|
||||
val query = Query()
|
||||
return this.repo.count(query.where(params, clazz), clazz).toInt()
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ open class MongodbUniversalRepository(private var repo: MongoTemplate) : IUniver
|
||||
this.repo.insert(roots, clazz)
|
||||
}
|
||||
|
||||
override fun <TAggregateRoot> count(params: Map<String, Any>?, clazz: Class<TAggregateRoot>): Int {
|
||||
override fun <TAggregateRoot> count(params: Map<String, String>?, clazz: Class<TAggregateRoot>): Int {
|
||||
val query = Query()
|
||||
return this.repo.count(query.where(params, clazz), clazz).toInt()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user