Spring Data Jpa返回自定义对象的三种方法

tasks表对应的Entity

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "tasks")
@Data
public class Tasks extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int taskId;
    private Integer websiteId;
    private String status;
    private String lastOperator;
    private String lastOperationTime;
    private String jobId;
    private int retryTimes;
}

websites表对应的Entity

@Entity
@Table(name = "websites")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Websites extends BaseEntity{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int websiteId;
    private String country;
    private String websiteName;
    private String baseUrl;
    private String smartphoneUrl;
    private String tabletUrl;
    private String smartdeviceUrl;
    private String websiteCategory;
    private String webtypeRefreshRate;
    private String urlRefreshRate;
    private String configTime;
    private String configDesc;
}

自定义对象

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CustomizedDto implements Serializable {
    private static final long serialVersionUID = -7242005560621561106L;
    private String country;
    private String websiteName;
    private String baseUrl;
    private String status;
}

方法一、简单查询直接new对象

        使用hql,将结果返回到new出来的自定义对象中,注意,自定义对象中要有对应的构造函数。

@Query(value = "select new com.bigdata.mrcrawler.dto.CustomizedDto(w.country,w.websiteName,w.baseUrl,t.status) " +
            "from Websites w join Tasks t on w.id=t.websiteId " +
            "where t.status=?1")
    List<CustomizedDto> getByStatus1(String status);

        但是这个方法只使用于简单的查询语句,如果遇到复杂查询需要使用原生SQL(即nativeQuery=true)时, 此方法不适用,会抛出如下异常。

@Query(value = "select w.country as country,w.website_name as websiteName,w.base_url as baseUrl,t.status as status " +
            "from websites w join tasks t on w.website_id=t.website_id " +
            "where t.status=?1",nativeQuery = true)
    List<CustomizedDto> getByStatus2(String status);

No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.bigdata.mrcrawler.dto.CustomizedDto]

方法二、Service层使用EntityManager

        直接在Service层使用EntityManager进行查询,可以自由组装各种复杂sql。

    public List<CustomizedDto> t(String param) {
        String sql = "select w.country as country,w.website_name as websiteName,w.base_url as baseUrl,t.status as status " +
                "from websites w join tasks t on w.website_id=t.website_id " +
                "where t.status='" + param + "'";
        List<CustomizedDto> res = entityManager.createNativeQuery(sql).getResultList();
        return res;
    }

        但是会有sql注入问题,例如:param传入Running' or 1=1 --\t  上述查询会将查询整个数据表。解决方法如下,使用预编译防止sql注入问题。

    public List<CustomizedDto> t(String param) {
        String sql = "select w.country as country,w.website_name as websiteName,w.base_url as baseUrl,t.status as status " +
                "from websites w join tasks t on w.website_id=t.website_id " +
                "where t.status=:param" ;
        Query nativeQuery = entityManager.createNativeQuery(sql);
        nativeQuery.setParameter("param", param);
        List<CustomizedDto> res  = nativeQuery.getResultList();
        return res;
    }

        然而,个人很不喜欢这种代码中嵌入大片大片sql的写法,排查问题的时候看得头疼(别问,问就是被坑过/捂脸.jpg/)。所以方法二即便可行,我私心还是不想推荐。

方法三、Dao层使用Map接收自定义对象

        使用List<Map> 接收返回结果,无论是原生sql还是hql都支持,当然也支持分页,只需要把返回对象改为Page<Map>即可,其他操作与普通分页没有差别。

@Query(value = "select w.country as country,w.website_name as websiteName,w.base_url as baseUrl,t.status as status " +
            "from websites w join tasks t on w.website_id=t.website_id " +
            "where t.status=?1",nativeQuery = true)
    List<Map> getByStatus3(String status);

        Service层也需要用List<Map>进行接收。 

    public List<Map> t(String param) {
        return testRepository.getByStatus3(param);
    }

        如果后续还有其他操作,还是需要转成自定义对象怎么办,毕竟Map操作起来挺麻烦的。可以用如下解决方案,先用Object进行接收,然后强转成自定义对象List。

    public List<CustomizedDto> t(String param) {
        Object data = testRepository.getByStatus3(param);
        return  (List<CustomizedDto>)data;
    }

        强转需要注意自定义对象和数据库中字段类型的强一致性,如数据库中datetime类型,自定义对象对应的字段必须是Date,不能是String,否则转换的时候会有问题,数据会丢失。 

  • 6
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Spring Data JPA 中,我们可以通过定义 Repository 接口中的方法,使用方法名来自动生成查询语句。但是有时候,我们需要自定义查询结果,比如说只需要查询结果中的部分字段,或者对查询结果进行聚合操作等。这时,我们可以使用 Spring Data JPA 提供的投影(Projection)功能来自定义查询结果。 投影是指将实体类中的一部分属性或关联属性映射成一个接口或类,从而返回一个自定义的结果集。Spring Data JPA 支持三种投影方式: 1. 接口投影:定义一个接口,接口中声明需要的属性,Spring Data JPA 将根据接口定义的属性生成查询结果。 2. 类投影:定义一个类,类中声明需要的属性,Spring Data JPA 将根据类定义的属性生成查询结果。 3. 动态投影:可以根据查询条件动态地返回不同的投影结果,比如说根据用户的角色返回不同的查询结果。 下面是一个简单的例子,演示如何使用接口投影来自定义查询结果: 定义一个接口投影: ```java public interface UserProjection { String getUsername(); String getEmail(); } ``` 在 UserRepository 中使用接口投影: ```java public interface UserRepository extends JpaRepository<User, Long> { List<UserProjection> findByUsername(String username); } ``` 通过上面的代码,我们可以在 UserRepository 中定义一个方法 findByUsername,该方法返回一个 List<UserProjection> 类型的结果集,该结果集中只包含 username 和 email 两个字段。 当我们调用 findByUsername 方法时,Spring Data JPA 将根据方法名自动生成查询语句,并根据 UserProjection 接口中定义的属性生成查询结果。 当然,除了接口投影,还有其他的投影方式,你可以根据自己的需求选择适合的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值