易居住房5(“发布房源”--房源内饰信息填写)
在“易居住房4”的基础上进行增加或修改代码
“IHouseViewDao.java”增加代码
void postHouseInter(HouseInter houseInter);
“IHouseViewService.java”增加代码
void postHouseInter(HouseInter houseInter);
“HouseViewService.java”增加代码
@Overridepublic void postHouseInter(HouseInter houseInter) {houseViewDao.postHouseInter(houseInter);}
“HouseViewController.java”
在@Param(value = "houseImg6") MultipartFile houseImg6后加HttpSession session
修改代码
将
Map images=new HashMap<>();
改为
Map images=new HashMap<>();
在if语句的picName,houseHeadimg后加.getBytes(),以此类推
将
Set keys=images.keySet();for (String key:keys){MultipartFile file=images.get(key);YijuUtil.upload(file.getBytes(),key);}houseViewService.postHouse(house);
改为
session.setAttribute("house",house);session.setAttribute("images",images);
将
houseInfo.setHouseId(10001);houseViewService.postHouseInfo(houseInfo);
改为
session.setAttribute("houseInfo",houseInfo);
并增加代码
@RequestMapping("responsePost3.do")@ResponseBodypublic String responsePost3(HouseInter houseInter,HttpSession session){/*houseInter.setInterId(10013);houseViewService.postHouseInter(houseInter);*/session.setAttribute("houseInter",houseInter);return new JSONObject().toString();}@RequestMapping("doHousePost.do")public String doHousePost(HttpSession session) throws IOException {House house= (House) session.getAttribute("house");house.setUserId(10001);Map images= (Map) session.getAttribute("images");HouseInfo houseInfo= (HouseInfo) session.getAttribute("houseInfo");HouseInter houseInter= (HouseInter) session.getAttribute("houseInter");houseViewService.postHouse(house);Set keys=images.keySet();for (String key:keys){byte[] bytes=images.get(key);YijuUtil.upload(bytes,key);}houseInfo.setHouseId(house.getHouseId());houseViewService.postHouseInfo(houseInfo);houseInter.setHouseId(house.getHouseId());houseViewService.postHouseInter(houseInter);return "redirect:findHouseById.do?houseId="+house.getHouseId();}
“HouseViewController.java”完整代码如下
package com.yiju.controller;import com.github.pagehelper.PageInfo;
import com.yiju.bean.House;
import com.yiju.bean.HouseInfo;
import com.yiju.bean.HouseInter;
import com.yiju.pojo.HouseView;
import com.yiju.service.IHouseViewService;
import com.yiju.util.YijuUtil;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;@Controller
@RequestMapping("house")
public class HouseViewController {@Autowiredprivate IHouseViewService houseViewService;@RequestMapping("findFourHouse.do")public ModelAndView findFourHouse(){ModelAndView mv=new ModelAndView();List newHouses=houseViewService.findFourHouseByType(0);List oldHouses=houseViewService.findFourHouseByType(1);List rentHouses=houseViewService.findFourHouseByType(2);mv.addObject("newHouses",newHouses);mv.addObject("oldHouses",oldHouses);mv.addObject("rentHouses",rentHouses);mv.setViewName("../main");return mv;}@RequestMapping("findHouseById.do")public ModelAndView findHouseByHouseId(int houseId){HouseView house=houseViewService.findHouseByHouseId(houseId);ModelAndView mv=new ModelAndView();mv.addObject("houseInfo",house);mv.setViewName("details");return mv;}@RequestMapping("findHouse.do")public ModelAndView findHouse(int currentPage,int houseType){ModelAndView mv=new ModelAndView();List houses=houseViewService.findHouseByType(currentPage,houseType);List fh=houseViewService.findFourHouseByType(houseType);PageInfo pageInfo=new PageInfo<>(houses);mv.addObject("pageInfo",pageInfo);mv.addObject("fh",fh);if (houseType==0){mv.setViewName("newhouse");}else if (houseType==1){mv.setViewName("oldhouse");}else {mv.setViewName("renthouse");}return mv;}@RequestMapping("toHousePost2.do")public ModelAndView toHousePost2(@Param(value = "houseTitle") String houseTitle,@Param(value = "houseAddress") String houseAddress,@Param(value = "housePrice") BigDecimal housePrice,@Param(value = "priceUnit") String priceUnit,@Param(value = "houseHeadimg") MultipartFile houseHeadimg,@Param(value = "housePlanimg1") MultipartFile housePlanimg1,@Param(value = "housePlanimg2") MultipartFile housePlanimg2,@Param(value = "houseImg1") MultipartFile houseImg1,@Param(value = "houseImg2") MultipartFile houseImg2,@Param(value = "houseImg3") MultipartFile houseImg3,@Param(value = "houseImg4") MultipartFile houseImg4,@Param(value = "houseImg5") MultipartFile houseImg5,@Param(value = "houseImg6") MultipartFile houseImg6,HttpSession session) throws IOException {House house=new House();house.setHouseTitle(houseTitle);house.setHouseAddress(houseAddress);house.setHousePrice(housePrice);house.setPriceUnit(priceUnit);house.setUserId(10007);Map images=new HashMap<>();if (houseHeadimg.getSize()!=0){String picName= YijuUtil.getPicName();house.setHouseHeadimg(picName);images.put(picName,houseHeadimg.getBytes());}if (housePlanimg1.getSize()!=0){String picName= YijuUtil.getPicName();house.setHouseHeadimg(picName);images.put(picName,housePlanimg1.getBytes());}if (housePlanimg2.getSize()!=0){String picName= YijuUtil.getPicName();house.setHouseHeadimg(picName);images.put(picName,housePlanimg2.getBytes());}if (houseImg1.getSize()!=0){String picName= YijuUtil.getPicName();house.setHouseHeadimg(picName);images.put(picName,houseImg1.getBytes());}if (houseImg2.getSize()!=0){String picName= YijuUtil.getPicName();house.setHouseHeadimg(picName);images.put(picName,houseImg2.getBytes());}if (houseImg3.getSize()!=0){String picName= YijuUtil.getPicName();house.setHouseHeadimg(picName);images.put(picName,houseImg3.getBytes());}if (houseImg4.getSize()!=0){String picName= YijuUtil.getPicName();house.setHouseHeadimg(picName);images.put(picName,houseImg4.getBytes());}if (houseImg5.getSize()!=0){String picName= YijuUtil.getPicName();house.setHouseHeadimg(picName);images.put(picName,houseImg5.getBytes());}if (houseImg6.getSize()!=0){String picName= YijuUtil.getPicName();house.setHouseHeadimg(picName);images.put(picName,houseImg6.getBytes());}/*Set keys=images.keySet();for (String key:keys){byte[] bytes=images.get(key);YijuUtil.upload(bytes,key);}*//*houseViewService.postHouse(house);*/session.setAttribute("house",house);session.setAttribute("images",images);ModelAndView mv=new ModelAndView();mv.setViewName("housePost2");return mv;}@RequestMapping("responsePost2.do")@ResponseBodypublic String responsePost2(HouseInfo houseInfo,HttpSession session){/*houseInfo.setHouseId(10001);houseViewService.postHouseInfo(houseInfo);*/session.setAttribute("houseInfo",houseInfo);return new JSONObject().toString();}@RequestMapping("responsePost3.do")@ResponseBodypublic String responsePost3(HouseInter houseInter,HttpSession session){/*houseInter.setInterId(10013);houseViewService.postHouseInter(houseInter);*/session.setAttribute("houseInter",houseInter);return new JSONObject().toString();}@RequestMapping("doHousePost.do")public String doHousePost(HttpSession session) throws IOException {House house= (House) session.getAttribute("house");house.setUserId(10001);Map images= (Map) session.getAttribute("images");HouseInfo houseInfo= (HouseInfo) session.getAttribute("houseInfo");HouseInter houseInter= (HouseInter) session.getAttribute("houseInter");houseViewService.postHouse(house);Set keys=images.keySet();for (String key:keys){byte[] bytes=images.get(key);YijuUtil.upload(bytes,key);}houseInfo.setHouseId(house.getHouseId());houseViewService.postHouseInfo(houseInfo);houseInter.setHouseId(house.getHouseId());houseViewService.postHouseInter(houseInter);return "redirect:findHouseById.do?houseId="+house.getHouseId();}
}
“HouseMapper.xml”增加代码
insert into tb_house_inter(house_id, bed, washing, air, balcony, ice, toilet, kitchen, tv, heater, wardrobe, heating, internet, sofa, create_time, update_time)values (#{houseId}, #{bed}, #{washing}, #{air}, #{balcony}, #{ice}, #{toilet}, #{kitchen}, #{tv}, #{heater}, #{wardrobe}, #{heating}, #{internet}, #{sofa}, #{createTime}, #{updateTime})
“housePost3.jsp”在







