实训日志7 Springboot项目实现登陆退出,分类查看
SpringBoot项目相比ssm项目而言,配置更加简洁,避免了不必要的繁琐配置。本次SpringBoot项目为新闻管理系统。
登陆退出实现
首先我们需要创建所有的数据库表单,这里我们可以使用orm对象关系映射,具体为hibernate对象关系映射框架来实现数据可持久化,可以自动在数据库创建表单。
application.yml配置如下:
spring:
datasource:
url: jdbc:mysql://localhost:3306/tnews?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true
在po层编写所需要的实体类,运行产生表单,这里先展示本次需要的User和Type类,如下:
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue
private Long id;
private String nickname;
private String username;
private String password;
private String email;
private String avatar;
private Integer type;
@Temporal(TemporalType.TIMESTAMP)
private Date createTime;
@Temporal(TemporalType.TIMESTAMP)
private Date updateTime;
@OneToMany(mappedBy = "user")
private List<News> newsList = new ArrayList<>();
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public List<News> getNewsList() {
return newsList;
}
public void setNewsList(List<News> newsList) {
this.newsList = newsList;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", nickname='" + nickname + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", avatar='" + avatar + '\'' +
", type=" + type +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
}
@Entity
@Table(name = "t_type")
public class Type {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(mappedBy = "type")
private List<News> news = new ArrayList<>();
public Type() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<News> getNews() {
return news;
}
public void setNews(List<News> news) {
this.news = news;
}
@Override
public String toString() {
return "Type{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
下面编写controller层,LoginController类如下:
@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";
}
//注销操作
@GetMapping("logout")
public String logout(HttpSession session){
session.removeAttribute("user");
return "redirect:/admin";
}
}
从上面可以看到我们调用了service层的方法,所以我们创建UserService接口和UserServiceImpl类,编写checkUser()方法,判断登陆。
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User checkUser(String username, String password) {
return userDao.findByUsernameAndPassword(username,password);
}
}
最后dao层UserDao继承JpaRepository<User,Long>,重写方法findByUsernameAndPassword()
public interface UserDao extends JpaRepository<User,Long> {
User findByUsernameAndPassword(String username, String password);
}
这样便实现了登陆操作,注销则将session中的User去除,再redirect:/admin重定向到/admin请求,即登陆界面。
查看分类
同样编写TypeController类对传来的请求进行操作响应,具体如下:
@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";
}
}
这里我们用到了Pageable这个类,并且根据id降序显示,Pageable 是Spring Data库中定义的一个接口,用于构造翻页查询,是所有分页相关信息的一个抽象,通过该接口,我们可以得到和分页相关所有信息(例如pageNumber、pageSize等),这样,Jpa就能够通过pageable参数来得到一个带分页信息的Sql语句。
同时看到本层中调用了service层方法,所以创建TypeService和TypeServiceImpl,并编写实现方法,调用Dao层方法,数据库中根据分页信息找到所有的种类信息并且返回:
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeDao typeDao;
@Override
public Page<Type> listType(Pageable pageable) {
return typeDao.findAll(pageable);
}
}
后端实现后,我们还需要在前端显示,这里我们使用了thymeleaf模板语言,把model中的数据渲染到html中,通过语法解析model中的数据,将数据库中id和种类名传入显示,下面为种类显示界面的前端。同时对上一页,下一页进行了处理,如果是第一页或者最后一页,不显示上一页或下一页。
<tbody>
<tr th:each="type,iterStat:${page.content}">
<td th:text="${iterStat.count}">1</td>
<td th:text="${type.name}">刻意练习清单</td>
<td>
<a href="#" class="ui mini teal basic button">编辑</a>
<a href="#" class="ui mini red basic button">删除</a>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="6">
<div class="ui mini pagination menu" th:if="${page.totalPages}>1">
<a class=" item" th:href="@{/admin/types(page=${page.number}-1)}" th:unless="${page.first}">上一页</a>
<a class=" item" th:href="@{/admin/types(page=${page.number}+1)}" th:unless="${page.last}">下一页</a>
</div>
<a href="#" class="ui mini right floated teal basic button">新增</a>
</th>
</tr>
</tfoot>
实现效果:
登陆界面
主页
查看分类和分页
下一篇: Jquery中时间与时间戳之间的转换