Spring Boot වල MySql Database Queries භාවිතා කරන්නේ කොහමද ??

Seven Net

Active member
  • Jan 13, 2014
    290
    62
    28
    30
    Earth
    Spring Boot වල MySql Database Queries භාවිතා කරන්නේ කොහමද ??

    මම Spring Boot භාවිතා කරල API එකක් හදලයි තියේන්නේ. මෙහිදි පාවිච්චි කරේ Spring JPA. මේ API එකේ GET request එකේදි දැනට table තියේන සියලුම Data පෙන්වනවා. මට ඕන , සියලුම Data නැතුව Columns 3ක් විතරක් පෙන්වන්න. ඒ කිව්වේ, JPA එක්ක, මේ වගේ "SELECT devname,hrs,ot FROM imaginaryTable" එකක් පාවිච්චි කරන්නේ කොහමද ??

    Controller class එක.

    Code:
    @RestController
    @RequestMapping("/api")
    public class ImController {
    
        @Autowired
        private ImRepository TaskRepository;
    
        @GetMapping("/projects")
        public List<ImModel> findAll() {
            return (List<ImModel>) TaskRepository.findAll();
        }
    
        @GetMapping("/developers/{id}")
        public ImModel findByName(@PathVariable final int id){
            return TaskRepository.findById(id);
        }
    
    }

    Repository interface එක.

    Code:
    package com.kisalka.pacrestapi.repository;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    
    import com.kisalka.pacrestapi.model.ImModel;
    
    public interface ImRepository extends JpaRepository<ImModel, Integer> {
    
        ImModel findById(int id);
    
    }
     

    u_make_me_sick_

    Well-known member
  • Oct 1, 2011
    11,522
    7,320
    113
    JPA nethuwa JOOQ wage framework ekak use karoth wede lesiyenma goda daganna puluwan, But oyata jooq query eka liyanna wenawa.
     

    yakaa

    Active member
  • Mar 5, 2016
    160
    31
    28
    Colombo
    @Query annotation eka daala, oyata one query eka liyanna.

    example:

    public final String EXISTING_KEY_TYPE_BY_RES = "SELECT a FROM abc WHERE a.x=:xy";


    @Query(EXISTING_KEY_TYPE_BY_RES)
    public DTOTest getKeyTypeByResId(@Param("xy") String param);
     
    Last edited:

    u_make_me_sick_

    Well-known member
  • Oct 1, 2011
    11,522
    7,320
    113
    @Query annotation eka daala, oyata one query eka liyanna.

    example:

    public final String EXISTING_KEY_TYPE_BY_RES = "SELECT a FROM abc WHERE a.x=:xy";


    @Query(EXISTING_KEY_TYPE_BY_RES)
    public DTOTest getKeyTypeByResId(@Param("xy") String param);

    Native query liyana eka therumak ne, Spring JPA full support thiyanawa.
     

    හෙනයා

    Well-known member
  • May 23, 2014
    16,876
    17,087
    113
    Kottawa
    ay therumak nette? Spring data support eka thiyenawa findBy query walata nam
    eth meyata lesiyenma column tika select karaganna one nam
    @Query daala liwwata eke kisi weraddak nehe.


    named query, native query පුළුවන් තරම් use කරන එක අඩු කරන එක හොදයි.. මොකද පස්සේ maintain කරන්න ගියාම එපා වෙනවා.. මොකක්හරි table එකක structure එක වෙනස් කලොත් එකට කරලා තියෙන queries හොය හොය edit කරන්න වෙනවා.. db එක මාරු කලොත් native query නම් ආයේ මුල ඉදන් වෙනස් කරන්න වෙයි.. :confused::confused:
     

    හෙනයා

    Well-known member
  • May 23, 2014
    16,876
    17,087
    113
    Kottawa

    ක්‍රම දෙකක් තියේ.. interface based, class based(DTO)

    interface based තමා ලේසිම ක්‍රමේ..

    මුලින්ම entity class එකේ තමුන්ට ඕනේ properties ටිකේ getters ටික දාලා interface එකක් හදන්න..



    මේ විදිහට



    ඊට පස්සේ මේ විදිහට අපේ repository එක ලියාගන්න..

    PHP:
    interface PersonRepo extends JpaRepository<Person, UUID>{
    
       List<NamesOnly> findAllByFirstName(String firstName);
    }

    උඩ JpaRepo එකේ generic අස්සට දාන්නේ entity class එක.. method එකේ return type එකේ generic අස්සට දාන්නේ හදාගත්ත interface එක..

    method එක Data JPA method naming conventions වලට අනුව ඕනේ විදිහට filters දදා ලියාගන්න පුළුවන්..


     
    Last edited: