大家都可能会遇到,在用json传输数据时,很多字段是来自不同的表数据,需要我们进行封装数据。

hibernate提供这么一个方法用来直接封装查询属性:

query.setResultTransformer(Transformers.aliasToBean(VO.class));

Example:

session.createQuery("select u.name as name , u.age as age from User as u").setResultTransformer(Transformers.aliasToBean(UserVo.class)).list();

其中红色代码为自定义vo中的属性,可直接用List<UserVo>接收。

这里需要注意:当User中有name字段或age字段为空(null)的情况,此条数据不会封装进List<UserVo>集合中。

相关文章:

  • 2022-12-23
  • 2021-07-13
  • 2021-09-05
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2021-05-23
  • 2021-12-23
猜你喜欢
  • 2021-06-07
  • 2022-01-20
  • 2021-11-15
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
相关资源
相似解决方案