spring jpa 多表查询最快的实现,以接口接收查询字段

2022-07-27  本文已影响0人  饱饱想要的灵感
  1. DAO代码中每个字段都要设置别名, 即 as..., 推荐使用hql
/**
 * 关键词DAO层
 */
public interface KeywordDAO extends JpaRepository<KeywordDO, Long>, JpaSpecificationExecutor<KeywordDO> {
    @Query(value = "select kc.id as classId,kc.name as className, kc.priority as classPriority, " +
            "k.nameOne as nameOne, k.nameTwo as nameTwo, k.useType as useType, k.modifyTime as modifyTime " +
            "from KeywordDO k left join KeywordClassDO kc on k.classId = kc.id where k.modifyTime is not null")
    List<KeywordVO> queryKeywordJoinKeywordClass();
}
  1. 查询接收类全部由getXX()方法组成, 另外还可以通过@Value(#{...})使用spel, spring不推荐复杂的spel, 若字段值为空可能出现SpelEvaluationException错误
/**
 * 关键词查询接收类
 */
public interface KeywordVO  {
    Long getClassId();
    String getClassName();
    Integer getClassPriority();

    String getNameOne();
    String getNameTwo();
    String getUseType();

    @Value("#{T(com.popo.boot.utils.DateUtil).formatToString(target.modifyTime, 'yyyy-MM-dd')}")
    String getModifyTime();
}
  1. 需要拷贝接口接收的字段时, 推荐使用org.apache.commons.beanutils.BeanUtils, 因为它处理了字段为空的情况, 而org.springframework.beans.BeanUtils没有处理
上一篇 下一篇

猜你喜欢

热点阅读