基于javaweb+mysql的springboot房屋租赁管理系统(java+springboot+vue+maven+mysql)
运行环境
Java≥8、MySQL≥5.7、Node.js≥10
开发工具
后端:eclipse/idea/myeclipse/sts等均可配置运行
前端:WebStorm/VSCode/HBuilderX等均可
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SpringBoot房屋租赁管理系统(java+springboot+vue+maven+mysql)
用户类型:管理员、房主、用户
一、项目运行 环境配置:
Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。
项目技术:
Spring + SpringBoot+ mybatis + Maven + Vue 等等组成,B/S模式 + Maven管理等等。
return true;
}
}
@RestController
public class MessageController {
@Autowired
MessageService messageService;
@Autowired
UserService userService;
@GetMapping("/getUserState")
@ApiOperation("获取状态")
public Result<?> getUserState() {
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
User user = userService.getUserBasicByUId(uId);
if (user != null){
return Result.success(user.getuState());
}
return Result.error("-1", "非法操作");
}
@GetMapping("/getMyChatList")
@ApiOperation("获取当前用户的历史聊天列表")
public Result<?> getMyChatList() {
@PostMapping("/updateMateRule")
@ApiOperation("修改规则征集房源")
public Result<?> updateMateRule(@RequestBody MateRuleVo mateRuleVo) {
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
if (mateRuleVo.getMrId() != null) {
// 将前端选择的户型多选 合并为一个字符串使用#进行间隔
mateRuleVo.transitionToStringHouseType();
// 修改匹配规则
int updateResult = mateRuleService.updateMateRule(mateRuleVo, uId);
if (updateResult == 1) {
return Result.success("");
} else {
return Result.error("-1", "更新失败");
}
}
return Result.error("-1", "必须输入规则");
}
@GetMapping("/deleteMateRule")
@ApiOperation("终止规则征集房源")
public Result<?> deleteMateRule(@RequestParam("mrId") Long mrId,
HttpServletRequest request) {
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
if (mrId != null) {
// 删除匹配规则
int deleteResult = mateRuleService.deleteMateRule(mrId, uId);
if (deleteResult == 1) {
return Result.success("");
} else {
return Result.error("-1", "删除失败--当前规则不属于该用户");
}
}
return Result.error("-1", "删除失败--没有传入id");
}
@GetMapping("/getCurrentRentHouse")
@ApiOperation("获取当前当前用户租的房源")
public Result<?> getCurrentRentHouse(HttpServletRequest request){
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
// 获取当前用户正在租的房源
List<RentHouseContract> rentHouseContractList = rentHouseService.getCurrentRentHouse(uId);
@GetMapping("/getComplaintUser")
@ApiOperation("获取未处理的投诉用户信息")
@ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "显示的起始页"),
@ApiImplicitParam(name = "pageSize", value = "每页的条数"),})
public Result<?> getComplaintUser(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize){
Page<ComplaintUser> result =
adminService.getUntreatedComplaintUser(new Page<ComplaintUser>(pageNum, pageSize));
return Result.success(result);
}
@GetMapping("/getComplaintHouse")
@ApiOperation("获取未处理的投诉房源信息")
@ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "显示的起始页"),
@ApiImplicitParam(name = "pageSize", value = "每页的条数"),})
public Result<?> getComplaintHouse(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize){
Page<ComplaintHouse> result =
adminService.getUntreatedComplaintHouse(new Page<ComplaintHouse>(pageNum, pageSize));
return Result.success(result);
}
@PostMapping("/notPassComplaint")
@ApiOperation("投诉信息--审核不通过(投诉房源、用户)")
@ApiImplicitParams({@ApiImplicitParam(name = "comeFromUId", value = "该投诉的发起者用户id"),
@ApiImplicitParam(name = "type", value = "类型(house、user)"),
@ApiImplicitParam(name = "id", value = "投诉记录的id"),
@ApiImplicitParam(name = "noPassReason", value = "不通过的理由"),})
public Result<?> getComplaintUser(@RequestParam("type") String type,
@RequestParam("id") Long id,
@RequestParam("comeFromUId") Long comeFromUId,
@RequestParam(name = "noPassReason") String noPassReason){
int update = adminService.updateComplaintState(type, id);
if (update == 1){
return Result.success();
}
return Result.error("-1", "操作失败");
}
@PostMapping("/passComplaint")
@ApiOperation("投诉信息--审核通过(投诉房源、用户)")
@ApiImplicitParams({@ApiImplicitParam(name = "comeFromUId", value = "该投诉的发起者用户id"),
@ApiImplicitParam(name = "type", value = "类型(house、user)"),
@ApiImplicitParam(name = "id", value = "投诉记录的id"),
@ApiImplicitParam(name = "punishType", value = "处罚的类型"),})
public Result<?> passComplaint(@RequestParam("type") String type,
@RequestParam("id") Long id,
return Result.error("-1", "录入失败");
}
@PostMapping("/terminateContract")
@ApiOperation("终止合同")
@ApiImplicitParam(name = "rhcId", value = "合同的id")
public Result<?> terminateContract(@RequestParam("rhcId") Long rhcId, HttpServletRequest request) {
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
int insert = houseService.terminateContract(rhcId, uId);
if (insert == 1){
return Result.success();
}
return Result.error("-1", "终止失败");
}
@GetMapping("/getHistoryBill")
@ApiOperation("获取我的历史账单(商家)")
@ApiImplicitParams({
@ApiImplicitParam(name = "search", value = "搜索的关键字"),
@ApiImplicitParam(name = "type", value = "根据哪种类型搜索(价格,房间号,地址)"),
@ApiImplicitParam(name = "pageNum", value = "当前页数"),
@ApiImplicitParam(name = "pageSize", value = "每页的条数"),})
public Result<?> getHistoryBill(@RequestParam(value = "search", defaultValue = "") String search,
@RequestParam(value = "type", defaultValue = "") String type,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
HttpServletRequest request){
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
String price = "";
String address = "";
String roomNum = "";
if (type.equals("price")) {
String regEx = "[^0-9]";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher;
// 将价格的非数字字符去除
matcher = pattern.matcher(search);
price = matcher.replaceAll("").trim();
}
else if (type.equals("address")){
address = search;
}
else if (type.equals("roomNum")){
roomNum = search;
}
/**
* 登录和权限相关的接口
* */
@RestController
@Api(tags = {"登录和注册"})
public class LoginRegisterController {
@Autowired
UserService userService;
/*登录
* 自己设置默认rememberMe登录状态true
* 自定义有效期7天 --cookie在ShiroLoginFilter中有设置了
* --Session在ShiroLoginFilter中有设置了
* */
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public Result<?> login(@RequestParam("username") String username,
@RequestParam("password") String password,
@RequestParam(defaultValue = "true") Boolean rememberMe) {
Subject subject = SecurityUtils.getSubject();
// 在认证提交前准备 token(令牌)
System.out.println(rememberMe + "======================");
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
// 执行认证登陆
try {
subject.login(token);
} catch (UnknownAccountException uae) {
return Result.error("-1", "未知账户");
} catch (IncorrectCredentialsException ice) {
return Result.error("-1", "密码不正确");
} catch (LockedAccountException lae) {
return Result.error("-1", "账户已锁定");
} catch (ExcessiveAttemptsException eae) {
return Result.error("-1", "用户名或密码错误次数过多");
} catch (AuthenticationException ae) {
return Result.error("-1", "用户名或密码不正确");
}
if (subject.isAuthenticated()) {
// 将用户名和角色返回给前端
User user = userService.getAccountByName(username);
UserNameAndRoleVo result = new UserNameAndRoleVo();
result.setRole(user.getuRole());
result.setUsername(user.getuNickname());
return Result.success(result);
} else {
token.clear();
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
List<Chat> chatList = messageService.getChatHistory(uId, targetUId);
return Result.success(chatList);
}
@GetMapping("/getUserBasicImforByUId")
@ApiOperation("根据用户id获取用户基本信息")
@ApiImplicitParam(name = "uId", value = "用户id")
public Result<?> getUserBasicImforByUId(Long uId){
User user = userService.getUserBasicByUId(uId);
if (user != null){
UserVo userVo = new UserVo();
BeanUtils.copyProperties(user, userVo);
return Result.success(userVo);
}
return Result.error("-1", "没有查到该用户");
}
}
@RestController
@RequestMapping("/screening")
//说明接口文件
@Api(tags = "筛选房源", value = "用于系统主页index页面的筛选房源")
public class ScreeningController {
@Autowired
HouseService houseService;
/*
* 获取主页的初始化房源简要信息
* */
* @throws Exception
*/
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("application/json");
httpServletResponse.getWriter().write(JSONObject.toJSON(Result.error("401","登录认证失效,请重新登录!")).toString());
return false;
}
@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
WebUtils.getAndClearSavedRequest(request);
// 登录成功之后 设置session时间为7天 单位为毫秒
SecurityUtils.getSubject().getSession().setTimeout(7 * 24 * 60 * 60 * 1000L);
return true;
}
}
@RestController
public class MessageController {
@Autowired
MessageService messageService;
@Autowired
UserService userService;
@GetMapping("/getUserState")
@ApiOperation("获取状态")
public Result<?> getUserState() {
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("application/json");
httpServletResponse.getWriter().write(JSONObject.toJSON(Result.error("401","登录认证失效,请重新登录!")).toString());
return false;
}
@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
WebUtils.getAndClearSavedRequest(request);
// 登录成功之后 设置session时间为7天 单位为毫秒
SecurityUtils.getSubject().getSession().setTimeout(7 * 24 * 60 * 60 * 1000L);
return true;
}
}
@RestController
public class MessageController {
@Autowired
MessageService messageService;
@Autowired
UserService userService;
@GetMapping("/getUserState")
@ApiOperation("获取状态")
public Result<?> getUserState() {
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
User user = userService.getUserBasicByUId(uId);
if (user != null){
return Result.success(user.getuState());
}
if (!runState) {
mateRuleAction.initData();
try {
mateRuleAction.start();
} catch (InterruptedException e) {
e.printStackTrace();
return Result.error("-1", "启动失败,程序出错");
}
return Result.success();
}
return Result.error("-1", "当前已正在运行,无序手动start");
}
}
/**
* 登录和权限相关的接口
* */
@RestController
@Api(tags = {"登录和注册"})
}
@GetMapping("/manualStartMatchHouse")
@ApiOperation("人工启动匹配房源程序接口")
public Result<?> manualStartMatchHouse() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println("执行一次"+ sdf.format(new Date()));
// 判断当前是否在运行,如果不是就启动
boolean runState = MateRuleAction.runState;
if (!runState) {
mateRuleAction.initData();
try {
mateRuleAction.start();
} catch (InterruptedException e) {
e.printStackTrace();
return Result.error("-1", "启动失败,程序出错");
}
return Result.success();
}
return Result.error("-1", "当前已正在运行,无序手动start");
}
}
@GetMapping("/cancelAppoint")
@ApiOperation("取消预约")
@ApiImplicitParam(name = "apId", value = "预约记录的id")
public Result<?> cancelAppoint(@RequestParam("apId") Long apId,
HttpServletRequest request) {
// 从token中获取出用户id
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
// 取消预约记录
int result = houseService.cancelAppoint(apId, uId, "user");
// 删除预设未签订的合约
int result2 = houseService.deleteContract(apId);
if (result < 1) {
return Result.error("-1", "取消失败");
}
if (result2 < 1) {
System.out.println("预设的合同删除失败(取消预约联动删除预设合同)");
}
return Result.success("取消成功");
}
@GetMapping("/getMateRuleAndHouseImfor")
@ApiOperation("获取征集房源规则和当前规则匹配到的房源")
public Result<?> getMateRuleAndHouseImfor(HttpServletRequest request) {
// 从token中获取出用户id
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
// 获取当前用户最新的匹配规则 和 当前规则匹配成功的房源
MateRuleAndRecordVo marVo = mateRuleService.getRuleAndHouseImfor(uId);
if (marVo == null) {
return Result.success("noMateRule");
}
return Result.success(marVo);
}
@PostMapping("/addMateRule")
@ApiOperation("新建规则征集房源")
public Result<?> addMateRule(@RequestBody MateRuleVo mateRuleVo) {
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
if (mateRuleVo.getProvinceValue() != null) {
// 将前端选择的户型多选 合并为一个字符串使用#进行间隔
mateRuleVo.transitionToStringHouseType();
// 新增匹配规则
int addResult = mateRuleService.addMateRule(mateRuleVo, uId);
// 启动匹配
int startResult = mateRuleService.startMateRule(mateRuleVo.getMrId(), uId);
if (addResult != 1) {
return Result.error("-1", "创建失败");
HashMap<String, Object> result = new HashMap<>();
result.put("token", token);
result.put("user", userVo);
return Result.success(result);
}
return Result.error("-1", "用户名或密码错误");
}*/
/**
* 跳转登录页面
* */
/*@RequestMapping("/toLogin")
public String toLogin() {
System.out.println("toLogin接口");
return "login";
}*/
}
state = complaintService.complaintHouse(complaintVo, uId);
else
// 投诉用户
state = complaintService.complaintUser(complaintVo, uId);
if (state)
return Result.success();
return Result.error("-1", "未知错误");
}
}
/*
* 日志的AOP
* */
@Aspect
@Component
public class LogAspect {
private Logger logger = LogManager.getLogger(LogAspect.class);
@Pointcut("execution(* com.like.*.controller.*.*(..))")
public void LogPointcut() {}
@Around("LogPointcut()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
javax.servlet.http.HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
logger.info("请求连接 : " + request.getRequestURL().toString());
logger.info("请求方法 : " + request.getMethod());
logger.info("传入参数 :{} ", proceedingJoinPoint.getArgs());
// 获取方法的返回结果
}
else if (type.equals("roomNum")){
roomNum = search;
}
Page<HistoryBill> resultPage = payAndBillService.getHistoryBillByUId("user", new Page<HistoryBill>(pageNum, pageSize), uId, price, address, roomNum);
return Result.success(resultPage);
}
@GetMapping("/getUnpaidBill")
@ApiOperation("获取当前账号未支付的账单")
public Result<?> getUnpaidBill(HttpServletRequest request){
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
// 根据用户id获取未支付的账单和对应的合同信息
List<HistoryBill> historyBillList = payAndBillService.getUnPaidBillByUId(uId);
return Result.success(historyBillList);
}
@PostMapping("/payMoney")
@ApiOperation("支付租金")
@ApiImplicitParam(name = "hbId", value = "账单id")
public Result<?> getCurrentRentHouse(@RequestParam("hbId") Long hbId,
HttpServletRequest request){
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
// 根据账单id更新账单支付状态
int result = payAndBillService.updateBillState(hbId);
if (result == 1) {
return Result.success();
}
return Result.error("-1", "支付失败");
}
}
/**
* 这个方法决定了是否能让用户登录
*/
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
Subject subject = getSubject(request, response);
//如果 isAuthenticated 为 false 证明不是登录过的,同时 isRememberd 为true 证明是没登陆直接通过记住我功能进来的
if(!subject.isAuthenticated() && subject.isRemembered()){
//获取session看看是不是空的
Session session = subject.getSession(true);
//随便拿session的一个属性来看session当前是否是空的,我用userId,你们的项目可以自行发挥
/*if(session.getAttribute("userId") == null){
//如果是空的才初始化,否则每次都要初始化,项目得慢死
//这边根据前面的前提假设,拿到的是username
String username = subject.getPrincipal().toString();
//在这个方法里面做初始化用户上下文的事情,比如通过查询数据库来设置session值,你们自己发挥
globalUserService.initUserContext(username, subject);
}*/
}
//这个方法本来只返回 subject.isAuthenticated() 现在我们加上 subject.isRemembered() 让它同时也兼容remember这种情况
return subject.isAuthenticated() || subject.isRemembered();
}
/**
* 在访问controller前判断是否登录,返回json,不进行重定向。
* @param request
* @param response
* @return true-继续往下执行,false-该filter过滤器已经处理,不继续执行其他过滤器
* @throws Exception
*/
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
httpServletResponse.setCharacterEncoding("UTF-8");
httpServletResponse.setContentType("application/json");
httpServletResponse.getWriter().write(JSONObject.toJSON(Result.error("401","登录认证失效,请重新登录!")).toString());
return false;
}
@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
WebUtils.getAndClearSavedRequest(request);
// 登录成功之后 设置session时间为7天 单位为毫秒
SecurityUtils.getSubject().getSession().setTimeout(7 * 24 * 60 * 60 * 1000L);
return true;
return Result.success(result);
}
return Result.error("-1", result);
}
/* ============== end ======================= */
@PostMapping("/deleteHouse")
@ApiOperation("删除房源")
@ApiImplicitParam(name = "hId", value = "房源id")
public Result<?> deleteHouse(@RequestParam("hId") Long hId,
HttpServletRequest request){
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
String result = houseService.deleteHouse(hId, uId);
if (result.equals("删除成功")){
return Result.success();
}
return Result.error("-1", result);
}
@GetMapping("/getMyBasicHouseImfor")
//方法参数说明,name参数名;value参数说明,备注;dataType参数类型;required 是否必传;defaultValue 默认值
@ApiOperation("获取当前用户发布的房源(简要信息)")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNum", value = "显示的起始页"),
@ApiImplicitParam(name = "pageSize", value = "每页的条数"),
@ApiImplicitParam(name = "titleKeyword", value = "标题关键字"),})
public Result<?> getBasicHouseImfor(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(name = "titleKeyword", defaultValue = "") String titleKeyword,
HttpServletRequest request){
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
Page<House> resultPage =
houseService.getMyBasicHouseImfor(new Page<House>(pageNum, pageSize), titleKeyword, uId);
return Result.success(resultPage);
}
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
HttpServletRequest request){
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
String price = "";
String address = "";
String roomNum = "";
if (type.equals("price")) {
String regEx = "[^0-9]";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher;
// 将价格的非数字字符去除
matcher = pattern.matcher(search);
price = matcher.replaceAll("").trim();
}
else if (type.equals("address")){
address = search;
}
else if (type.equals("roomNum")){
roomNum = search;
}
Page<HistoryBill> resultPage = payAndBillService.getHistoryBillByUId("user", new Page<HistoryBill>(pageNum, pageSize), uId, price, address, roomNum);
return Result.success(resultPage);
}
@GetMapping("/getUnpaidBill")
@ApiOperation("获取当前账号未支付的账单")
public Result<?> getUnpaidBill(HttpServletRequest request){
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
// 根据用户id获取未支付的账单和对应的合同信息
List<HistoryBill> historyBillList = payAndBillService.getUnPaidBillByUId(uId);
return Result.success(historyBillList);
}
@PostMapping("/payMoney")
@ApiOperation("支付租金")
@ApiImplicitParam(name = "hbId", value = "账单id")
public Result<?> getCurrentRentHouse(@RequestParam("hbId") Long hbId,
HttpServletRequest request){
Long uId = (Long) SecurityUtils.getSubject().getPrincipal();
// 根据账单id更新账单支付状态
int result = payAndBillService.updateBillState(hbId);
if (result == 1) {
return Result.success();