mirror of
https://github.com/tursom/TursomServer.git
synced 2025-01-30 22:30:11 +08:00
add mongo module
This commit is contained in:
parent
028e5d39da
commit
655e698eb2
8
database/mongodb/build.gradle
Normal file
8
database/mongodb/build.gradle
Normal 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
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package cn.tursom.mongodb
|
||||
|
||||
interface MongoTemplate<T> {
|
||||
fun save(entity: T)
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package cn.tursom.mongodb.annotation
|
||||
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
annotation class Collection(val name: String)
|
@ -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.
|
||||
}
|
||||
|
||||
}
|
@ -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.
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user