티스토리 뷰

간단한 건 findBy로 퉁칠수 있다. 

 

하지만 그렇지 않다면 @Query를 사용하거나 QueryDsl을 사용해야 한다.

 

다양한 예제를 통해서 설명을 하면

 

//숫자 반환
@Query("select count(*) from ㅌㅌㅌㅌ where title like concat('%', :title, '%')")
    suspend fun findAllByTitleContainingCount(title: String):Long
    

// 게시물 검색, 여러 리스트 
    @FlowPreview //여러개일때 붙임 
    @Query("  select row_number() over ( order by id) as idx, A.id, A.title, A.content,A.created_at \n" +
            "         from ㅌㅌㅌㅌㅌ A \n" +
            "         Where 1 = 1" +
            "              and (\n" +
            "    case when :title != ''\n" +
            "        then title like concat('%',:title , '%') \n" +
            "    else 1 = 1 end \n" +
            "    ) " +
            "        order by created_at desc         \n" +
            "        Limit :start, :end"
    )
    fun findByBoardList(@Param("start") start:Long, @Param("end") end:Long, @Param("title") title: String ):Flow<ㅌㅌㅌㅌㅌ>
    
    
   
// 리스트 형태로 부르는 방법은 이것도 가능 
@Query(
        value="""
            select * from ㅌㅌㅌㅌ
        """
    )
    suspend fun selectㅌㅌㅌㅌㅌ():List<ㅌㅌㅌㅌ>

 

수정이나 삭제는 @Modifiying이 있어야 한다.

// 게시물 수정
    @Modifying
    @Query(
        """
            update ㄴㄴㄴㄴㄴㄴ 
            SET
                content = :content, 
                title = :title,
                updated_at = current_timestamp()
            WHERE 
                 id = :id
        """
    )
    suspend fun updateContentTitleById(@Param("id")  id:Long, @Param("content")content:String,@Param("title") title:String):Int

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함