java业务逻辑_JavaWeb 期末项目设计 业务逻辑与实现

1.1 业务逻辑设计与实现

业务逻辑层主要由控制层实现,需要实现的需求:

登陆注册:RegisterController:

package com.dpqq.springboot.web.controller;

import com.dpqq.springboot.web.Entity.User;

import com.dpqq.springboot.web.repository.UserRepository;

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.RequestParam;

import java.util.Map;

@Controller

public class RegisterController {

@Autowired

UserRepository userRepository;

@PostMapping(value = "/register")

public String register(@RequestParam("name") String name, String gender,

@RequestParam("phone") String phone,

@RequestParam("password") String password,

Map map){

// 判断数据合法性

if(name.length()>4 || name.isEmpty()){map.put("warning1","姓名小于4个字");}

if(password.length()>16 || password.length()<8){map.put("warning2","密码8位到16位");}

if(phone.length()!=11){map.put("warning3","手机号11位");}

if(userRepository.existsByPhone(phone)){map.put("warning3","手机号已存在");}

if(!isNumeric(phone)){map.put("warning3","手机号数字");}

if(gender==null){map.put("warning4","性别不为空");}

// 合法则设置用户信息并保存至数据库

if(map.isEmpty()){

User user = new User();

user.setPassword(password);

user.setPhone(phone);

user.setName(name);

user.setGender(gender);

userRepository.save(user);

return "login";

}

// 不合法返回注册页

else{return "register";}

}

//判断字符串是否是数字函数

public static boolean isNumeric(String str)

{

for (int i =0;i

{

if(!Character.isDigit(str.charAt(i)))

{

return false;

}

}

return true;

}

}

package com.dpqq.springboot.web.controller;

import com.dpqq.springboot.web.Entity.User;

import com.dpqq.springboot.web.repository.CardRepository;

import com.dpqq.springboot.web.repository.UserRepository;

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.RequestParam;

import javax.servlet.http.HttpSession;

import java.util.Map;

@Controller

public class LoginController {

@Autowired

UserRepository userRepository;

@Autowired

CardRepository cardRepository;

@PostMapping(value = "/user/login")

public String login(@RequestParam("phone") String phone,

@RequestParam("password") String password,

Map map, HttpSession session){

// 判断数据合法性

if(password.length()>16 || password.length()<8){map.put("warning2","密码8位到16位");}

if(phone.length()!=11){map.put("warning1","手机号11位");}

// 数据合法进一步判断,不合法返回登录页

if(map.isEmpty()) {

// 判断是否存在手机号密码匹配,存在即登陆可以登陆重定向至首页,不存在返回登录页

if (userRepository.existsByPhoneAndPassword(phone, password)) {

User user = userRepository.findByPhone(phone);

// 登陆后就再session中存放用户信息

session.setAttribute("loginUser",user);

return "redirect:/";

} else {

map.put("msg", "用户名或密码错误!");

return "login";

}

}

else{return "login";}

}

}

办卡:CardAddController:

package com.dpqq.springboot.web.controller;

import com.dpqq.springboot.web.Entity.Card;

import com.dpqq.springboot.web.Entity.User;

import com.dpqq.springboot.web.repository.CardRepository;

import com.dpqq.springboot.web.repository.UserRepository;

import org.springframework.beans.factory.annotation.Autowir

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值