16.房型管理
16.1 添加房型管理页面16.2 房型管理的增删改查
16.2.1 实体类16.2.2 数据层16.2.3 业务层16.2.4 控制层
16.3 添加房型窗口的图片上传功能
建议配合视频学习食用(8-5~9-2):
https://www.bilibili.com/video/BV1rr4y1F7Y2?p=1
1.roomTypeManager.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
layui
<%-- 搜索条件 --%>
<%-- 表格工具栏 --%>
<%-- 数据表格 --%>
<%-- 行工具栏 --%>
<%-- 添加和修改窗口 --%>
2.PageController.java
@RequestMapping("/toRoomTypeManager")
public String toRoomTypeManager(){
return "roomType/roomTypeManager";
}
3.数据库
或者直接在页面修改,修改后记得给超级管理员添加权限
RoomType.java
package com.manong.entity;
import lombok.Data;
@Data
public class RoomType {
private Integer id;
private String typeName;
private String photo;
private Double price;
private Integer liveNum;
private Integer bedNum;
private Integer roomNum;
private Integer reservedNum;
private Integer avilableNum;
private Integer livedNum;
private Integer status;
private String remark;
}
RoomTypeVo.java
package com.manong.vo;
import com.manong.entity.RoomType;
import lombok.Data;
@Data
public class RoomTypeVo extends RoomType {
private Integer page;//当前页码
private Integer limit;//每页显示数量
}
16.2.2 数据层
RoomTypeMapper.java
package com.manong.dao;
import com.manong.entity.RoomType;
import com.manong.vo.RoomTypeVo;
import java.util.List;
public interface RoomTypeMapper {
List findRoomTypeList(RoomTypeVo roomTypeVo);
int insert(RoomType record);
int updateRoomType(RoomType roomType);
int getRoomCountByRoomTypeId(Integer roomTypeId);
int deleteById(Integer id);
}
RoomTypeMapper.xml
16.2.3 业务层insert into t_room_type (typeName, photo, price, liveNum, bedNum, roomNum, reservedNum, avilableNum, livedNum, status, remark ) values (#{typeName,jdbcType=VARCHAR}, #{photo,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL}, #{liveNum,jdbcType=INTEGER}, #{bedNum,jdbcType=INTEGER}, #{roomNum,jdbcType=INTEGER}, #{reservedNum,jdbcType=INTEGER}, #{avilableNum,jdbcType=INTEGER}, #{livedNum,jdbcType=INTEGER}, #{status,jdbcType=INTEGER}, #{remark,jdbcType=VARCHAR} ) update t_room_type where id = #{id,jdbcType=INTEGER} typeName = #{typeName,jdbcType=VARCHAR}, photo = #{photo,jdbcType=VARCHAR}, price = #{price,jdbcType=DECIMAL}, liveNum = #{liveNum,jdbcType=INTEGER}, bedNum = #{bedNum,jdbcType=INTEGER}, roomNum = #{roomNum,jdbcType=INTEGER}, reservedNum = #{reservedNum,jdbcType=INTEGER}, avilableNum = #{avilableNum,jdbcType=INTEGER}, livedNum = #{livedNum,jdbcType=INTEGER}, status = #{status,jdbcType=INTEGER}, remark = #{remark,jdbcType=VARCHAR}, delete from t_room_type where id = #{id}
RoomTypeService.java
package com.manong.service;
import com.manong.entity.RoomType;
import com.manong.vo.RoomTypeVo;
import java.util.List;
public interface RoomTypeService {
List findRoomTypeList(RoomTypeVo roomTypeVo);
int insert(RoomType record);
int updateRoomType(RoomType roomType);
int getRoomCountByRoomTypeId(Integer roomTypeId);
int deleteById(Integer id);
}
RoomTypeServiceImpl.java
package com.manong.service.impl;
import com.alibaba.fastjson.JSON;
import com.manong.dao.RoomTypeMapper;
import com.manong.entity.RoomType;
import com.manong.service.RoomTypeService;
import com.manong.utils.JedisPoolUtils;
import com.manong.utils.RedisKey;
import com.manong.vo.RoomTypeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class RoomTypeServiceImpl implements RoomTypeService {
@Resource
private RoomTypeMapper roomTypeMapper;
public List findRoomTypeList(RoomTypeVo roomTypeVo) {
return roomTypeMapper.findRoomTypeList(roomTypeVo);
}
public int insert(RoomType roomType) {
//设置已预订数量
roomType.setReservedNum(0);
//设置可入住房间数量
roomType.setAvilableNum(roomType.getRoomNum());
//设置已入住房间数量
roomType.setLivedNum(0);
return roomTypeMapper.insert(roomType);
}
public int updateRoomType(RoomType roomType) {
//修改可用房间数
roomType.setAvilableNum(roomType.getRoomNum());
return roomTypeMapper.updateRoomType(roomType);
}
public int getRoomCountByRoomTypeId(Integer roomTypeId) {
return roomTypeMapper.getRoomCountByRoomTypeId(roomTypeId);
}
public int deleteById(Integer id) {
return roomTypeMapper.deleteById(id);
}
}
16.2.4 控制层
RoomTypeController.java
package com.manong.controller;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manong.entity.RoomType;
import com.manong.service.RoomTypeService;
import com.manong.utils.DataGridViewResult;
import com.manong.utils.SystemConstants;
import com.manong.utils.UUIDUtils;
import com.manong.vo.RoomTypeVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/admin/roomType")
public class RoomTypeController {
@Resource
private RoomTypeService roomTypeService;
@RequestMapping("/list")
public DataGridViewResult list(RoomTypeVo roomTypeVo){
PageHelper.startPage(roomTypeVo.getPage(),roomTypeVo.getLimit());
List roomTypeList = roomTypeService.findRoomTypeList(roomTypeVo);
PageInfo pageInfo = new PageInfo(roomTypeList);
return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
}
@RequestMapping("/addRoomType")
public String addRoomType(RoomType roomType){
Map map = new HashMap();
if(roomTypeService.insert(roomType)>0){
map.put(SystemConstants.SUCCESS,true);
map.put(SystemConstants.MESSAGE,"添加成功");
}else{
map.put(SystemConstants.SUCCESS,false);
map.put(SystemConstants.MESSAGE,"添加失败");
}
return JSON.toJSONString(map);
}
@RequestMapping("/updateRoomType")
public String updateRoomType(RoomType roomType){
Map map = new HashMap();
if(roomTypeService.updateRoomType(roomType)>0){
map.put(SystemConstants.SUCCESS,true);
map.put(SystemConstants.MESSAGE,"修改成功");
}else{
map.put(SystemConstants.SUCCESS,false);
map.put(SystemConstants.MESSAGE,"修改失败");
}
return JSON.toJSONString(map);
}
@RequestMapping("/checkRoomTypeHasRoom")
public String checkRoomTypeHasRoom(Integer roomTypeId){
Map map = new HashMap();
if(roomTypeService.getRoomCountByRoomTypeId(roomTypeId)>0){
map.put(SystemConstants.EXIST,true);
map.put(SystemConstants.MESSAGE,"该房型下存在房间信息,无法删除");
}else{
map.put(SystemConstants.EXIST,false);
}
return JSON.toJSONString(map);
}
@RequestMapping("/deleteById")
public String deleteById(Integer id){
Map map = new HashMap();
if(roomTypeService.deleteById(id)>0){
map.put(SystemConstants.SUCCESS,true);
map.put(SystemConstants.MESSAGE,"删除成功");
}else{
map.put(SystemConstants.SUCCESS,false);
map.put(SystemConstants.MESSAGE,"删除失败");
}
return JSON.toJSONString(map);
}
}
16.3 添加房型窗口的图片上传功能
1.roomTypeManager.jsp
2.RoomTypeController.java,记得修改文件上传路径
@RequestMapping("/uploadFile")
public String uploadFile(MultipartFile attach){
//创建Map集合保存数据(响应到前台页面的数据)
Map map = new HashMap();
//判断是否有选中文件
if(!attach.isEmpty()){
//获取文件上传地址
String path = "E:/ssm_hotel/upload/";
//获取源文件名称
String oldName = attach.getOriginalFilename();
//获取文件后缀名
String extension = FilenameUtils.getExtension(oldName);
//重命名文件名称
String newFileName = UUIDUtils.randomUUID()+"."+extension;
//为了解决同一个文件夹下文件过多的问题,使用日期作为文件夹管理
String datePath = new SimpleDateFormat("yyyyMMdd").format(new Date());
//组装最终的文件名
String finalName = datePath + "/" + newFileName;
//创建文件对象
File destFile = new File(path,finalName);
//判断文件夹是否存在,文件夹不存在则创建该文件夹
if(!destFile.getParentFile().exists()){
destFile.getParentFile().mkdirs();
}
try {
//将文件保存到硬盘
attach.transferTo(destFile);
//保存响应的数据
map.put("code",0);
map.put("msg","");
Map dataMap = new HashMap();
dataMap.put("src","/hotel/show/"+finalName);//文件上传成功的回显地址
map.put("data",dataMap);
map.put("imagePath",finalName);//图片名称,目的是给photo隐藏域赋值
} catch (IOException e) {
e.printStackTrace();
}
}
return JSON.toJSONString(map);
}
3.设置
选择本地图片的位置,就是之前**/ssm_hote/upload



