欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringDataJpa寫原生sql遇到的問題及解決

 更新時間:2021年11月22日 11:26:34   作者:Irelia520  
這篇文章主要介紹了SpringDataJpa寫原生sql遇到的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SpringDataJpa寫原生sql遇到的問題

@Repository
public interface EduCourseDao extends JpaRepository<EduCourse,Long>, JpaSpecificationExecutor<EduCourse> {
    //根據(jù)課程id查詢課程的確認信息
    @Query(value = "SELECT ec.id,ec.title,ec.price,ec.lesson_num, " +
            "ecd.description, " +
            "es1.title AS oneSubject, " +
            "es2.title AS twoSubject " +
            "FROM edu_course ec LEFT JOIN edu_course_description ecd ON ec.id=ecd.id " +
            "LEFT JOIN edu_teacher et ON ec.teacher_id=et.id " +
            "LEFT JOIN edu_subject es1 ON ec.subject_parent_id=es1.id " +
            "LEFT JOIN edu_subject es2 ON ec.subject_id=es2.id " +
            "WHERE ec.id=?1 ",nativeQuery = true)
    List< Map<String,Object>> findPublishInfoById(String id);

}

因為涉及到其他表,所以返回類型使用List< Map<String,Object>> ,而不是用原先定義的vo類

每行最后的,與 " 之間要有一個空格,不然報錯。

Spring data jpa 自定義SQL語句遇到錯誤

Not supported for DML operations

今天在自定義一個Update語句時運行遇到一個錯誤,顯示Not supported for DML operations 也就是說不支持DML操作。

我的UserRepository是繼承的PagingAndSortingRepository接口,在看了JPA的文檔之后,發(fā)現(xiàn)此接口不支持update事務(wù),所以需要在注解上添加@Modifying。

原文如下:

3.3.7. Modifying queries

All the sections above describe how to declare queries to access a given entity or collection of entities.Of course you can add custom modifying behaviour by using facilities described in Customimplementations for Spring Data repositories. As this approach is feasible for comprehensive customfunctionality, you can achieve the execution of modifying queries that actually only need parameterbinding by annotating the query method with @Modifying:

Example 45. Declaring manipulating queries

@Modifying
@Query("update User u set u.firstname = ?1 where u.lastname = ?2")
int setFixedFirstnameFor(String firstname, String lastname);
This will trigger the query annotated to the method as updating query instead of a selecting one. As theEntityManager might contain outdated entities after the execution of the modifying query, we do notautomatically clear it (see JavaDoc of EntityManager.clear() for details) since this will effectively dropall non-flushed changes still pending in the EntityManager. If you wish the EntityManager to be clearedautomatically you can set @Modifying annotation's clearAutomatically attribute to true.

以下是我的源碼:

@Modifying
@Query("update User u set u.u_name=?2,u.u_sex=?3,u.u_date = ?4,u.u_minzu = ?5,u.u_area=?6,u.u_country=?7 where u.u_id =?1")
public void updateUserById(String u_id,String u_name,String u_sex,
                               String u_date, String u_minzu,String u_area,
                               String u_country);

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java發(fā)送郵件及打開狀態(tài)判斷分析實例

    java發(fā)送郵件及打開狀態(tài)判斷分析實例

    這篇文章主要為大家介紹了java發(fā)送郵件及打開狀態(tài)判斷分析實例
    2023-12-12
  • spring boot 命令行啟動的方式

    spring boot 命令行啟動的方式

    這篇文章主要介紹了spring boot 命令行啟動的方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • SpringBoot如何獲取Kafka的Topic列表

    SpringBoot如何獲取Kafka的Topic列表

    這篇文章主要介紹了SpringBoot如何獲取Kafka的Topic列表問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • SpringBoot下載文件的正確解法方式

    SpringBoot下載文件的正確解法方式

    這篇文章主要給大家介紹了關(guān)于SpringBoot下載文件的正確解法方式,SpringBoot是一款流行的框架,用于開發(fā)Web應(yīng)用程序,在使用SpringBoot構(gòu)建Web應(yīng)用程序時,可能需要實現(xiàn)文件下載的功能,需要的朋友可以參考下
    2023-08-08
  • SpringMVC實現(xiàn)文件上傳和下載功能

    SpringMVC實現(xiàn)文件上傳和下載功能

    這篇文章主要為大家詳細介紹了SpringMVC實現(xiàn)文件上傳和下載功能 ,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Java計算器核心算法代碼實現(xiàn)

    Java計算器核心算法代碼實現(xiàn)

    今天小編就為大家分享一篇關(guān)于Java計算器核心算法代碼實現(xiàn),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java int與integer的對比區(qū)別

    Java int與integer的對比區(qū)別

    這篇文章主要介紹了Java int與integer的對比區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法

    SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法

    這篇文章主要介紹了SpringBoot使用JdbcTemplate訪問操作數(shù)據(jù)庫基本用法,Spring對數(shù)據(jù)庫的操作在jdbc上s面做了深層次的封裝,使用spring的注入功能,可以把DataSource注冊到JdbcTemplate之中。下文詳細內(nèi)容需要的小伙伴可以參考一下
    2022-02-02
  • Java集合List與Array的相互轉(zhuǎn)換

    Java集合List與Array的相互轉(zhuǎn)換

    本篇文章主要介紹了Java集合List與Array的相互轉(zhuǎn)換,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • springboot后端解決跨域問題

    springboot后端解決跨域問題

    今天小編就為大家分享一篇關(guān)于springboot后端解決跨域問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03

最新評論