SSM订单操作

1795-王同学

发表文章数:58

热门标签

,
首页 » MySQL » 正文

一、表结构分析

1.1 订单表信息描述 orders

序号 字段名称 字段类型 字段描述
1 id varchar2(32) 无意义、主键uuid
2 orderNum varchar2(50) 订单编号 不为空 唯一
3 orderTime timestamp 下单时间
4 peopleCount int 出行人数
5 orderDesc varchar2(500) 订单描述(其它信息)
6 payType int 支付方式(0 支付宝 1 微信 2其它)
7 orderStatus int 订单状态(0 未支付 1 已支付)
8 productId int 产品id 外键
9 memberid int 会员(联系人)id 外键

productId描述了订单与产品之间的关系。
memberid描述了订单与会员之间的关系。

创建表sql

CREATE TABLE orders( 
id varchar2(32) default SYS_GUID() PRIMARY KEY, 
orderNum VARCHAR2(20) NOT NULL UNIQUE, 
orderTime timestamp, 
peopleCount INT, 
orderDesc VARCHAR2(500), 
payType INT, 
orderStatus INT, 
productId varchar2(32), 
memberId varchar2(32), 
FOREIGN KEY (productId) REFERENCES product(id), 
FOREIGN KEY (memberId) REFERENCES member(id) )
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid) values ('0E7231DC797C486290E8713CA3C6ECCC', '12345', to_timestamp('02-03-2018 12:00:00.000000','dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '676C5BD1D35E429A8C2E114939C5685A', 'E61D65F673D54F68B0861025C69773DB'); 
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid) values ('5DC6A48DD4E94592AE904930EA866AFA', '54321', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '676C5BD1D35E429A8C2E114939C5685A', 'E61D65F673D54F68B0861025C69773DB'); 
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid) values ('2FF351C4AC744E2092DCF08CFD314420', '67890', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB'); 
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid) values ('A0657832D93E4B10AE88A2D4B70B1A28', '98765', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB'); 
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid) values ('E4DD4C45EED84870ABA83574A801083E', '11111', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB'); 
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid) values ('96CC8BD43C734CC2ACBFF09501B4DD5D', '22222', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '12B7ABF2A4C544568B0A7C69F36BF8B7', 'E61D65F673D54F68B0861025C69773DB'); 
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid) values ('55F9AF582D5A4DB28FB4EC3199385762', '33333', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '9F71F01CB448476DAFB309AA6DF9497F', 'E61D65F673D54F68B0861025C69773DB'); 
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid) values ('CA005CF1BE3C4EF68F88ABC7DF30E976', '44444', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '9F71F01CB448476DAFB309AA6DF9497F', 'E61D65F673D54F68B0861025C69773DB'); 
insert into ORDERS (id, ordernum, ordertime, peoplecount, orderdesc, paytype, orderstatus, productid, memberid) values ('3081770BC3984EF092D9E99760FDABDE', '55555', to_timestamp('02-03-2018 12:00:00.000000', 'dd-mm-yyyy hh24:mi:ss.ff'), 2, '没什么', 0, 1, '9F71F01CB448476DAFB309AA6DF9497F', 'E61D65F673D54F68B0861025C69773DB');

实体类

public class Orders { 
    private String id; 
    private String orderNum; 
    private Date orderTime; 
    private String orderTimeStr;
    private int orderStatus; 
    private int peopleCount; 
    private Product product; 
    private List<Traveller> travellers; 
    private Member member; 
    private Integer payType; 
    private String payTypeStr; 
    private String orderDesc; //省略getter/setter 
}

1.2 会员表信息描述member

订单与会员之间是多对一关系,我们在订单表中创建一个外键来进行关联。

序号 字段名称 字段类型 字段描述
1 id varchar2(32) 无意义、主键uuid
2 name varchar2(20) 姓名
3 nickName varchar2(20) 昵称
4 phoneNum varchar2(20) 电话号码
5 email varchar2(50) 邮箱

创建表sql

CREATE TABLE member( 
    id varchar2(32) default SYS_GUID() PRIMARY KEY, 
    NAME VARCHAR2(20), nickname VARCHAR2(20), 
    phoneNum VARCHAR2(20), 
    email VARCHAR2(20) 
)
insert into MEMBER (id, name, nickname, phonenum, email) values ('E61D65F673D54F68B0861025C69773DB', '张三', '小三', '18888888888', 'zs@163.com');

实体类

public class Member { 
    private String id; 
    private String name; 
    private String nickname; 
    private String phoneNum; 
    private String email; //省略getter/setter 
}

1.3.旅客表信息描述 traveller

序号 字段名称 字段类型 字段描述
1 id varchar2(32) 无意义、主键uuid
2 name varchar2(20) 姓名
3 sex varchar2(20) 性别
4 phoneNum varchar2(20) 电话号码
5 credentialsType int 证件类型 0身份证 1护照 2军官证
6 credentialsNum varchar2(50) 证件号码
7 travellerType int 旅客类型(人群) 0 成人 1 儿童

创建表sql

CREATE TABLE traveller( 
    id varchar2(32) default SYS_GUID() PRIMARY KEY, 
    NAME VARCHAR2(20), 
    sex VARCHAR2(20), 
    phoneNum VARCHAR2(20), 
    credentialsType INT, 
    credentialsNum VARCHAR2(50), 
    travellerType INT 
)
insert into TRAVELLER (id, name, sex, phonenum, credentialstype, credentialsnum, travellertype) values ('3FE27DF2A4E44A6DBC5D0FE4651D3D3E', '张龙', '男', '13333333333', 0, '123456789009876543', 0); 
insert into TRAVELLER (id, name, sex, phonenum, credentialstype, credentialsnum, travellertype) values ('EE7A71FB6945483FBF91543DBE851960', '张小龙', '男', '15555555555', 0, '987654321123456789', 1);

实体类

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; //省略getter/setter 
}

旅客与订单之间是多对多关系,所以我们需要一张中间(order_traveller)来描述。

序号 字段名称 字段类型 字段描述
1 orderId varchar2(32) 订单id
2 travellerId varchar2(32) 旅客id

创建表sql

CREATE TABLE order_traveller( 
    orderId varchar2(32), 
    travellerId varchar2(32), 
    PRIMARY KEY (orderId,travellerId), 
    FOREIGN KEY (orderId) REFERENCES orders(id), 
    FOREIGN KEY (travellerId) REFERENCES traveller(id) 
)
insert into ORDER_TRAVELLER (orderid, travellerid) values ('0E7231DC797C486290E8713CA3C6ECCC', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E'); 
insert into ORDER_TRAVELLER (orderid, travellerid) values ('2FF351C4AC744E2092DCF08CFD314420', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E'); 
insert into ORDER_TRAVELLER (orderid, travellerid) values ('3081770BC3984EF092D9E99760FDABDE', 'EE7A71FB6945483FBF91543DBE851960'); 
insert into ORDER_TRAVELLER (orderid, travellerid) values ('55F9AF582D5A4DB28FB4EC3199385762', 'EE7A71FB6945483FBF91543DBE851960'); 
insert into ORDER_TRAVELLER (orderid, travellerid) values ('5DC6A48DD4E94592AE904930EA866AFA', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E'); 
insert into ORDER_TRAVELLER (orderid, travellerid) values ('96CC8BD43C734CC2ACBFF09501B4DD5D', 'EE7A71FB6945483FBF91543DBE851960'); 
insert into ORDER_TRAVELLER (orderid, travellerid) values ('A0657832D93E4B10AE88A2D4B70B1A28', '3FE27DF2A4E44A6DBC5D0FE4651D3D3E'); 
insert into ORDER_TRAVELLER (orderid, travellerid) values ('CA005CF1BE3C4EF68F88ABC7DF30E976', 'EE7A71FB6945483FBF91543DBE851960'); 
insert into ORDER_TRAVELLER (orderid, travellerid) values ('E4DD4C45EED84870ABA83574A801083E', 'EE7A71FB6945483FBF91543DBE851960');

二、订单查询

2.1 订单查询页面 order-list.jsp

SSM订单操作

2.2 Controller

@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrderService orderService;

    //查询全部订单(未分页)
    /*@RequestMapping("/findAll.do")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        List ordersList = orderService.findAll();
        mv.addObject("ordersList", ordersList);
        mv.setViewName("orders-list");
        return mv;
    }*/

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1")int page,@RequestParam(name = "size", required = true, defaultValue = "4") int size) throws Exception{
        ModelAndView mv = new ModelAndView();
        List<Orders> ordersList =orderService.findAll(page,size);
        //PageInfo 就是一个分页的bean
        PageInfo pageInfo = new PageInfo(ordersList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("order-page-list");
        return mv;
    }

    @RequestMapping("/findById.do")
    public ModelAndView findById(@RequestParam( name = "id", required = true)String ordersId) throws Exception {
        ModelAndView mv = new ModelAndView();
        Orders orders = orderService.findById(ordersId);
        mv.addObject("orders", orders);
        mv.setViewName("orders-show");
        return mv;
    }
}

2.3 Dao

public interface IOrdersDao {

    @Select("select * from 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 = "com.itheima.ssm.dao.IProductDao.findById"))
    })
    public List<Orders> findAll() throws Exception;

    //多表操作
    @Select("select * from orders where id = #{ordersId}")
    @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 = "com.itheima.ssm.dao.IProductDao.findById")),
            @Result(property = "member", column = "memberId", javaType = Member.class,one = @One(select = "com.itheima.ssm.dao.IMemberDao.findById")),
            @Result(property = "travellers", column = "id", javaType = java.util.List.class,many = @Many(select = "com.itheima.ssm.dao.ITravellerDao.findByOrdersId"))
    })
    public Orders findById(String ordersId) throws  Exception;
}

IProductDao的findById

public interface IProductDao {

    //根据id查询产品
    @Select("select * from product where id = #{id}")
    public Product findById(String id) throws Exception;

    //查询所有的产品信息
    @Select("select * from product")
    public List<Product> findAll() throws Exception;

    @Insert("insert into product(productNum,productName,cityName,departureTime,productPrice,productDesc,productStatus) values(#{productNum},#{productName},#{cityName},#{departureTime},#{productPrice},#{productDesc},#{productStatus})")
    public void save(Product product);
}
标签:

拜师教育学员文章:作者:1795-王同学, 转载或复制请以 超链接形式 并注明出处 拜师资源博客
原文地址:《SSM订单操作》 发布于2021-11-25

分享到:
赞(0) 打赏

评论 抢沙发

评论前必须登录!

  注册



长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu3.3主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录