JPA在做查询的时候,默认是查询全部的字段,当想要查询部分字段的时候,就需要做字段映射,最简单的办法就是使用
Map<String, Object>
充当映射。见如下代码:
@Query(value = "select aid, article_name, author_id, author_username, description, modified_time, read_count, cover, likes_count " +
"from article where author_username = ?1 and is_delete = 0", nativeQuery = true)
List<Map<String, Object>> findAllArticlesByUsername(String username);
另一种方法是使用Dto映射
做项目遇到一个问题:
在使用Jpa映射的时候:
Dto接口
package com.gsjt.******.projection;
import java.util.Date;
public interface ArticleBasicDto {
Integer getAid();
String getArticleName();
Integer getAuthorId();
String getAuthorUsername();
String getDescription();
Date getModifiedTime();
Integer getReadCount();
String getCover();
}
数据库字段
Repository接口中的查询方法
@Query(value = "select aid, article_name, author_id, author_username, description, modified_time, read_count, cover from article where author_id = ?1", nativeQuery = true)
List<ArticleBasicDto> findAllArticlesByUid(Integer uid);
得到的结果
可以看到只要是有驼峰命名的数据库字段映射都找不到为null,没有驼峰命名的字段就没有问题。
按照其他博客的解法:
http://www.javashuo.com/article/p-ejhcjlbk-nq.html
https://www.cnblogs.com/412013cl/p/10868041.html
…
暂时没有解决!!!
留作疑问
暂时修改Dto接口类的命名解决