之前都是使用 List<Map> 获取,然后通过工具转换。
后来吧,我总觉得 JPA不应该这么弱智,一定是我弱智才对!
后来终于发现,就这么简单!
planTest的实体:
@Data public class planTest { private Long id; private String name; private Integer plan_count; private Integer status; }
Dao例子:
@Repository public interface AdScheduleExtendDao extends JpaRepository<ad_schedule_extend,String> { //测试使用别名和原名 @Query(value = "select plan_id as id ,plan_name as name " + " ,plan_count,status from ad_plan ",nativeQuery = true) <T> List<T> findTest(Long planID); //全部用别名 @Query(value="select * from ad_schedule_extend " + " where plan_id=:planID " + " and machine_id=:machineID " + " and play_date=:playDate ",nativeQuery = true) <T> List<T> findByPlan_idAndMachine_idAndPlay_date(Long planID, Long machineID, Date playDate); }
impl:
@Override public List<plan_find_schedule_play_details_res> findByPlan_idAndMachine_idAndPlay_date(Long playID, Long machineID, Date playDate) { //planTest 自定义model List<planTest> listTest=dao.findTest(playID); //plan_find_schedule_play_details_res 自定义model List<plan_find_schedule_play_details_res> list=dao.findByPlan_idAndMachine_idAndPlay_date(playID, machineID, playDate); return list; }
结果: