官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 吕梁市| 枞阳县| 确山县| 历史| 石家庄市| 汤阴县| 潼南县| 耒阳市| 松潘县| 雷州市| 裕民县| 隆回县| 新郑市| 厦门市| 威海市| 海林市| 滦南县| 凤阳县| 石渠县| 监利县| 玉溪市| 扎兰屯市| 瑞丽市| 肇庆市| 额敏县| 象州县| 利辛县| 宁城县| 息烽县| 休宁县| 太湖县| 乐陵市| 隆德县| 嘉善县| 永登县| 柳林县| 和龙市| 贵南县| 安图县| 沂南县| 丹阳市|