学习内容
几天的学习内容是基于spring boot实现登录、分类、和注销
首先在po层编写几个实体类(直接导入老师发的),每个实体类都对应一张数据库表,分别有Comment类、News类、Tag类、Type类、User类
application.yml文件:
server:
port: 8082
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
登录
LoginController
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"; //默认后缀会加上.html
}
@PostMapping("login")
public String logig(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","用户名或者密码错误1");
return "redirect:/admin";
}
}
}
service层编写一个UserService接口和一个实现类。实现类UserServiceImpl类:
@Service
public class UserServiceImpl implements UserService {
//调用dao层
@Autowired
private UserDao userDao;
@Override
public User checkUser(String username, String password) {
return userDao.findByUsernameAndPassword(username,password);
}
}
dao层的UserDao如下,注意findByUserAndPassword不要写错,不然运行的时候会报错。
public interface UserDao extends JpaRepository<User,Long> {
//会自动生成SQL
User findByUsernameAndPassword(String username, String password);
}
登陆页面如下:
分类
首先在数据库表t_type中插入以下数据:
期望结果是将这八个数据在分类页面以倒叙的是形式显示出来,并且每一页只显示五条数据。TypeController文件:
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.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";
}
}
TypeServiceImpl如下:
@Service
public class TypeServiceImpl implements TypeService {
@Autowired
private TypeDao typeDao;
@Override
public Page<Type> listType(Pageable pageable) {
return typeDao.findAll(pageable);
}
}
创建一个TypeDao接口,该接口继承JpaRepository,接口内部不用写任何东西。
希望在页面上点击“上一页”或者“下一页”的时候,也能显示相应的数据,此时修改type.html文件:
<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>
<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>
分类页面如下:
注销
期望的是:点击用户名称下方的注销之后,可以退出当前页面,回到登录页面。这个在之前的ssm框架项目中也写过。
LoginController类中:
@GetMapping("logout")
public String logout(HttpSession session){
session.removeAttribute("user");
return "admin/login";
}
然后修改_fragments.html文件“注销”这一部分
<div class="menu">
<a href="#" th:href="@{/admin/logout}" class="item">注销</a>
</div>
总结
今天是第一次正式用spring boot写项目,我今天有点跟不上节奏,主要是我之前创建项目的时候创建错了…然后再一次创建的时候又很慢,就浪费了很多时间。后来写登录的时候,dao层的findByUsernameAndPassword()方法写错了,Password那里多写了一个s,然后就一直报错,看了很久也没看出来是什么意思,就去搜了一下解决办法。又找了很久才发现这里多写了s,只能说jpa太严谨了!坑也太多了,一不小心就写错了,写错了也不报红就只能自己去找。以后写的时候还是要再认真一些。