- Kotlin Blueprints
- Ashish Belagali Hardik Trivedi Akshay Chordiya
- 251字
- 2021-07-02 21:50:18
CrudRepository
This defines the structure of the functions for performing create, read, update, and delete operations on the table, including functions exclusively related to geospatial nature:
/**
* Basic CRUD operations related to Geospatial
*/
interface CrudRepository<T, K> {
/**
* Creates the table
*/
fun createTable()
/**
* Insert the item
*/
fun insert(t: T): T
/**
* Get list of all the items
*/
fun findAll(): Iterable<T>
/**
* Delete all the items
*/
fun deleteAll(): Int
/**
* Get list of items in the specified box
*/
fun findByBoundingBox(box: PGbox2d): Iterable<T>
/**
* Update the location of the user
*/
fun updateLocation(userName: K, location: Point)
}
The MessageRepository handles the actual interaction with the database; in our case, it performs the CRUD operations on the Messages table:
/**
* @inheritDoc
*/
interface MessageRepository: CrudRepository<Message, Int>
/**
* @inheritDoc
*/
@Repository
@Transactional
class DefaultMessageRepository : MessageRepository {
/**
* @inheritDoc
*/
override fun createTable() = SchemaUtils.create(Messages)
/**
* @inheritDoc
*/
override fun insert(t: Message): Message {
t.id = Messages.insert(insertQuery(t))[Messages.id]
return t
}
/**
* @inheritDoc
*/
override fun findAll() = Messages.selectAll().map {
it.getMessage() }
/**
* @inheritDoc
*/
override fun findByBoundingBox(box: PGbox2d) = Messages.select {
Messages.location within box }.map { it.getMessage() }
/**
* @inheritDoc
*/
override fun updateLocation(id:Int, location: Point) {
location.srid = 4326
Messages.update({ Messages.id eq id}) { it[Messages.location]
= location}
}
/**
* @inheritDoc
*/
override fun deleteAll() = Messages.deleteAll()
}
There are lots of advantages of using the Repository pattern. We recommend you read more about it.
推薦閱讀
- ClickHouse性能之巔:從架構設計解讀性能之謎
- 數字媒體應用教程
- CMDB分步構建指南
- JavaFX Essentials
- 深入淺出Windows API程序設計:編程基礎篇
- Hands-On Microservices with Kotlin
- Oracle從入門到精通(第5版)
- Angular開發入門與實戰
- Kotlin開發教程(全2冊)
- CRYENGINE Game Development Blueprints
- Python從入門到精通(第3版)
- 從Power BI到Analysis Services:企業級數據分析實戰
- 奔跑吧 Linux內核
- 嵌入式Linux C語言程序設計基礎教程
- Oracle SOA Suite 12c Administrator's Guide