1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      SpringDataJpa原生SQL返回自定義對象最簡潔方式是什么

      本篇內(nèi)容介紹了“Spring Data Jpa原生SQL返回自定義對象最簡潔方式是什么”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

      創(chuàng)新互聯(lián)公司是一家專注于成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計,運河網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:運河等地區(qū)。運河做網(wǎng)站價格咨詢:028-86922220

      Spring Data Jpa 原生SQL返回自定義對象最簡潔方式

      此文章只討論兩種方式查詢, 使用jpa關(guān)鍵字查詢和自定義sql

        // 方式1	
        1. List findByName(String name);
        // 方式2
        2. @Query(value = "select name from t_users where name = ?", nativeQuery = true)
         List findByName2(String name);
      使用接口接收

      即通過定義一個接口接口 UserName,兩種方式都支持通過定義接口接受返回,JPA原生支持

      public interface UserName {
         String getNname();
      }
      自定義對象接收

      方式一 JAP原生支持自定義對象,但條件是而且只有一個構(gòu)造函數(shù),有些工具類需要用到默認構(gòu)造函數(shù),不方便

      方式二 JAP不支持自定義對象,會返回Object[] 對象數(shù)組

      解決方案

      例子只是解決方式二, 如果需要解決方式一構(gòu)造函數(shù)問題,可以借鑒,下面例子自己實現(xiàn) 我們定義一個接口ReabamDTO

      public interface ReabamDTO {
      }
      GenericConverter實現(xiàn)類

      內(nèi)容是map 轉(zhuǎn) ReabamDTO

      public final class JpaConvert implements GenericConverter {
      
         @Override
         public Set getConvertibleTypes() {
            return Collections.singleton(new ConvertiblePair(Map.class, ReabamDTO.class));
         }
      
         @Override
         public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            return map2Pojo(source, targetType.getObjectType());
         }
      
         public static  T map2Pojo(Object entity, Class tClass) {
            CopyOptions copyOptions = CopyOptions.create()
                  .setIgnoreCase(true)
                  .setIgnoreError(true)
                  .setIgnoreNullValue(true)
                  .setFieldNameEditor(StrUtil::toUnderlineCase);
            return BeanUtil.toBean(entity, tClass, copyOptions);
         }
      }

      將自定義convert加入到GenericConversionService

      @Configuration
      public class JpaConfig {
      
         @PostConstruct
         public void init() {
            GenericConversionService genericConversionService = ((GenericConversionService) DefaultConversionService.getSharedInstance());
            genericConversionService.addConverter(new JpaConvert());
         }
      }
      測試
      public interface UsersRepository extends JpaRepository {
         
          List findByName(String name);
          
         @Query(value = "select name from t_users where name = ?", nativeQuery = true)
         List findByName2(String name);
      }
      @Data
      @ToString
      @NoArgsConstructor
      public class UserName implements ReabamDTO {
         private String name;
      }
      @RunWith(SpringJUnit4ClassRunner.class)
      @SpringBootTest(classes = DemoApplication.class, properties = {"spring.profiles.active=local"})
      class DemoApplicationTests {
      
         @Autowired
         private UsersRepository usersRepository;
      
         @Test
         void contextLoads() {
            Users users = new Users();
            users.setName("A");
            users.setAge(1);
            users.setAddress("AA");
            usersRepository.save(users);
         }
      
         /**
          * 非自定義sql可以返回自定義對象
          */
         @Test
         void t2() {
            List a = usersRepository.findByName("A");
            System.out.println(a);
         }
      
         /**
          * 自定義sql 自定義對象
          */
         @Test
         void t3() {
            List a = usersRepository.findByName2("A");
            System.out.println(a);
         }
      }

      原理可選擇觀看

      查看報錯信息,找不到轉(zhuǎn)換器異常, 那么只需要加入對應(yīng)的裝換器就可以了,

      org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.architeture.demo.UserName]
      at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322)
      at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
      at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175)
      at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:309)
      at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.lambda$and$0(ResultProcessor.java:225)
      at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:236)
      at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:156)
      at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:158)
      at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:143)

      通過查看源碼, 獲取轉(zhuǎn)換器的位置 org.springframework.core.convert.support.GenericConversionService#getConverter

      @Nullable
      protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
         ConverterCacheKey key = new ConverterCacheKey(sourceType, targetType);
         GenericConverter converter = this.converterCache.get(key);
         if (converter != null) {
            return (converter != NO_MATCH ? converter : null);
         }
      
         converter = this.converters.find(sourceType, targetType);
         if (converter == null) {
            converter = getDefaultConverter(sourceType, targetType);
         }
      
         if (converter != null) {
            this.converterCache.put(key, converter);
            return converter;
         }
      
         this.converterCache.put(key, NO_MATCH);
         return null;
      }

      繼續(xù)看源碼, 查找能加入轉(zhuǎn)換器位置 converter = this.converters.find(sourceType, targetType);

      @Nullable
      public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) {
         // Search the full type hierarchy
         // 獲取 
         List> sourceCandidates = getClassHierarchy(sourceType.getType());
         List> targetCandidates = getClassHierarchy(targetType.getType());
         for (Class sourceCandidate : sourceCandidates) {
            for (Class targetCandidate : targetCandidates) {
               ConvertiblePair convertiblePair = new ConvertiblePair(sourceCandidate, targetCandidate);
               GenericConverter converter = getRegisteredConverter(sourceType, targetType, convertiblePair);
               if (converter != null) {
                  return converter;
               }
            }
         }
         return null;
      }

      Spring Data Jpa原生SQL返回自定義對象最簡潔方式是什么 Spring Data Jpa原生SQL返回自定義對象最簡潔方式是什么

      此方法 源對象TupBackedMap父類或接口和UserName父類或接口進行組合,獲取到對應(yīng)的轉(zhuǎn)換器

      TupBackedMap的父類或接口

      Spring Data Jpa原生SQL返回自定義對象最簡潔方式是什么

      org.springframework.core.convert.support.GenericConversionService#addConverter(org.springframework.core.convert.converter.GenericConverter)

      @Nullable
      private GenericConverter getRegisteredConverter(TypeDescriptor sourceType,
            TypeDescriptor targetType, ConvertiblePair convertiblePair) {
      
         // Check specifically registered converters
         ConvertersForPair convertersForPair = this.converters.get(convertiblePair);
         if (convertersForPair != null) {
            GenericConverter converter = convertersForPair.getConverter(sourceType, targetType);
            if (converter != null) {
               return converter;
            }
         }
         // Check ConditionalConverters for a dynamic match
         for (GenericConverter globalConverter : this.globalConverters) {
            if (((ConditionalConverter) globalConverter).matches(sourceType, targetType)) {
               return globalConverter;
            }
         }
         return null;
      }

      可以看到在 ConvertersForPair convertersForPair = this.converters.get(convertiblePair); 在convertes中加入轉(zhuǎn)換器

      org.springframework.core.convert.support.GenericConversionService.Converters

      private static class Converters {
      
         private final Map converters = new LinkedHashMap<>(36);
      
         public void add(GenericConverter converter) {
         }
      }

      查看調(diào)用 add 方法位置, org.springframework.core.convert.support.GenericConversionService#addConverter(org.springframework.core.convert.converter.GenericConverter)最終定位到這里

      “Spring Data Jpa原生SQL返回自定義對象最簡潔方式是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!


      本文名稱:SpringDataJpa原生SQL返回自定義對象最簡潔方式是什么
      本文來源:http://www.ef60e0e.cn/article/piscoj.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        清水河县| 湖北省| 澎湖县| 汝南县| 山西省| 偃师市| 博湖县| 石家庄市| 正阳县| 灵璧县| 苏尼特右旗| 尚义县| 浙江省| 图木舒克市| 安平县| 昭平县| 沙河市| 石林| 蒲江县| 定边县| 阿坝| 耒阳市| 澄迈县| 鹤山市| 麻阳| 松江区| 沁源县| 卫辉市| 望都县| 深泽县| 南丹县| 河间市| 灵宝市| 衡阳县| 石台县| 唐山市| 扎兰屯市| 道孚县| 兴义市| 深水埗区| 尤溪县|