티스토리 뷰
gradle 설정
//MongoDB
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
다음으로 ymal 설정
spring:
data:
mongodb:
host: localhost
port: 27017
database: mudspoon
Domain 설정(entity Vo 등의 이름..)
@Document(collection = "t_lounge")
data class LoungeEntity (
@Id
@GeneratedValue
val id : Long? = 0,
@Column(name="auther")
val auther :String? = "",
@Column(name="contents")
val contents :String?= "",
@Column(name="updated_at")
val updatedAt: LocalDateTime? = LocalDateTime.now(),
@Column(name="created_at")
val createdAt: LocalDateTime? = LocalDateTime.now()
)
@Document(collection = "t_lounge") 처럼 collection 안에 들어갈 내용은 테이블 이름이다.
package com.lunastratos.mudspoon.Repository
import com.lunastratos.mudspoon.Entity.LoungeEntity
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.stereotype.Repository
@Repository
interface LoungeRepository : MongoRepository<LoungeEntity, Long>
Repository 설정
package com.lunastratos.mudspoon.Service
import com.lunastratos.mudspoon.Entity.LoungeEntity
import com.lunastratos.mudspoon.Repository.LoungeRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
@Service
class LoungeService @Autowired constructor(
private val loungeRepo: LoungeRepository
){
fun save(entity:LoungeEntity){
loungeRepo.save(entity)
}
}
서비스 설정
@Test
fun MongoDBConnectionTest() {
val time = LocalDateTime.now()
val insertData = LoungeEntity(
1,
"orion스타",
"The quick brown fox jumps over a(the) lazy dog",
time,
time
)
"2021-01-01"
loungeRepo.save(insertData)
}
Junit에서 단위 테스트를 해본다.
그렇게 어렵지는 않은데, RDB와 개념이 다르니 그 부분에서 좀 골싸맬거 같다.