手把手教你SSM搭建Easymall电商项目 (六)
一.订单功能
1.1代码实现
1.1代码实现 通过传入的ID 去调用获取信息封装在list中
1.1代码实现
1.1代码实现
1.1效果实现
二.添加订单功能
1.1代码实现
调用了 购物车里面的功能 通过ID 获取购物车里面的信息 添加到订单中 ![](https://i-blog.csdnimg.cn/blog_migrate/e72b3cb5c16056e7e08a68f29026fcd1.png)
1.1代码实现
1.1代码实现
1.1代码实现
1.1代码实现
三.删除订单
1.1代码实现
1.1代码实现
1.1代码实现
1.1代码实现
1.1代码实现
四.超时删除订单
1.1代码实现
设置一个配置 和依赖 来实现 pom.xml
<!-- 石英钟 依赖 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 定义任务bean -->
<bean name="paymentOrderJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 指定具体的job类 超时订单删除都是从这个类里实现的 手动编写-->
<property name="jobClass" value="com.jt.easymall.job.OrderOTJob" />
<!-- 指定job的名称 -->
<property name="name" value="paymentOrder" />
<!-- 指定job的分组 -->
<property name="group" value="Order" />
<!-- 必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中删除该任务 -->
<property name="durability" value="true"/>
<!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的对象mapper,
通过获取spring上下文对象,context.getBean(mapper)调用删除逻辑-->
<property name="applicationContextJobDataKey" value="applicationContext"/>
</bean>
<!-- 定义触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="paymentOrderJobDetail" />
<!-- 每一分钟执行一次 -->
<property name="cronExpression" value="0,20,40 0/1 * * * ?" />
</bean>
<!-- 定义调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
1.1代码实现
package com.jt.easymall.job;
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.jt.easymall.mapper.OrderMapper;
/**
* 从quartz的环境中获取spring框架的上下文对象context
* 调用mapper中的方法,执行超时订单删除逻辑
*/
public class OrderOTJob extends QuartzJobBean{
/*
* 定时任务实行方法
* @see org.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org.quartz.JobExecutionContext)
*/
@Override
protected void executeInternal(JobExecutionContext jobContext) throws JobExecutionException {
//获取管理当前job的jobDetail对象
JobDetail jobDetail = jobContext.getJobDetail();
//在配置文件中 jobDetail的一个属性配置了map值,key applicationContext
//value就是spring上下文对象context
ApplicationContext context = (ApplicationContext) jobDetail.getJobDataMap().get("applicationContext");
//获取orderMapper的bean(代理)
System.out.println("定时任务开始执行");
//超时删除的参数Date
Date oTime=new Date(new Date().getTime()-60*60*24);
context.getBean(OrderMapper.class).orderOTDelete(oTime);
//进行参数传递,传递一个order_time<当前时间-1天的时间值
System.out.println("定时任务执行结束");
}
}
1.1代码实现
package com.jt.easymall.service;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jt.easymall.mapper.OrderMapper;
import com.jt.easymall.pojo.Order;
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
public List<Order> queryMyOrder(String userId) {
//不适用left join等关联查询多张表格的语句,保持
//单表的查询操作,保持性能;
//第一种方法保持单表操作
//第二种方法,在持久层完成所有数据封装,还能保持单表操作
List<Order> orderList=orderMapper.queryOrder(userId);
return orderList;
}
public void addOrder(Order order) {
//缺少主键id值,orderTime,支付状态0未支付
order.setOrderId(UUID.randomUUID().toString());
order.setOrderTime(new Date());
order.setOrderPaystate(0);
orderMapper.saveOrder(order);
}
public void deleteOrder(String orderId) {
orderMapper.deleteOrder(orderId);
}
}
1.1代码实现
这个SSM项目 懒得更新了 太简单 直接上传项目了
1.1代码实现 设置每20秒检查一下