Spring boot编写VO层的意义

首先要明白,VO层存在的意义,通俗的讲,VO层的存在就是方便前端获取数据,后端将前端的需要的数据做一个整合,打包成一个类。
举一个我第一次使用的小例子,这是一个给前端传列表数据的例子,首先看我的数据库类

public class NewsAllInformation {
int id;
String tatil;
String title;
String content;
String image1;
String image2;
String image3;
String image4;
String time;
//set、get方法跳过
}

这是与数据库对应的全部字段,在我我返回的页面中,只需要

 int id;
String title;
String image1;
String image2;
String image3;
String image4;

这几个,这时候我就可以新建一个vo类,存放着几个属性,在service中,做一个替换,然后通过controller层返给前端。

	List<NewsAllInformation> newsAllInformations = newsAllInformationMapper.select_1(tatil_1);

    List<NewsAllInformationVO> newsAllInformationVOS = Lists.newArrayList();

    for(NewsAllInformation newsAllInformation : newsAllInformations){

	    NewsAllInformationVO newsAllInformationVO = NewsAllInformationVOContent(newsAllInformation);
        newsAllInformationVOS.add(newsAllInformationVO);}
    
    return newsAllInformationVOS;

在这里因为是处理列表,引用了谷歌的Lists方法创建一个列表数据,然后需要在service层的这个类中写一个私有方法来实现这一数据的变换,编写NewsAllInformationVOContent方法

 //返回前端新闻页面数据
private NewsAllInformationVO NewsAllInformationVOContent(NewsAllInformation newsAllInformation){
    NewsAllInformationVO newsAllInformationVO = new NewsAllInformationVO();
    newsAllInformationVO.setImage1(newsAllInformation.getImage1());
    newsAllInformationVO.setImage2(newsAllInformation.getImage2());
    newsAllInformationVO.setImage3(newsAllInformation.getImage3());
    newsAllInformationVO.setImage4(newsAllInformation.getImage4());
    newsAllInformationVO.setTitle(newsAllInformation.getTitle());
    newsAllInformationVO.setId(newsAllInformation.getId());
    return newsAllInformationVO;
}

以上就是我第一次使用vo层的一个心得,还有很多不足之处!

  • 26
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值