Skip to content

Instantly share code, notes, and snippets.

View nakamura-to's full-sized avatar

Toshihiro Nakamura nakamura-to

View GitHub Profile
@nakamura-to
nakamura-to / Script.java
Created August 8, 2024 23:00
Domaの複数行追加でonDuplicateKeyUpdate
@Dao
public interface DeptDao {
@MultiInsert(duplicateKeyType = DuplicateKeyType.UPDATE)
int insertOnDuplicateKeyUpdate(List<Dept> entities);
}
```
insert into DEPARTMENT as target (
DEPARTMENT_ID,
DEPARTMENT_NO,
@nakamura-to
nakamura-to / Application.kt
Last active July 27, 2024 12:58
Komapperで複数エンティティに共通のプロパティを使ってクエリを組み立てる方法 (SELECTだけでなくUPDATEやDELETEにも対応)
package org.komapper.quickstart
import org.komapper.core.dsl.Meta
import org.komapper.core.dsl.QueryDsl
import org.komapper.jdbc.JdbcDatabase
import java.time.LocalDateTime
fun main() {
val database = JdbcDatabase("jdbc:h2:mem:quickstart;DB_CLOSE_DELAY=-1")
@nakamura-to
nakamura-to / Application.kt
Created July 27, 2024 03:38
Komapperで複数エンティティに共通のプロパティを使ってクエリを組み立てる方法
package org.komapper.quickstart
import org.komapper.core.dsl.Meta
import org.komapper.core.dsl.QueryDsl
import org.komapper.core.dsl.expression.WhereDeclaration
import org.komapper.core.dsl.scope.WhereScope
import org.komapper.jdbc.JdbcDatabase
import java.time.LocalDateTime
fun main() {
@nakamura-to
nakamura-to / Application.kt
Created July 21, 2024 13:55
KomapperのEntityMetamodelのためにインタフェースを定義する案
import org.komapper.annotation.*
import org.komapper.core.dsl.expression.PropertyExpression
import org.komapper.core.dsl.expression.WhereDeclaration
import java.time.LocalDateTime
// EntityMetamodelのためのインタフェース
interface BiTemporal {
val validFrom: PropertyExpression<LocalDateTime, LocalDateTime>
val validTo: PropertyExpression<LocalDateTime, LocalDateTime>
}
@nakamura-to
nakamura-to / Application.kt
Created July 21, 2024 07:41
Komapperにおける複数クエリ共通の検索条件の定義方法 2
package org.komapper.quickstart
import org.komapper.annotation.*
import org.komapper.core.dsl.Meta
import org.komapper.core.dsl.QueryDsl
import org.komapper.core.dsl.expression.PropertyExpression
import org.komapper.core.dsl.expression.WhereDeclaration
import org.komapper.core.dsl.metamodel.EntityMetamodel
import org.komapper.jdbc.JdbcDatabase
import java.time.LocalDateTime
@nakamura-to
nakamura-to / Application.kt
Last active July 21, 2024 06:55
Komapperにおける複数クエリ共通の検索条件の定義方法
package org.komapper.quickstart
import org.komapper.annotation.*
import org.komapper.core.dsl.Meta
import org.komapper.core.dsl.QueryDsl
import org.komapper.core.dsl.expression.PropertyExpression
import org.komapper.core.dsl.expression.WhereDeclaration
import org.komapper.jdbc.JdbcDatabase
import java.time.LocalDateTime
@nakamura-to
nakamura-to / entities.kt
Last active July 20, 2024 01:23
クエリの比較 - Komapper版
@KomapperEntity
@KomapperTable("users")
data class User(
@KomapperId
val id: String,
val name: String,
val age: Int
)
@KomapperEntity
@nakamura-to
nakamura-to / ContextualAbstractions.kt
Created May 31, 2022 14:21
KotlinでScala3のContextual Abstractionsみたいなもの
// 下記の記事の最後のコード例をKotlinで書いてみた
// https://zenn.dev/hinastory/articles/c8d5267ea43ed6
package hello.kotlin
data class Person(val name: String, val age: Int)
data class Japanese(val name: String, val age: Int, val info: String)
interface CanGreet<T> {
fun T.hello()
@nakamura-to
nakamura-to / AssociationExample.kt
Created November 3, 2021 14:10
Komapperでエンティティの関連づけを行うAPIの例
val d = Dept.meta
val e = Emp.meta
val query = SqlDsl.from(d).innerJoin(e){ d.id eq e.deptId }.associate(d, e)
val aggregate = db.runQuery { query }
val map: Map<Dept, Set<Emp>> = aggregate.oneToMany(d, e)
@nakamura-to
nakamura-to / Address.java
Last active October 30, 2021 08:50
Domaで@embeddableの代替策
public class Address {
public String ctiy;
public String street;
public Address(String city, String street) {
this.city = ctiy;
this.street = street;
}
}