以项目的方式展示DTO VO Query DO的使用
1.先看项目结构
dao接口定义
public interface OrderDao {
Order get(Long id) throws Exception;
int create(Order order) throws Exception;
int update(Order order) throws Exception;
int delete(Long id) throws Exception;
}
public interface UserDao {
/**
* 根据用户查询用户订单信息
* @param userQuery
* @return
*/
List selectUserOrderByUser(UserQuery userQuery);
User get(Long id) throws Exception;
int create(User user) throws Exception;
int update(User user) throws Exception;
int delete(Long id) throws Exception;
}
xml文件
service
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Autowired
private GoodsService goodsService;
@Override
public UserOrderVO selectUserOrderByUser(UserQuery userQuery) {
UserOrderVO userOrderVO = new UserOrderVO();
UserOrderDTO userOrderDTO = userDao.selectUserOrderByUser(userQuery);
if(userOrderDTO == null) return null;
BeanUtils.copyProperties(userOrderDTO,userOrderVO);
//service调用service把结果封装到同一个vo中
List orderGoodsDTOList = userOrderVO.getOrderGoodsDTOList();
for (OrderGoodsDTO orderGoodsDTO : orderGoodsDTOList) {
//订单id查询出商品
List goodsList = goodsService.selectGoodsByOrderId(orderGoodsDTO.getOid());
orderGoodsDTO.setGoodsList(goodsList);
}
//订单条数
userOrderVO.setOrderCount(orderGoodsDTOList.size());
float totalPrice=0;
//计算该用户的商品总价
for (OrderGoodsDTO orderGoodsDTO : userOrderVO.getOrderGoodsDTOList()) {
List goodsList = orderGoodsDTO.getGoodsList();
for (Goods goods : goodsList) {
totalPrice+=goods.getPrice();
}
}
userOrderVO.setTotalPrice(totalPrice);
return userOrderVO;
}
}
Controller
@RestController
@RequestMapping("/userOrder")
public class UserOrderController {
@Autowired
private UserService userService;
@RequestMapping("/selectUserOrderByUser")
public JsonResult selectUserOrderByUser(@Validated UserQuery userQuery, BindingResult bindingResult){
JsonResult jsonResult=new JsonResult();
if(bindingResult.hasErrors()){
String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
jsonResult.setCode(500);
jsonResult.setMsg(defaultMessage);
return jsonResult;
}
UserOrderVO userOrderVO = userService.selectUserOrderByUser(userQuery);
jsonResult.setData(userOrderVO);
if(userOrderVO == null){
jsonResult.setCode(500);
jsonResult.setMsg("未查找数据");
}
return jsonResult;
}
}
JsonResult 工具类
public class JsonResult {
private int code;
private String msg;
private Object data;
private Date timestamp;
public JsonResult() {
this.code = 200;
this.msg = " *** 作成功";
this.data = null;
this.timestamp = new Date();
}
public JsonResult(Object data){
this();
this.data=data;
}
}
1.Query负责接收前端的参数
public class UserQuery {
@NotNull(message = "id不能为空!")
private Integer id;
@NotEmpty(message = "userName不能为空!")
private String userName;
}
2.DO与数据库的字段保持一致和数据库交互
public class Goods {
private int gid;
private int oid;
private String productName;
private float price;
}
public class Order {
private Integer oid;
private Integer uid;
private String code;
private Date creatTime;
}
public class User {
private Integer uid;
private String username;
}
3.DTO dao层和service层和返回的数据都可以用dto,通常数据表查询都是多表查询把多张表的查询结果封装在一个dto中即可
public class UserOrderDTO {
private Integer uid;
private String userName;
private List orderGoodsDTOList;
}
public class OrderGoodsDTO {
private Integer oid;
private String code;
private Date createTime;
private List goodsList;
}
4.VO负责返回前端的数据 在service层返回即可
public class UserOrderVO {
private Integer uid;
private String userName;
private List orderGoodsDTOList;
private int orderCount;
private float totalPrice;
}
漏传参数调用接口打印
http://localhost:8080/userOrder/selectUserOrderByUser?id= 打印
{"code":500,"msg":"id不能为空!","data":null,"timestamp":1652357412150}
正常调用接口打印http://localhost:8080/userOrder/selectUserOrderByUser?id=7788 &userName=不爱穿裤子的小红娘
{
"code": 200,
"msg": " *** 作成功",
"data": {
"uid": 7788,
"userName": "不爱穿裤子的小红娘",
"orderGoodsDTOList": [{
"oid": 1,
"code": "1559722872969097255",
"createTime": 1652338841593,
"goodsList": [{
"gid": 1,
"productName": "冬瓜",
"price": 5.8
}, {
"gid": 2,
"productName": "西瓜",
"price": 4.5
}, {
"gid": 3,
"productName": "哈密瓜",
"price": 3.0
}]
}, {
"oid": 2,
"code": "2606159379158097255",
"createTime": 1652338841593,
"goodsList": [{
"gid": 4,
"productName": "苹果5s",
"price": 500.0
}, {
"gid": 5,
"productName": "小米8Puls",
"price": 888.0
}, {
"gid": 6,
"productName": "华为荣耀30P",
"price": 1888.0
}]
}],
"orderCount": 2,
"totalPrice": 3289.3
},
"timestamp": 1652338841593
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)