Jpa関連
@Queryを使う
jpaを使う時、もし手書きsqlを使うなら@Query注解を使ってもいいです.元のsqlを使う時はパラメータnative Queryをtrueにします.
パラメータを注入する方式方式の一:使う? 方式2:@Param(org.springframe ewark.data.repository.query.Param)を使う 方式3:使用対象
変更と削除
@Queryを使って修正と削除をする場合は@Modifying、@Transationを追加する必要があります.
jpaを使う時、もし手書きsqlを使うなら@Query注解を使ってもいいです.元のsqlを使う時はパラメータnative Queryをtrueにします.
パラメータを注入する方式
@Query("select * from block where height > ?2 order by height desc limit ?1", nativeQuery = true)
fun queryForIndex(size: Int, startHeight: Long): List<Block>
@Query("select * from block where height > :startHeight order by height desc limit :size", nativeQuery = true)
fun queryForIndex(@Param("size") size: Int, @Param("startheight") startHeight: Long): List<Block>
@Query("select * from Block where height > :#{po.startHeight} order by height desc limit :#{po.size}")
fun queryForIndex(po: IndexPO) startHeight: Long): List<Block>
注意:対象を使う時、native Queryパラメータはfalseで、実際に私達が書いたHQL文で、Blockは対象名で表名ではありません.本猿はjpaプロジェクトでも複雑な動きsqlを使う必要があったが、最終的にはプロジェクトの中で同時に引用したjpaとmybatisで、複雑なsqlクエリ(ダイナミック条件が多い)を書く時には、mybatisのほうが便利で効率的です.変更と削除
@Queryを使って修正と削除をする場合は@Modifying、@Transationを追加する必要があります.
@Query(value = "delete from contract_url where contract_address = ?1 ", nativeQuery = true)
@Modifying
@Transactional
fun deleteByContractAddress(contractAddress: String)
@Modifying:削除を修正するには、このコメントを追加する必要があります.@Transaction: デフォルトの場合、repositoryインターフェースのCRUD方法は@Transactionalに注釈されて修飾されています.読む操作方法に対して@Transactionに注釈されたreadOnly属性はtrueに設定されている、つまり読み取り専用です.CRUD内の他の方法は@Transactionによって修飾されています.つまり読み取り専用ではありません.もしあなたがrepositoryインターフェースのいくつかの方法のトランザクション属性を修正する必要があるなら、この方法に@Transationコメントを追加し、必要な属性を設定することができます./**
* {@code true} if the transaction is read-only.
* Defaults to {@code false}.
*
This just serves as a hint for the actual transaction subsystem;
* it will not necessarily cause failure of write access attempts.
* A transaction manager which cannot interpret the read-only hint will
* not throw an exception when asked for a read-only transaction
* but rather silently ignore the hint.
* @see org.springframework.transaction.interceptor.TransactionAttribute#isReadOnly()
*/
boolean readOnly() default false;