day15-Session发布房源

day15-Session发布房源

YijuUtil工具类

public class YijuUtil {
    private static String AK = "";//七牛云AK
    private static String SK = "";//七牛云SK
    private static String BUCKET = "zeyu";
    public static String getPicName(){
        String picName = new SimpleDateFormat("yyyyMMdd").format(new Date());
        picName = picName + UUID.randomUUID().toString();
        return picName;
    }

    public static void upload(byte[] file,String key){
        Configuration cfg = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(AK,SK);
        String uploadToken = auth.uploadToken(BUCKET);
        try {
            uploadManager.put(file,key,uploadToken);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
    }

}

getPicName()

用来存放图片数据,防止重名,按照时间随机生成

upload((byte[] file,String key))

从前台获取储存图片的byte[]数组,和队友的key,存储到七牛云存储空间

housePost1.jsp

<form action="${pageContext.request.contextPath}/house/toHousePost1.do" method="post" enctype="multipart/form-data">
</form>

HouseController

@RequestMapping("toHousePost1.do")
    public String toHousePost2(HttpSession httpSession,
                               @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
    ) throws IOException {
        House house = new House();
        house.setHouseTitle(houseTitle);
        house.setHouseAddress(houseAddress);
        house.setHousePrice(housePrice);
        house.setPriceUnit(priceUnit);
        house.setUserId(10001);
        
        //存放图片
        Map<String,byte[]> images = new HashMap<>();
        
        //获得Byte数组,获取工具类生成名字,把名字set进去,在存进images数组
        if (houseHeadimg.getSize()!= 0){
            byte[] houseHeadimgBytes = houseHeadimg.getBytes();
            String picName = YijuUtil.getPicName();
            house.setHouseHeadimg(picName);
            images.put(picName,houseHeadimgBytes);
        }

        if (housePlanimg1.getSize()!= 0){
            byte[] housePlanimg1Bytes = housePlanimg1.getBytes();
            String planimg1 = YijuUtil.getPicName();
            house.setHousePlanimg1(planimg1);
            images.put(planimg1,housePlanimg1Bytes);
        }
		//...剩余图片按照上面格式添加到byte数组,放进map里

        httpSession.setAttribute("house",house);
        httpSession.setAttribute("images",images);
        return "housePost2";
    }

把图片images,房屋house用setAttribute放入session中

@ResponseBody

作用:

​ 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

​ 返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

返回housePost2.jsp

housePost2.jsp利用ajax发送HouseInfo数据到session存储

@RequestMapping("responsePost2.do")
    @ResponseBody
    public String responsePost(HttpSession httpSession, HouseInfo houseInfo){
        httpSession.setAttribute("houseInfo", houseInfo);
        return new JSONObject().toString();
    }

存入session后跳转到housePost3

@RequestMapping("toHousePost3.do")
    public String toHousePost3(){
        return "housePost3";
    }

housePost3.jsp利用ajax发送HouseInter数据到session存储

@RequestMapping("responsePost3.do")
@ResponseBody
public String responsePost(HttpSession httpSession, HouseInter houseInter){
    httpSession.setAttribute("houseInter", houseInter);
    return new JSONObject().toString();
}

等到最后一步选择完就可以发布,把之前的session全部取出数据进行存储

@RequestMapping("doHousePost.do")
public String doHousePost(HttpSession httpSession){
    House house = (House) httpSession.getAttribute("house");//获取session中的house,需要强制转型
    house.setCreateTime(new Date().getTime());//获取当前时间
    house.setUpdateTime(new Date().getTime());
    iHouseservice.postHouse(house);

    Map<String,byte[]> images = (Map<String, byte[]>) httpSession.getAttribute("images");//获取session中的image
    Set<String> keys = images.keySet();
    for (String key : keys){
        byte[] file = images.get(key);
        YijuUtil.upload(file,key);
    }
    HouseInfo houseInfo = (HouseInfo) httpSession.getAttribute("houseInfo");//获取session中的houseInfo
    houseInfo.setHouseId(house.getHouseId());//把house中的id拿出来set进去
    iHouseservice.postHouseInfo(houseInfo);

    HouseInter houseInter = (HouseInter) httpSession.getAttribute("houseInter");//获取session中的houseInter
    houseInter.setHouseId(house.getHouseId());//把house中的id拿出来set进去
    iHouseservice.postHouseInter(houseInter);
    return "redirect:/house/searchDetail.do?houseId=" + house.getHouseId();//跳转到房屋详情页
}

Servie

    void postHouse(House house);

    void postHouseInfo(HouseInfo houseInfo);

    void postHouseInter(HouseInter houseInter);

serviceimpl

    /**
     * 发布房源
     * @param house
     */
    @Override
    public void postHouse(House house) {
        iHouseDao.postHouse(house);
    }

    /**
     * 发布房源详细信息
     * @param houseInfo
     */
    @Override
    public void postHouseInfo(HouseInfo houseInfo) {
        iHouseDao.postHouseInfo(houseInfo);
    }

    /**
     * 发布房源内饰信息
     * @param houseInter
     */
    @Override
    public void postHouseInter(HouseInter houseInter) {
        iHouseDao.postHouseInter(houseInter);
    }

Dao

    void postHouse(House house);

    void postHouseInfo(HouseInfo houseInfo);

    void postHouseInter(HouseInter houseInter);

mapper.xml

<!--发布房源-->
    <insert id="postHouse" parameterType="com.yiju.bean.House" useGeneratedKeys="true" keyProperty="houseId">
        insert into tb_house(user_id, house_type, house_title, house_headimg, house_planimg1, house_planimg2, house_img1, house_img2, house_img3, house_img4, house_img5, house_img6, house_price, price_unit, house_address, is_delete, create_time, update_time)
        values (#{userId}, #{houseType}, #{houseTitle}, #{houseHeadimg}, #{housePlanimg1}, #{housePlanimg2}, #{houseImg1}, #{houseImg2}, #{houseImg3}, #{houseImg4}, #{houseImg5}, #{houseImg6}, #{housePrice}, #{priceUnit}, #{houseAddress}, #{isDelete}, #{createTime}, #{updateTime})
    </insert>
    <!--发布房源详细信息-->
    <insert id="postHouseInfo" parameterType="com.yiju.bean.HouseInfo" useGeneratedKeys="true" keyProperty="infoId">
        insert into tb_house_info(house_id, house_nature, house_model, house_year, house_valid, house_layout, house_area, house_turn, house_floor, floor_all, house_decorate, house_lift, create_time, update_time)
        values (#{houseId}, #{houseNature}, #{houseModel}, #{houseYear}, #{houseValid}, #{houseLayout}, #{houseArea}, #{houseTurn}, #{houseFloor}, #{floorAll}, #{houseDecorate}, #{houseLift}, #{createTime}, #{updateTime})
    </insert>
    <!--房源内饰信息填写-->
    <insert id="postHouseInter" parameterType="com.yiju.bean.HouseInter" useGeneratedKeys="true" keyProperty="interId">
        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})
    </insert>

keyProperty=“id” 和useGeneratedKeys="true"作用

在使用mybatis时,常常会出现这种需求:
当主键id是自增的情况下,添加一条记录的同时,其主键id是不能使用的,当我们取出主键id的值发现id为null,但是有时我们需要该主键,这时我们该如何处理呢?
这时我们只需要在其对应xxxmapper.xml中加入以下属性即可:
useGeneratedKeys=”true” keyProperty=”id”

  <insert id="insertSelective" parameterType="com.xxx.dataobject.UserDo" keyProperty="id" useGeneratedKeys="true">
	<!-- 数据库语句-->
 </insert> 

主要是在主键是自增的情况下,添加成功后可以直接使用主键值,其中keyProperty的值是对象的属性值不是数据库表中的字段名

housePost2.jsp

<%--下一步按钮--%>
<input type="button" value="下一步" class="login-box-cen-form-button w180" id="next_submit" />
<%--此处省略n行--%>

<script type="text/javascript">
    $(function () {
        $("#next_submit").on("click",function () {
            //获取几个基本信息
            var houseNature = $("input[name = houseNature]").val();
            var houseModel = $("input[name = houseModel]").val();
            var houseYear = $("input[name = houseYear]").val();
            var houseValid = $("input[name = houseValid]").val();
            var houseLayout = $("input[name = houseLayout]").val();
            var houseArea = $("input[name = houseArea]").val();
            var houseTurn = $("input[name = houseTurn]").val();
            var houseFloor = $("input[name = houseFloor]").val();
            var floorAll = $("input[name = floorAll]").val();
            var houseDecorate = $("input[name = houseDecorate]").val();
            var houseLift;
            if($("input[name=houseLift]").val() == "是") {
                houseLift = 1;
            } else {
                houseLift = 0;
            }
            //校验信息
            if (houseNature == null){
                alert("请输入房源性质!");
                return;//不return会继续执行,return会停止
            } else if (houseModel == null){
                alert("房源类型不能为空,给我滚去写");
                return;
            } 
            //省略基本信息判断
             else if (houseDecorate == null){
                alert("装修类型不能为空,给我滚去写");
                return;
            }else {
                $.ajax({
                    url:"${pageContext.request.contextPath}/house/responsePost2.do",
                    type:"post",
                    async:true,//异步
                    dataType:"json",
                    data:{
                        houseNature : houseNature,
                        houseModel : houseModel,
                        houseYear : houseYear,
                        houseValid : houseValid,
                        houseLayout : houseLayout,
                        houseArea  : houseArea,
                        houseTurn  : houseTurn,
                        houseFloor : houseFloor,
                        floorAll : floorAll,
                        houseDecorate : houseDecorate,
                        houseLift : houseLift
                    },
                    success:function () {
                        window.location.href = "${pageContext.request.contextPath}/house/toHousePost3.do";
                    }
                })
            }

        })
    })
</script>

housePost3.jsp

<script type="text/javascript">
    var bed = 0;
    var washing = 0;
    var air = 0;
    var balcony = 0;
    var ice = 0;
    var toilet = 0;
    var kitchen = 0;
    var tv = 0;
    var heater = 0;
    var wardrobe = 0;
    var heating = 0;
    var internet = 0;
    var sofa = 0;
    //点击反转
    function bedClick() {
        if (bed == 0) bed = 1;
        else bed = 0;
    }

    function washingClick() {
        if (washing == 0) washing = 1;
        else washing = 0;
    }
    //下同
    $(function () {
        $("#next_submit").on("click",function () {
            $.ajax({
                url:"${pageContext.request.contextPath}/house/responsePost3.do",
                type:"post",
                async:true,//异步
                dataType:"json",
                data:{
                     bed : bed,
                     washing : washing,
                     air : air,
                     balcony : balcony,
                     ice : ice,
                     toilet : toilet,
                     kitchen : kitchen,
                     tv : tv,
                     heater : heater,
                     wardrobe : wardrobe,
                     heating : heating,
                     internet : internet,
                     sofa : sofa
                },
                success:function () {
                    window.location.href = "${pageContext.request.contextPath}/house/doHousePost.do";
                }
        })

    })
    })
</script>

detail.jsp

记得把图片前缀的网址替换成自己的即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值