项目环境搭建可参考博客,项目的结构如下,controller控制层,dao数据访问层,po实体层,service服务层。static存放页面样式文件,templates存放前端页面。
用户表如下
首先进入管理员的登录界面,在浏览器中输入http://localhost:8082/admin,控制层在收到请求时,会跳转到admin/login界面。login.html中有一个form表单。
<form class="ui large form" method="post" action="#" th:action="@{/admin/login}" >
<input type="text" name="username" placeholder="用户名">
<input type="password" name="password" placeholder="密码">
<button class="ui fluid large teal submit button">登 录 </button>
</form>
当登录的请求触发时,会将username和password等信息传入控制层。控制层再调用服务层userService,对用户名和密码进行检验,若用户存在则返回一个user对象,并将其添加到session中,若用户不存在,则提示一个错误信息。当用户注销时,只需要移除user对象即可。
package com.zr.controller;
import com.zr.po.User;
import com.zr.service.UserService;
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.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("admin")
public class LoginController {
@Autowired
private UserService userService;
@GetMapping
public String toLogin(){
return "admin/login";
}
@PostMapping("login")
public String login(String username, String password, HttpSession session, RedirectAttributes redirectAttributes){
User user = userService.checkUser(username,password);
if(user!=null){
session.setAttribute("user",user);
return "admin/index";
}else{
redirectAttributes.addFlashAttribute("message","用户名密码错误");
return "redirect:/admin";
}
}
@PostMapping("logout")
public String logout(HttpSession session){
session.removeAttribute("user");
return "admin/login";
}
}
Service层包含接口UserService,其实体对象UserSeriveImpl,是通过调用dao层的findByUsernameAndPassword获取到一个实体对象的,这里为了安全性调用了MD5进行加密用户的密码(数据库中存的也是加密的密码)。
package com.zr.service.impl;
import com.zr.dao.UserDao;
import com.zr.po.User;
import com.zr.service.UserService;
import com.zr.service.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User checkUser(String username, String password) {
return userDao.findByUsernameAndPassword(username, MD5Util.code(password));
// return userDao.findByUsernameAndPassword(username,password);
// }
}
}
而dao层的UserDao通过继承JpaRepository接口,来实现findByUsernameAndPassword,JpaRepository接口可以实现根据查询的方法来自动解析成SQL,来进行一些简单的SQL查询,登录功能已实现的差不多了。
package com.zr.dao;
import com.zr.po.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserDao extends JpaRepository <User,Long>{
User findByUsernameAndPassword(String username, String password);
}
接着是查看数据库新闻分类,当控制接收到跳转types页面时,调用pageable接口,该接口是跟分页有关的接口,通过该接口可以得到和分页有关的信息。调用Service层listType方法可以得到type类型的page。并通过model传给前端。
package com.zr.controller;
import com.zr.po.Type;
import com.zr.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/admin/types")
public class TypeController {
@Autowired
private TypeService typeService;
@RequestMapping
public String list(@PageableDefault(size = 5,sort={"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model){
Page<Type> page = typeService.listType(pageable);
model.addAttribute("page",page);
return "admin/types";
}
service继续将pageable对象传递交给dao层出处理,typeDao调用findAll方法并将pageable作为参数传入。而TypeDao同样继承了JpaRepository接口,该接口实现了findAll方法。
package com.zr.dao;
import com.zr.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TypeDao extends JpaRepository<Type,Long> {
}
下面是一些前端页面的内容,前端利用了Thymeleaf的语法规则,同样是循环迭代生成一张表格
<tr th:each="type,iterStat:${page.content}">
<td th:text="${iterStat.count}">1</td>
<td th:text="${type.name}">刻意练习清单</td>
<td>
<a href="#" th:href="@{/admin/types/input/{id}(id=${type.id})}" class="ui mini teal basic button">编辑</a>
<a href="#" th:href="@{/admin/types/delete/{id}(id=${type.id})}" class=" ui mini red basic button">删除</a>
</td>
</tr>
代码已上传GitHub