028 SSM综合练习04--数据后台管理系统--订单相关操作
1.订单表及其关联表的关系分析
2.数据库表对应实体类
(1)Orders.java
package lucky.domain; import lucky.utils.DateUtils; import java.util.Date; import java.util.List; /** * 订单表实体类 * 对应数据库中orders表 * 注意:实体类中的成员变量名必须与数据库中的字段名一致,否则会给后续开发造成不必要的麻烦 */ public class Orders { private String id; private String orderNum; private Date orderTime; private String orderTimeStr; private int orderStatus; private String orderStatusStr; private int peopleCount; private Product product; private List<Traveller> travellers; private Member member; private Integer payType; private String payTypeStr; private String orderDesc; public String getOrderStatusStr() { //订单状态(0未支付,1已支付) if(orderStatus==0){ orderStatusStr="未支付"; }else if(orderStatus==1){ orderStatusStr="已支付"; } return orderStatusStr; } public void setOrderStatusStr(String orderStatusStr) { this.orderStatusStr = orderStatusStr; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getOrderNum() { return orderNum; } public void setOrderNum(String orderNum) { this.orderNum = orderNum; } public Date getOrderTime() { return orderTime; } public void setOrderTime(Date orderTime) { this.orderTime = orderTime; } public String getOrderTimeStr() { if(orderTime!=null){ orderTimeStr=DateUtils.dateToString(orderTime,"yyyy-MM-dd HH:mm"); } return orderTimeStr; } public void setOrderTimeStr(String orderTimeStr) { this.orderTimeStr = orderTimeStr; } public int getOrderStatus() { return orderStatus; } public void setOrderStatus(int orderStatus) { this.orderStatus = orderStatus; } public int getPeopleCount() { return peopleCount; } public void setPeopleCount(int peopleCount) { this.peopleCount = peopleCount; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public List<Traveller> getTravellers() { return travellers; } public void setTravellers(List<Traveller> travellers) { this.travellers = travellers; } public Member getMember() { return member; } public void setMember(Member member) { this.member = member; } public Integer getPayType() { return payType; } public void setPayType(Integer payType) { this.payType = payType; } public String getPayTypeStr() { //支付方式(0 支付宝 1 微信 2其它) if(payType==0){ payTypeStr="支付宝"; }else if(payType==1){ payTypeStr="微信"; }else if(payType==2){ payTypeStr="其他"; } return payTypeStr; } public void setPayTypeStr(String payTypeStr) { this.payTypeStr = payTypeStr; } public String getOrderDesc() { return orderDesc; } public void setOrderDesc(String orderDesc) { this.orderDesc = orderDesc; } @Override public String toString() { return "Orders{" + "id='" + id + '\'' + ", orderNum='" + orderNum + '\'' + ", orderTime=" + orderTime + ", orderTimeStr='" + orderTimeStr + '\'' + ", orderStatus=" + orderStatus + ", peopleCount=" + peopleCount + ", product=" + product + ", travellers=" + travellers + ", member=" + member + ", payType=" + payType + ", payTypeStr='" + payTypeStr + '\'' + ", orderDesc='" + orderDesc + '\'' + '}'; } }
(2)Member.java
package lucky.domain; /** * 会员表实体类 * 对应数据库中member表 * 注意:实体类中的成员变量名必须与数据库中的字段名一致,否则会给后续开发造成不必要的麻烦 */ public class Member { private String id; private String name; private String nickname; private String phoneNum; private String email; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "Member{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", nickname='" + nickname + '\'' + ", phoneNum='" + phoneNum + '\'' + ", email='" + email + '\'' + '}'; } }
(3)Traveller.java
package lucky.domain; /** * 旅客表实体类 * 对应数据库中traveller * 注意:实体类中的成员变量名必须与数据库中的字段名一致,否则会给后续开发造成不必要的麻烦 */ public class Traveller { private String id; private String name; private String sex; private String phoneNum; private Integer credentialsType; private String credentialsTypeStr; private String credentialsNum; private Integer travellerType; private String travellerTypeStr; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPhoneNum() { return phoneNum; } public void setPhoneNum(String phoneNum) { this.phoneNum = phoneNum; } public Integer getCredentialsType() { return credentialsType; } public void setCredentialsType(Integer credentialsType) { this.credentialsType = credentialsType; } public String getCredentialsTypeStr() { return credentialsTypeStr; } public void setCredentialsTypeStr(String credentialsTypeStr) { this.credentialsTypeStr = credentialsTypeStr; } public String getCredentialsNum() { return credentialsNum; } public void setCredentialsNum(String credentialsNum) { this.credentialsNum = credentialsNum; } public Integer getTravellerType() { return travellerType; } public void setTravellerType(Integer travellerType) { this.travellerType = travellerType; } public String getTravellerTypeStr() { return travellerTypeStr; } public void setTravellerTypeStr(String travellerTypeStr) { this.travellerTypeStr = travellerTypeStr; } @Override public String toString() { return "Traveller{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", phoneNum='" + phoneNum + '\'' + ", credentialsType=" + credentialsType + ", credentialsTypeStr='" + credentialsTypeStr + '\'' + ", credentialsNum='" + credentialsNum + '\'' + ", travellerType=" + travellerType + ", travellerTypeStr='" + travellerTypeStr + '\'' + '}'; } }
3.订单控制器类 OrdersController.java
package lucky.controller; import lucky.domain.Orders; import lucky.service.IOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.List; /** * 订单控制器 * @Controller 此注解是Spring的ioc,用于创建对象,用于把当前类对象存入spring容器中 * @RequestMapping url请求的一级目录 * @Autowired 用于注入IProductService的对象 */ @Controller @RequestMapping(path = "/orders") public class OrdersController { @Autowired private IOrderService iOrderService; @RequestMapping(path = "/queryAll") public ModelAndView queryAll() throws Exception{ ModelAndView mv=new ModelAndView(); List<Orders> orders = iOrderService.queryAll(); mv.addObject("ordersList",orders); mv.setViewName("orders-list"); return mv; } }
4.service层的接口及其实现类
(1)IOrderService.java
package lucky.service; import lucky.domain.Orders; import java.util.List; public interface IOrderService { public List<Orders> queryAll() throws Exception; }
(2)OrderServiceImpl.java
package lucky.service.impl; import lucky.dao.IOrdersDao; import lucky.domain.Orders; import lucky.service.IOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @Transactional public class OrderServiceImpl implements IOrderService { @Autowired private IOrdersDao iOrdersDao; @Override public List<Orders> queryAll() throws Exception { return iOrdersDao.queryAll(); } }
5.dao层
package lucky.dao; import lucky.domain.Orders; import lucky.domain.Product; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import java.util.List; public interface IOrdersDao { /** * 查询所有订单表 * @return 查询结果 * @throws Exception * @Select 查询的sql语句 * @Results 映射查询结果集到实体类属性 * column为数据库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键。 * @One 当我们需要通过查询到的一个字段值作为参数,去执行另外一个方法来查询关联的内容,lucky.dao.queryById为要执行的方法 */ @Select("select * from LUCKY.ORDERS") @Results({ @Result(id=true,property = "id",column = "id"), @Result(property = "orderNum",column = "orderNum"), @Result(property = "orderTime",column = "orderTime"), @Result(property = "orderStatus",column = "orderStatus"), @Result(property = "peopleCount",column = "peopleCount"), @Result(property = "payType",column = "payType"), @Result(property = "orderDesc",column = "orderDesc"), @Result(property = "product",column = "productId",javaType = Product.class,one = @One(select = "lucky.dao.IProductDao.queryById")), }) public List<Orders> queryAll() throws Exception; }
6.查询结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)