add mongo module

This commit is contained in:
tursom 2020-02-18 16:53:29 +08:00
parent 028e5d39da
commit 655e698eb2
6 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,8 @@
dependencies {
implementation project(":")
// https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.12.1'
//
api group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: kotlinVersion
}

View File

@ -0,0 +1,16 @@
package cn.tursom.mongodb
import cn.tursom.mongodb.annotation.Collection
import com.mongodb.MongoClient
interface MongoConnection {
val client: MongoClient
fun <T> createTemplate(clazz: Class<T>): MongoTemplate<T>
companion object {
fun collectionName(clazz: Class<*>): String {
return clazz.getAnnotation(Collection::class.java)?.name ?: clazz.simpleName
}
}
}

View File

@ -0,0 +1,5 @@
package cn.tursom.mongodb
interface MongoTemplate<T> {
fun save(entity: T)
}

View File

@ -0,0 +1,5 @@
package cn.tursom.mongodb.annotation
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
annotation class Collection(val name: String)

View File

@ -0,0 +1,21 @@
package cn.tursom.mongodb.impl
import cn.tursom.mongodb.MongoConnection
import cn.tursom.mongodb.MongoTemplate
import com.mongodb.MongoClient
class MongoConnectionImpl(
db: String,
val host: String = "127.0.0.1",
val port: Int = 27017
) : MongoConnection {
override val client: MongoClient = MongoClient(host, port)
val db = client.getDatabase(db)
override fun <T> createTemplate(clazz: Class<T>): MongoTemplate<T> {
db.createCollection(MongoConnection.collectionName(clazz))
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}

View File

@ -0,0 +1,11 @@
package cn.tursom.mongodb.impl
import cn.tursom.mongodb.MongoTemplate
class MongoTemplateImpl<T>(
) : MongoTemplate<T> {
override fun save(entity: T) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}