登录功能:
@RequestMapping("/")
public String index(){
return "login";
}
@PostMapping("/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
Model model,
HttpSession session){
User user1 = userMapper.selectOne(new QueryWrapper<User>().eq("username",username)
.eq("password",password));
if(ObjectUtils.isEmpty(user1)){
model.addAttribute("msg","账号或密码错误");
return "login";
}else {
session.setAttribute("loginUser",username);
return "redirect:/list";
}
}
前端页面
<tr>
<td valign="middle" align="right">
用户名:
td>
<td valign="middle" align="left">
<input type="text" class="inputgri" name="username"/>
td>
tr>
<tr>
<td valign="middle" align="right">
密码:
td>
<td valign="middle" align="left">
<input type="password" class="inputgri" name="password"/>
td>
tr>
<tr>
<p style="color: red" th:text="${msg}">p>
tr>
注册功能:
@RequestMapping("/zhuce")
public String zhuce(){
return "regist";
}
@PostMapping("/register")
public String register(User user,String code,HttpSession session){
String code1 = (String)session.getAttribute("code");
if(code1.equalsIgnoreCase(code)){
userMapper.insert(user);
return "redirect:/";
}else{
return "redirect:/zhuce";
}
}
查找功能
@GetMapping("/list")
public String list(Model model){
List<Student> studentList = studentMapper.selectList(null);
model.addAttribute("studentlist",studentList);
return "emplist";
}
前端页面
<tr class="row1" th:each="std:${studentlist}">
<td>
<span th:text="${std.getSid()}"></span>
</td>
<td>
<span th:text="${std.getSname()}"></span>
</td>
<td>
<span th:text="${std.getGender()}"></span>
</td>
<td>
<!--日期格式化-->
<span th:text="${#dates.format(std.getBirthday(),'yyyy-MM-dd')}"></span>
</td>
<td>
<span th:text="${std.getAddress()}"></span>
</td>
<td>
<span th:text="${std.getClassesId()}"></span>
</td>
<td>
<a th:href="@{/delete(sid=${std.sid})}">删除信息</a>
<a th:href="@{/update(sid=${std.sid})}">修改信息</a>
</td>
</tr>
添加功能
@GetMapping("/add")
public String add(){
return "addEmp";
}
@PostMapping("/save")
public String save(Student student){
studentMapper.insert(student);
return "redirect:/list";
}
前端页面(部分)
<tr>
<td valign="middle" align="right">
姓名:
td>
<td valign="middle" align="left">
<input type="text" class="inputgri" name="sname"/>
td>
tr>
<tr>
<td valign="middle" align="right">
性别:
td>
<td valign="middle" align="left">
<input type="radio" class="inputgri" name="gender" value="男"/><label>男label>
<input type="radio" class="inputgri" name="gender" value="女"/><label>女label>
td>
tr>
删除功能
@GetMapping("/delete")
public String delete(int sid){
studentMapper.deleteById(sid);
return "redirect:/list";
}
前端页面
<td>
<a th:href="@{/delete(sid=${std.sid})}">删除信息a>
<a th:href="@{/update(sid=${std.sid})}">修改信息a>
td>
修改功能
@GetMapping("/update")
public String update(int sid,Model model){
Student student = studentMapper.selectById(sid);
model.addAttribute("emp",student);
return "updateEmp";
}
@PostMapping("/update2")
public String update2(Student student){
studentMapper.updateById(student);
return "redirect:/list";
}
前端页面(部分)
<tr>
<td valign="middle" align="right">
性别:
td>
<td valign="middle" align="left">
<input type="radio" class="inputgri" name="gender" th:checked="${emp.gender=='男'}" value="男"/><label>男label>
<input type="radio" class="inputgri" name="gender" th:checked="${emp.gender=='女'}" value="女"/><label>女label>
td>
tr>
<tr>
<td valign="middle" align="right">
生日:
td>
<td valign="middle" align="left">
<input type="text" class="inputgri" name="birthday"
th:value="${#dates.format(emp.birthday,'yyyy/MM/dd')}"/>
td>
tr>
安全退出功能
@RequestMapping("/logout")
public String logout(HttpSession session){
session.invalidate();
return "redirect:/";
}
前端
<a class="nav-link" th:href="@{/logout}">安全退出</a>
list页面显示username
<h1>
Welcome![[${session.loginUser}]]
</h1>