Java项目:ssm房屋租赁系统

作者主页:夜未央5788

 简介:Java领域优质创作者、Java项目、学习资料、技术互助

文末获取源码

项目介绍

房屋租赁系统,基于 Spring5.x 的实战项目,此项目非Maven项目。

前台系统主要功能包括房源列表展示、房源详细信息展示、根据房源特征进行搜索,包括:房型、小区名;以及房源的预订功能。
后台管理:
用户信息管理
我的租房信息
修改我的密码
房源信息管理
发布房源信息
我发布的信息
 

多用户:普通用户与管理员各自都能发布房源信息

技术栈:

前端 Layu+JSPi,后端 Spring SpringMVC MyBatis

环境要求

IDEA/Eclipse 
Mysql 5.7 
Tomcat 9.x 
JDK 1.8

lombok

运行截图

 

 

 

 

 

 

 

相关代码 

添加房源信息控制类

package com.house.controller.house;

import com.house.entity.House;
import com.house.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * 添加房源信息控制类
 *
 * @author chriy
 */
@Controller
@RequestMapping("/house")
public class AddHouseController {

    /**
     * 映射本地路径
     * 注意:别漏了最后一个 /
     */
    //private String dirPath = "D:/upload/";
    // private String dirPath = "/Users/wangxinqiao/Desktop/apache-tomcat-8.5.60/";
    private String dirPath = "/Users/hanmeng/Downloads/apache-tomcat-8.5.61/";
    /**
     * 简介图片地址
     * 注意:虚拟路径映射的关键位置
     */
    private String simplePath = "/hrs/";
    /**
     * 详细图片地址
     */
    private StringBuilder detailsPath = new StringBuilder();

    @Autowired
    private IHouseService service;

    /**
     * 添加房源界面
     *
     * @return view
     */
    @GetMapping("/addHouse.html")
    public String addHouse() {
        return "addHouse.jsp";
    }

    /**
     * 简介图片上传
     *
     * @param briefFile file
     * @return res
     */
    @RequestMapping("/briefImage")
    @ResponseBody
    public Map<String, Object> briefImage(@RequestParam("brief") MultipartFile briefFile) {
        Map<String, Object> map = new HashMap<>(16);
        try {
            String suffixName = Objects.requireNonNull(briefFile.getOriginalFilename())
                .substring(briefFile.getOriginalFilename().lastIndexOf("."));
            String filename = UUID.randomUUID().toString().replace("-", "") + suffixName;
            File filePath = new File(dirPath);
            if (!filePath.exists()) {
                boolean mkdirs = filePath.mkdirs();
            }
            //创建虚拟路径存储
            simplePath = "/hrs/" + filename;
            map.put("image", simplePath);
            briefFile.transferTo(new File(dirPath + filename));
            map.put("code", 0);
            map.put("msg", "上传成功");
        } catch (Exception e) {
            map.put("code", 1);
            map.put("msg", "上传失败");
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 详情图片上传
     *
     * @param file file
     * @param req  req
     * @return res
     */
    @RequestMapping("/detailsImage")
    @ResponseBody
    public Map<String, Object> detailsImage(@RequestParam("detailsImage") List<MultipartFile> file, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>(16);
        if (!file.isEmpty()) {
            for (MultipartFile f : file) {
                try {
                    // 文件名
                    String filename = UUID.randomUUID()
                        + Objects.requireNonNull(f.getOriginalFilename())
                        .substring(f.getOriginalFilename().lastIndexOf("."));

                    // 存储虚拟路径
                    //String localPath = simplePath + "details/" + filename;
                    String localPath = "/hrs/" + filename;
                    detailsPath.append(localPath + ":-:");

                    File filePath = new File(dirPath);
                    if (!filePath.exists()) {
                        boolean mkdirs = filePath.mkdirs();
                    }
                    //上传
                    f.transferTo(new File(dirPath + filename));

                } catch (Exception e) {
                    map.put("code", 1);
                    map.put("msg", "上传失败");
                    e.printStackTrace();
                }
            }
            map.put("code", 0);
            map.put("msg", "上传成功");
        }
        return map;
    }

    /**
     * 添加新房源信息
     *
     * @param house 房源数据
     * @return res
     */
    @PostMapping("/addHouseRecord")
    @ResponseBody
    public String addHouse(House house) {
        if (house.getPublisher() == null || "".equals(house.getPublisher())) {
            house.setPublisher("管理员");
        }
        house.setHouseImage(simplePath);
        house.setHouseDetailsImg(detailsPath.toString());
        int n = service.addNewHouse(house);
        if (n > 0) {
            // 置空上一次的添加记录
            simplePath = "hrs/";
            detailsPath.delete(0, detailsPath.length());
            return "OK";
        }
        return "FAIL";
    }
}

HouseController

package com.house.controller.house;

import com.house.dto.UserHouseData;
import com.house.entity.House;
import com.house.entity.Page;
import com.house.entity.User;
import com.house.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author chriy
 */
@Controller
@RequestMapping("/house")
public class HouseController {

    @Autowired
    private IHouseService service;

    @RequestMapping("/findUserHouse")
    @ResponseBody
    public UserHouseData houseByUser(HttpServletRequest request, int page, int limit) {
        Page p = new Page();
        User u = (User) request.getSession().getAttribute("loginUser");
        String publisher = u.getUserNickName();
        p.setPublisher(publisher);
        p.setLimit(limit);
        p.setPage((page - 1) * limit);
        List<House> list = service.findHouseByUser(p);
        System.out.println(list);
        return new UserHouseData(0, "200", list.size(), list);
    }

    /**
     * 删除用户发布的房源信息
     *
     * @param houseId 房源 ID
     * @return res
     */
    @PostMapping("/deleteUserHouse")
    @ResponseBody
    public String deleteUserHouse(String houseId) {
        int n = service.deleteUserHouse(Integer.parseInt(houseId));
        if (n > 0) {
            return "OK";
        }
        return "FAIL";
    }

    /**
     * 更新房源信息
     *
     * @param house 房源数据
     * @return res
     */
    @PostMapping("/updateHouse")
    @ResponseBody
    public String updateHouse(House house) {
        int n = service.updateHouse(house);
        if (n > 0) {
            return "OK";
        }
        return "FAIL";
    }
}

HouseDetailController

package com.house.controller.house;

import com.house.entity.House;
import com.house.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 房屋详情控制类
 *
 * @author chriy
 */
@Controller
public class HouseDetailController {

    @Autowired
    private IHouseService service;

    /**
     * 房源详情
     *
     * @param id      id
     * @param request req
     * @return view
     */
    @GetMapping("/detail.html")
    public String detail(int id, HttpServletRequest request) {
        House details = service.findHouseDetailsById(id);
        List<String> list = new ArrayList<String>();
        String[] split = details.getHouseDetailsImg().split(":-:");
        for (int i = 0; i < split.length; i++) {
            list.add(split[i]);
        }
        request.getSession().setAttribute("Details", details);
        request.getSession().setAttribute("DetailsImg", list);
        return "/user/houseDetails.jsp";
    }
}

收藏控制类

package com.house.controller.house;

import com.house.dto.UserOrderData;
import com.house.entity.Order;
import com.house.entity.Page;
import com.house.entity.User;
import com.house.entity.UserOrder;
import com.house.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 收藏控制类
 *
 * @author chriy
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService service;

    /**
     * 用户的收藏房源界面
     *
     * @return view
     */
    @GetMapping("/myOrder.html")
    public String toOrderPage() {
        return "/user/myOrder.jsp";
    }

    /**
     * 添加订单
     *
     * @param id      房源id
     * @param request req
     * @return res
     */
    @PostMapping("/addOrder")
    @ResponseBody
    public String addOrder(String id, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("loginUser");
        try {
            Order order = new Order();
            order.setHouseId(Integer.parseInt(id));
            order.setOrderUser(user.getUserNickName());
            order.setUserId(user.getUserId());
            int n = service.addOrder(order);
            if (n > 0) {
                return "OK";
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return "FAIL";
    }

    /**
     * 查询我的所有收藏房源信息
     *
     * @param page    page
     * @param limit   limit
     * @param request req
     * @return res
     */
    @PostMapping("/myOrderInfo")
    @ResponseBody
    public UserOrderData findAllOrder(int page, int limit, HttpServletRequest request) {
        Page pageObj = new Page();
        pageObj.setPage((page - 1) * limit);
        pageObj.setLimit(limit);
        User user = (User) request.getSession().getAttribute("loginUser");
        pageObj.setUserId(user.getUserId());
        UserOrderData uod = new UserOrderData();
        List<UserOrder> order = service.findAllOrder(pageObj);
        uod.setCode(0);
        uod.setCount(service.getOrderCount(user.getUserId()));
        uod.setData(order);
        uod.setMsg("200");
        return uod;
    }

    /**
     * 删除收藏的房源信息
     *
     * @param orderId 单号
     * @return res
     */
    @PostMapping("/deleteOrder")
    @ResponseBody
    public String deleteOrder(int orderId) {
        int n = service.deleteOrder(orderId);
        if (n > 0) {
            return "OK";
        }
        return "FAIL";
    }
}

 普通用户登录

package com.house.controller.user;

import com.house.entity.User;
import com.house.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 普通用户登录
 *
 * @author chriy
 */
@Controller
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private IUserService mapper;

    /**
     * 登录
     *
     * @param userName     用户名
     * @param userPassword 密码
     * @param req          req
     * @return res
     */
    @PostMapping("/login")
    @ResponseBody
    public String toCustomerPage(String userName, String userPassword, HttpServletRequest req) {
        User user = new User();
        user.setUserName(userName);
        user.setUserPassword(userPassword);
        User loginUser = mapper.login(user);
        if (loginUser != null) {
            req.getSession().setAttribute("loginUser", loginUser);
            return "OK";
        }
        return "FAIL";
    }

    /**
     * 退出登录
     *
     * @param session session
     * @return view
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index.html";
    }

    @PostMapping("/register")
    @ResponseBody
    public String register(User user) {
        int register;
        try {
            register = mapper.register(user);
            if (register > 0) {
                return "OK";
            }
        } catch (Exception e) {
            return "FAIL";
        }
        return "FAIL";
    }

}

用户控制类

package com.house.controller.user;

import com.house.entity.House;
import com.house.entity.User;
import com.house.service.IHouseService;
import com.house.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户控制类
 *
 * @author chriy
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService service;
    @Autowired
    private IHouseService dao;

    /**
     * 用户管理页
     *
     * @return view
     */
    @GetMapping("/home.html")
    public String toUserSystemPage() {
        return "/user/home.jsp";
    }

    /**
     * 用户修改密码界面
     *
     * @return view
     */
    @GetMapping("/updatePassword.html")
    public String updatePassword() {
        return "/user/updatePassword.jsp";
    }

    /**
     * 后台第一个欢迎界面
     *
     * @return view
     */
    @GetMapping("/welcome.html")
    public String toWelcomePage() {
        return "welcome.jsp";
    }

    /**
     * 用户发布的租房信息
     * @return view
     */
    @GetMapping("/userRental.html")
    public String toUserRentalPage() {
        return "/user/myRental.jsp";
    }

    /**
     * 用户更新房源信息
     * 使用的也是管理员的界面
     *
     * @param houseId 房源ID
     * @param request req
     * @return view
     */
    @GetMapping("/updateHouse.html")
    public String toUpdatePage(int houseId, HttpServletRequest request) {
        House house = dao.findHouseDetailsById(houseId);
        request.getSession().setAttribute("House", house);
        return "/admin/updateHouse.jsp";
    }

    /**
     * 更新用户密码
     *
     * @param id     id
     * @param newPwd new password
     * @param oldPwd old password
     * @return res
     */
    @PostMapping("/updateUserPwd")
    @ResponseBody
    public String updateUserPwd(String id, String newPwd, String oldPwd) {
        User oldUser = new User();
        oldUser.setUserId(Integer.parseInt(id));
        oldUser.setUserPassword(oldPwd);
        User checkUser = service.checkOldPwd(oldUser);
        if (checkUser != null) {
            User newUser = new User();
            newUser.setUserId(Integer.parseInt(id));
            newUser.setUserPassword(newPwd);
            int n = service.updateUserPwd(newUser);
            if (n > 0) {
                return "OK";
            }
        }
        return "FAIL";
    }
}

如果也想学习本系统,下面领取。关注并回复:018ssm 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜未央5788

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值