How to use MySql Database queries in Spring Boot?

前端 未结 4 2052
攒了一身酷
攒了一身酷 2021-01-15 04:45

I am creating an API by using spring boot. In this project, I used spring web, JPA, jstl and MySql as dependencies of the API. In this project, I have created a Controller,

4条回答
  •  离开以前
    2021-01-15 05:18

    You can create an object by using the columns as parameters for a constructor.

    I'll give you an example of my own with a custom DTO I made:

    @Query("SELECT new org.twinnation.site.dto.TitleAndDescriptionAndId(a.title, a.description, a.id) "
          + "FROM Article a")
    List getAllArticlesWithoutContent();
    

    Where the DTO TitleAndDescriptionAndId is the following:

    public class TitleAndDescriptionAndId {
    
        private String title;
        private String description;
        private Long id;
    
    
        public TitleAndDescriptionAndId(String title, String description, Long id) {
            this.title = title;
            this.description = description;
            this.id = id;
        }
    
        // ...
    
    }
    

提交回复
热议问题