(3). 新增套餐
- ①. 关于文件上传各个参数的说明
②. 什么是UUID?
UUID 的十六个八位字节被表示为 32个十六进制数字,以连字号分隔的五组来显示,形式为 8-4-4-4-12,总共有 36个字符(即三十二个英数字母和四个连字号)。例如: 123e4567-e89b-12d3-a456-426655440000 UUID.randomUUID().toString()+extention;[extention为后缀名] (注意:这里的toString()是转成字符串类型)
③. 用elementUI 上传文件中,在人间上传成功后的钩子函数中response相当于response.data
①. 新增
1.
新增
前台: 1.点击新建按钮,显示模态框,将套餐数据 | 图片url | 检查组中的数据清空 | 选项卡选中套餐, 发送ajax查询到检查组所有的数据,将查询出来的数据赋值给检查组中表单数据,并显示在页面 2.点击新建按钮[表单进行验证],将套餐表单数据和检查组中复选框的id作为参数传递到后台.返回 结果[如果为true,则给出成功提示;否则给出失败提示],在返回结果中,要将模态框关闭 3.调用findPage方法重新查询页面数据 后台: 1.controller: 将套餐表单数据和检查组复选框ids作为参数传递给service 2.service: 2.1 将套餐数据作为参数传递给dao,进行数据库新增 注意:这里要拿到当前新增的id,要在insert中使用selectKey标签 2.2 将套餐新增的id和复选框ids传入到dao,进行中间表数据的添加[重点掌握] 3.dao Setmeal 和 中间表的新增
上传图片并预览 1.点击 + 号上传图片,action路径会访问controller 2.将图片传入到七里云中[传入的参数是UUID后缀名] UUID:UUID.randomUUID().toString()+extention; extention:[ String originalFilename = imgFile.getOriginalFilename();//原始文件名 String extention=originalFilename.substring (originalFilename.lastIndexOf(".")); ] 3.要将传入七牛云的图片传入到redis中 4.在返回时,要将上传到七牛云图片的名称返回给前台[在新增套餐时,有img这个属性]
- ①. 新增前台代码
步骤
效果图:
1.新建按钮绑定单击事件,对应的处理函数为handleCreate
2.点击新增按钮,触发点击事件
- ②.新增后台步骤
图片预览步骤:
1.前台
2.后台:
注意:别忘了在spring配置文件中配置文件上传组件
新增套餐:
1.controller:
2.service
3.dao:
②. 定时清理垃圾
- ①. 创建maven工程health_jobs,打包方式为war,导入Quartz等相关坐标
<packaging>war</packaging> <dependencies> <dependency> <groupId>com.itheima</groupId> <artifactId>health_interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <!-- 指定端口 --> <port>83</port> <!-- 请求路径 --> <path>/</path> </configuration> </plugin> </plugins> </build>
- ②. 配置web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
- ③. 配置log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c:\\mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=debug, stdout
- ④. 配置applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--Jedis连接池的相关配置--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal"> <value>200</value> </property> <property name="maxIdle"> <value>50</value> </property> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="true"/> </bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="poolConfig" ref="jedisPoolConfig" /> <constructor-arg name="host" value="127.0.0.1" /> <constructor-arg name="port" value="6379" type="int" /> <constructor-arg name="timeout" value="30000" type="int" /> </bean> </beans>
- ⑤.配置applicationContext-jobs.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--开启spring注解使用--> <context:annotation-config></context:annotation-config> <!--注册自定义Job--> <bean id="clearImgJob" class="com.itheima.jobs.ClearImgJob"></bean> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 注入目标对象 --> <property name="targetObject" ref="clearImgJob"/> <!-- 注入目标方法 --> <property name="targetMethod" value="clearImg"/> </bean> <!-- 注册一个触发器,指定任务触发的时间 --> <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 注入JobDetail --> <property name="jobDetail" ref="jobDetail"/> <!-- 指定触发的时间,基于Cron表达式 --> <property name="cronExpression"> <!-- <value>0 0 2 * * ?</value> --> <value>0/10 * * * * ?</value> </property> </bean> <!-- 注册一个统一的调度工厂,通过这个调度工厂调度任务 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 注入多个触发器 --> <property name="triggers"> <list> <ref bean="myTrigger"/> </list> </property> </bean> </beans>
- ⑥. 创建ClearImgJob定时任务类
package com.itheima.jobs; import com.itheima.constant.RedisConstant; import com.itheima.utils.QiniuUtils; import org.springframework.beans.factory.annotation.Autowired; import redis.clients.jedis.JedisPool; import java.util.Set; /* * 自定义Job,实现定时清理垃圾图片 * */ public class ClearImgJob { @Autowired private JedisPool jedisPool; public void clearImg(){ //根据redis中保存的两个set集合进行差值计算,获取垃圾图片名称集合 Set<String> set = jedisPool.getResource().sdiff (RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES); if(set!=null){ for (String picName : set) { //删除七牛云服务器上的图片 QiniuUtils.deleteFileFromQiniu(picName); //从redis集合中删除集合名称 jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName); } } } }