2.配置application.ymlorg.springframework.boot spring-boot-starter-freemarker 2.6.7
spring:
freemarker:
# 后缀名 freemarker 默认后缀为 .ftl ,当然你也可以改成自己习惯的 .html
suffix: .ftl
charset: UTF-8
content-type: text/html
allow-request-override: true
allow-session-override: true
expose-request-attributes: true
expose-session-attributes: true
expose-spring-macro-helpers: true
prefer-file-system-access: false
# 设定模板的加载路径
template-loader-path: file:static/mobile_static/ftl/,classpath:/static/mobile_static/ftl/
settings:
template_update_delay: 1
default_encoding: UTF-8
classic_compatible: true
date_format: yyyy-MM-dd
time_format: HH:mm:ss
datetime_format: yyyy-MM-dd HH:mm:ss
number_format: 0.##
3.定义模板输出路径(application.properties)
out_put_path=C:/Users/Shryu/Study/test/health_project/src/main/resources/static/mobile_static/14.使用Freemarker渲染页面
mobile_setmeal.ftl
预约
传智健康
<#list setmealList as setmeal>
-
${setmeal.name}
${setmeal.remark}
<#if setmeal.sex == '0'>
性别不限
<#else>
<#if setmeal.sex == '1'>
男
<#else>
女
#if>
#if>
${setmeal.age}
#list>
mobile_setmeal_detail.ftl
预约详情
传智健康
${setmeal.name}
${setmeal.remark}
<#if setmeal.sex == '0'>
性别不限
<#else>
<#if setmeal.sex == '1'>
男
<#else>
女
#if>
#if>
${setmeal.age}
套餐详情
项目名称
项目内容
项目解读
<#list setmeal.checkGroups as checkgroup>
-
${checkgroup.name}
<#list checkgroup.checkItems as checkitem>
#list>
${checkgroup.remark}
#list>
SetMealController
package com.shryu.controller;
import com.shryu.constant.MessageConstant;
import com.shryu.constant.RedisConstant;
import com.shryu.entity.PageResult;
import com.shryu.entity.QueryPageBean;
import com.shryu.entity.Result;
import com.shryu.pojo.Setmeal;
import com.shryu.service.ISetMealService;
import com.shryu.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
@RestController
@RequestMapping("/setMeal")
public class SetMealController {
@Autowired
private JedisPool jedisPool;
@Autowired
private ISetMealService setMealService;
@PostMapping("/upload")
public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
System.out.println(imgFile);
String originalFilename = imgFile.getOriginalFilename(); //原始文件名:03a36073-a140-4942-9b9b-712cecb144901.jpg
int index = originalFilename.lastIndexOf(".");
String extention = originalFilename.substring(index); //.jpg
String fileName = UUID.randomUUID().toString() + extention; //FuM1Sa5TtL_ekLsdkYWcf5pyjKGu.jpg
try {
// 将文件上传到七牛云服务器
QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
} catch (IOException e) {
e.printStackTrace();
return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
}
return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
}
// 新增套餐
@PostMapping("/add")
public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds) {
try {
setMealService.add(setmeal,checkgroupIds);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
}
return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
}
// 分页查询
@PostMapping("/findPage")
public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
return setMealService.pageQuery(queryPageBean);
}
// 查询所有套餐
@PostMapping("/getAllSetMeal")
public Result getAllSetMeal(){
try {
List list = setMealService.findAll();
return new Result(true,MessageConstant.GET_SETMEAL_LIST_FAIL,list);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,MessageConstant.GET_SETMEAL_LIST_FAIL);
}
}
// 根据套餐ID查询套餐详细信息(包括套餐基本信息、套餐包含的检查组、检查组包含的检查项)
@PostMapping("/findById")
public Result findById(int id){
try {
Setmeal setmeal = setMealService.findById(id);
return new Result(true,MessageConstant.QUERY_SETMEAL_FAIL,setmeal);
} catch (Exception e) {
e.printStackTrace();
return new Result(false,MessageConstant.QUERY_SETMEAL_SUCCESS);
}
}
}
ISetMealService
package com.shryu.service; import com.baomidou.mybatisplus.extension.service.IService; import com.shryu.entity.PageResult; import com.shryu.entity.QueryPageBean; import com.shryu.pojo.Setmeal; import java.util.List; public interface ISetMealService extends IService{ void add(Setmeal setmeal,Integer[] checkgroupIds); PageResult pageQuery(QueryPageBean queryPageBean); List findAll(); Setmeal findById(int id); }
SetMealServiceImpl
package com.shryu.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.shryu.constant.RedisConstant; import com.shryu.entity.PageResult; import com.shryu.entity.QueryPageBean; import com.shryu.mapper.SetMealMapper; import com.shryu.pojo.Setmeal; import com.shryu.service.ISetMealService; import freemarker.template.Configuration; import freemarker.template.Template; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import redis.clients.jedis.JedisPool; import java.io.File; import java.io.FileWriter; import java.io.Writer; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class SetMealServiceImpl extends ServiceImplimplements ISetMealService { @Autowired private JedisPool jedisPool; @Autowired private SetMealMapper setMealMapper; @Autowired private FreeMarkerConfigurer freeMarkerConfigurer; @Value("${out_put_path}") private String outPutPath; // 从属性文件中读取生成的html对应的目录 // 新增套餐信息 同时需要关联检查组 @Override public void add(Setmeal setmeal, Integer[] checkgroupIds) { setMealMapper.add(setmeal); Integer setmealId = setmeal.getId(); this.setSetMealAndCheckGroup(setmealId,checkgroupIds); // 将图片名称保存到Redis集合中 String fileName = setmeal.getImg(); jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName); // 当添加套餐后需要重新生成静态页面(套餐列表页面、套餐详情页面) generateMobileStaticHtml(); } // 生成当前方法所需的静态页面 public void generateMobileStaticHtml(){ // 在生成静态页面之前需要查询数据 List list = setMealMapper.findAll(); // 需要生成套餐列表静态页面 generateMobileSetMealListHtml(list); // 需要生成套餐详情静态页面 generateMobileSetMealDetailHtml(list); } // 生成套餐列表静态页面 public void generateMobileSetMealListHtml(List list){ Map map = new HashMap(); // 为模板提供数据 用于生成静态页面 map.put("setmealList",list); generateHtml("mobile_setmeal.ftl","m_setmeal.html",map); } // 生成套餐详情静态页面(可能有多个) public void generateMobileSetMealDetailHtml(List list){ for (Setmeal setmeal : list) { Map map = new HashMap(); map.put("setmeal",setMealMapper.findById(setmeal.getId())); generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+ setmeal.getId()+ ".html",map); } } // 通用方法:用于生成静态页面 public void generateHtml(String templateName, String htmlPageName, Map map){ // 获得配置对象 Configuration configuration = freeMarkerConfigurer.getConfiguration(); Writer out = null; try { Template template = configuration.getTemplate(templateName); // 构造输出流 out = new FileWriter(new File(outPutPath + "/" + htmlPageName)); // 输出文件 template.process(map,out); out.close(); } catch (Exception e) { e.printStackTrace(); } } @Override public PageResult pageQuery(QueryPageBean queryPageBean) { Integer currentPage = queryPageBean.getCurrentPage(); Integer pageSize = queryPageBean.getPageSize(); String queryString = queryPageBean.getQueryString(); PageHelper.startPage(currentPage,pageSize); Page page = setMealMapper.findByCondition(queryString); return new PageResult(page.getTotal(),page.getResult()); } @Override public List findAll() { return setMealMapper.findAll(); } // 根据套餐ID查询套餐详细信息(包括套餐基本信息、套餐包含的检查组、检查组包含的检查项) @Override public Setmeal findById(int id) { return setMealMapper.findById(id); } // 设置套餐和检查组多对多关系 操作 t_setmeal_checkgroup public void setSetMealAndCheckGroup(Integer setmealId,Integer[] checkgroupIds){ if (checkgroupIds != null && checkgroupIds.length > 0){ for (Integer checkgroupId : checkgroupIds) { Map map = new HashMap<>(); map.put("setmealId",setmealId); map.put("checkgroupId",checkgroupId); setMealMapper.setSetMealAndCheckGroup(map); } } } }
SetMealMapper
package com.shryu.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.github.pagehelper.Page; import com.shryu.pojo.Setmeal; import org.apache.ibatis.annotations.Mapper; import java.util.List; import java.util.Map; @Mapper public interface SetMealMapper extends BaseMapper{ void add(Setmeal setmeal); void setSetMealAndCheckGroup(Map map); Page findByCondition(String queryString); List findAll(); Setmeal findById(int id); }
SetMealMapper.xml
5.生成页面展示select LAST_INSERT_ID() insert into t_setmeal(code,name,sex,helpCode,remark,attention,age,price,img) values (#{code},#{name},#{sex},#{helpCode},#{remark},#{attention},#{age},#{price},#{img})insert into t_setmeal_checkgroup(setmeal_id, checkgroup_id) values (#{setmealId},#{checkgroupId})



