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