jpa查询只返回需要的单个/多个字段
jpa查询只返回需要的单个/多个字段
方法一
调用dao自带的findAll方法获取到所有记录的列表,然后遍历这个列表,将指定的字段提取到列表中。
方法二
spring data jpa怎么做到只查询部分字段而不返回全部字段?_博客-----CSDN博客_jpa 只取部分字段
public interface UserRepository extends CrudRepository<User, Long> {
@Query(value = "select name from user", nativeQuery = true)
Iterable<UserName> findAllName();
}
可以使用List替换Iterable。
方法三
springboot jpa之返回表中部分字段的处理方法_向阳居士的博客-CSDN博客_jpa 返回部分字段
@Query("select new com.ddzrh.dto.RoomOrderTimeOutPut(r.orderTime,COUNT(r.orderTime) )" +
" from RoomOrderTime as r " +
" where r.roomTypeId =:roomId"+
" GROUP BY r.orderTime" )
List<RoomOrderTimeOutPut> queryRoomOrderTime(@Param("roomId") Integer roomId);
SpringBoot使用JPA如何查询部分字段_静幽水-CSDN博客_jpa查询部分字段
public class YhbModel implements Serializable {
private Integer yhbh;
//用户名称
private String yhmc;
@Repository
public interface YhbDao extends JpaRepository<YHB,Integer>{
//根据部门查找用户
@Transactional
@Query(value = "select new com.nju.software.assessment.model.YhbModel(y.yhbh,y.yhmc) from YHB y where yhbm=?1")
List<YhbModel> findYhmcByYhbm(String yhbm);
}