- 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();
}
- 查询接收类全部由
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();
}
- 需要拷贝接口接收的字段时, 推荐使用
org.apache.commons.beanutils.BeanUtils
, 因为它处理了字段为空的情况, 而org.springframework.beans.BeanUtils
没有处理
网友评论