Mysql查询结果对应的java参数接收

  1. 使用数组接收:您可以使用 Object[] 类型的数组来接收查询结果中的每一行数据。在这种情况下,数组的每个元素将包含查询结果中的一个列的值。例如:

    @Repository
    public interface HazardApplicationRepository extends JpaRepository<HazardApplication, Long> {
        @Query(value = "SELECT t.type, count(*) FROM tb_hazard_application t WHERE t.deleted = false GROUP BY t.type ORDER BY t.type ASC", nativeQuery = true)
        List<Object[]> countByType();
    }

    然后,您可以在代码中使用索引来访问每个数组元素的值,如下所示:

    List<Object[]> result = hazardApplicationRepository.countByType();
       for (Object[] row : result) {
           String type = (String) row[0];
           Long count = (Long) row[1];
           // do something with type and count
       }

    2.使用 DTO 对象接收:您可以定义一个 Java 类来表示查询结果中的每一行数据,并使用该类的实例来接收查询结果。在这种情况下,DTO 类的属性名称应与查询结果中的列名相匹配。例如:

        public class CountByTypeResult {
            private String type;
            private Long count;
    ​
            // getters and setters
    ​
        }
    ​
        @Repository
        public interface HazardApplicationRepository extends JpaRepository<HazardApplication, Long> {
            @Query(value = "SELECT t.type, count(*) FROM tb_hazard_application t WHERE t.deleted = false GROUP BY t.type ORDER BY t.type ASC", nativeQuery = true)
            List<CountByTypeResult> countByType();
        }

    然后,您可以在代码中使用 DTO 对象的属性来访问查询结果中的每个列的值,如下所示:

    List<CountByTypeResult> result = hazardApplicationRepository.countByType();
    for (CountByTypeResult row : result) {
        String type = row.getType();
        Long count = row.getCount();
        // do something with type and count
    }

    3.使用List<Map<String, Object>>对象接收:这种接收方式可以把数据库中的字段及结果都查询出来

    @Repository
    public interface HazardApplicationRepository extends JpaRepository<HazardApplication, Long> {
        @Query(value = "SELECT t.type, count(*) FROM tb_hazard_application t WHERE t.deleted = false GROUP BY t.type ORDER BY t.type ASC", nativeQuery = true)
        List<Map<String, Object>> countByType();
    }

    在这个示例中,我们更新了 countByType() 方法的返回类型,以便返回一个 List<Map<String, Object>> 对象。在查询结果中,每一行的数据将被映射到一个 Map<String, Object> 对象中,并添加到返回的列表中。

    然后,您可以在代码中使用键来访问每个映射条目的值,如下所示:

    List<Map<String, Object>> result = hazardApplicationRepository.countByType();
    for (Map<String, Object> row : result) {
        String type = (String) row.get("type");
        Long count = (Long) row.get("count(*)");
        // do something with type and count
    }

    4.使用JSONObject对象接收:这种接收方式可以把数据库中的字段及结果都查询出来

    @Repository
    public interface HazardApplicationRepository extends JpaRepository<HazardApplication, Long> {
        @Query(value = "SELECT t.type, count(*) FROM tb_hazard_application t WHERE t.deleted = false GROUP BY t.type ORDER BY t.type ASC", nativeQuery = true)
        List<JSONObject> countByType();
    }

    在这个示例中,我们更新了 countByType() 方法的返回类型,以便返回一个 List<JSONObject> 对象。在查询结果中,每一行的数据将被映射到一个 List<JSONObject> 对象中,并添加到返回的列表中。

    然后,您可以在代码中使用键来访问每个映射条目的值,如下所示:

    List<JSONObject> result = hazardApplicationRepository.countByType();
    for (JSONObject row : result) {
        String type = (String) row.get("type");
        Long count = (Long) row.get("count(*)");
        // do something with type and count
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用\[1\]中提到,在MySQL中,使用tinyint(1)来设置表中字段的数据类型,可以存储0和1。在Java中,可以使用Boolean类型来接收,0映射为false,1映射为true。而如果使用tinyint来设置字段的数据类型,映射到Java数据类型中,不仅可以使用Boolean类型来接收,也可以使用int类型来接收。\[3\]所述的情况是,当tinyint(1)存储非0和1的数字时,查询结果会映射为Boolean类型,即0映射为false,1-9数字都将映射为true。如果希望单纯地查询出数字,可以将字段类型设置为tinyint而不是tinyint(1)。\[3\] #### 引用[.reference_title] - *1* [理解误区——mysql中tinyint与Java的数据类型的对应关系;tinyint(1) 与tinyint(4)的区别](https://blog.csdn.net/qq_42937522/article/details/104131892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Mysql中tinyint与Java数据类型对应关系](https://blog.csdn.net/LZ15932161597/article/details/110284630)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值