Doma2を使ってみる(その5)
検索3
ストリーム検索
方法は2つある。
- 関数にストリームを渡す
- ストリームを返す
関数にストリームを渡す
// 検証エラーが発生する
ストリームを返す
kotlin entity.kt
DAO
```kotlin dao.kt
@Dao
@ConfigAutowireable
interface ProductDao {
@Select
@Suppress(messages = [Message.DOMA4274])
fun selectAllStream(): Stream<ProductEntity>
}
main関数
kotlin main.kt
fun main(args: Array<String>) {
val context: ApplicationContext = AnnotationConfigApplicationContext(
ShoppingApplication::class.java)
val productDao: ProductDao = context.getBean(ProductDao::class.java)
val products = productDao.selectAllStream()
products.use {
it.forEach { product ->
println(product)
}
}
}
実行結果
> Task :bootRun
20:16:16.419 [main] INFO org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory -- Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
20:16:16.513 [main] WARN org.seasar.doma.boot.autoconfigure.DomaAutoConfiguration -- StandardDialect was selected because no explicit configuration and it is not possible to guess from 'spring.datasource.url property'
ProductEntity(id=p01, name=消しゴム, price=100, stock=10)
ProductEntity(id=p02, name=ノート, price=200, stock=20)
ProductEntity(id=p03, name=pname03, price=300, stock=30)
ProductEntity(id=p04, name=pname04, price=400, stock=40)
ProductEntity(id=p05, name=pname05, price=500, stock=50)
ProductEntity(id=p06, name=pname06, price=600, stock=60)
ProductEntity(id=p07, name=pname07, price=700, stock=70)
ProductEntity(id=p08, name=pname08, price=800, stock=80)
ProductEntity(id=p09, name=pname09, price=900, stock=90)
ProductEntity(id=p10, name=pname10, price=1000, stock=100)
ProductEntity(id=p11, name=pname11, price=1100, stock=110)
ProductEntity(id=p12, name=pname12, price=1200, stock=120)
ProductEntity(id=p13, name=pname13, price=1300, stock=130)
java.sql.ResultSet 、 java.sql.PreparedStatement 、 java.sql.Connection などのリソースを
適切に閉じるために、必ず Stream を閉じる必要がある。
Kotlinではuse関数を使って、自動的にクローズ処理を行うことができため、useを使う。
コレクター検索
DAO
kotlin dao.kt
@Dao
@ConfigAutowireable
interface ProductDao {
@Select(strategy = SelectType.COLLECT)
fun <RESULT> selectAllCollect(collector: Collector<ProductEntity, *, RESULT>): RESULT
}
main関数
kotlin main.kt
fun main(args: Array<String>) {
val context: ApplicationContext = AnnotationConfigApplicationContext(
ShoppingApplication::class.java)
val productDao: ProductDao = context.getBean(ProductDao::class.java)
val products = productDao.selectAllCollect(Collectors.toList())
products.forEach { product ->
println(product)
}
}
実行結果
> Task :bootRun
22:23:29.619 [main] INFO org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory -- Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
22:23:29.706 [main] WARN org.seasar.doma.boot.autoconfigure.DomaAutoConfiguration -- StandardDialect was selected because no explicit configuration and it is not possible to guess from 'spring.datasource.url property'
p12=[ProductEntity(id=p12, name=pname12, price=1200, stock=120)]
p01=[ProductEntity(id=p01, name=消しゴム, price=100, stock=10)]
p11=[ProductEntity(id=p11, name=pname11, price=1100, stock=110)]
p03=[ProductEntity(id=p03, name=pname03, price=300, stock=30)]
p13=[ProductEntity(id=p13, name=pname13, price=1300, stock=130)]
p02=[ProductEntity(id=p02, name=ノート, price=200, stock=20)]
p05=[ProductEntity(id=p05, name=pname05, price=500, stock=50)]
p04=[ProductEntity(id=p04, name=pname04, price=400, stock=40)]
p07=[ProductEntity(id=p07, name=pname07, price=700, stock=70)]
p06=[ProductEntity(id=p06, name=pname06, price=600, stock=60)]
p09=[ProductEntity(id=p09, name=pname09, price=900, stock=90)]
p08=[ProductEntity(id=p08, name=pname08, price=800, stock=80)]
p10=[ProductEntity(id=p10, name=pname10, price=1000, stock=100)]
動的に生成されたコレクターを使って、結果を任意の形式で収集することができる。