spring boot实体设计+用户登录+类别分页展示
2020-07-27 20:02
197 查看
在po实体文件夹下新建User实体类
@Entity @Table(name = “t_user”) public class User { @Id //主键标识 @GeneratedValue(strategy = GenerationType.IDENTITY) //自增 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 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 + ", newsList=" + newsList + '}'; }}
新建dao接口
public interface UserRepository extends JpaRepository<User,Long> { User findByUsernameAndPassword(String username,String password); }
在Service目录下新建一个UserService接口
public interface UserService { User checkUsers(String username,String password); }
在Impl下新建一个UserServiceImpl
@Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public User checkUsers(String username, String password) { return userRepository.findByUsernameAndPassword(username,password); } }
新建controller
@Controller @RequestMapping("/admin") public class LoginController { @Autowired UserService userService; //跳转到登陆界面 @GetMapping public String loginPage(){ return "admin/login"; } @PostMapping("/login") public String login(@RequestParam String username , @RequestParam String password, HttpSession session, RedirectAttributes attributes) { User user = userService.checkUsers(username, password); if (user != null) { user.setPassword(null); session.setAttribute("user", user); return "admin/index"; } else { attributes.addFlashAttribute("message", "用户名或密码错误"); return "redirect:/admin"; } } @GetMapping("/logout") public String logout(HttpSession session){ session.removeAttribute("user"); return "redirect:/admin"; } }
在po目录下新建一个Type实体类
@Entity @Table(name = "t_type") public class Type { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) //自增 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 + '\'' + '}'; } }
dao目录下新建一个TypeRepository接口
public interface TypeRepository extends JpaRepository<Type,Long> { }
在service目录下新建一个TypeService接口
public interface TypeService { Page<Type> listType(Pageable pageable); }
在Impl中新建一个TypeServiceImpl
@Service public class TypeServiceImpl implements TypeService { @Autowired private TypeRepository typeRepository; @Override public Page<Type> listType(Pageable pageable) { return typeRepository.findAll(pageable); //findAll方法来自jpa } }
新建一个TypeController
@Controller @RequestMapping("/admin") public class TypeController { @Autowired private TypeService typeService; @RequestMapping("/types") public String type(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model){ model.addAttribute("page",typeService.listType(pageable)); return "admin/types"; } }
效果展示
相关文章推荐
- 0727_实体设计+用户登录+类别分页展示
- 最新的spring boot技术实现登录、列表、分页、上传等功能
- spring boot+spring security+thymeleaf在页面上判断用户是否登录
- Springboot+mybatis+security搭建个人博客网站的第一天(用户登录)
- Spring Boot / Spring MVC 入门实践 (三) : 入门项目介绍与用户注册登录的实现
- spring boot 使用拦截器 实现 用户登录拦截
- spring boot 2.0完美整合mybatis注解版 加上redis储存数据 配上freemarker模板 实现用户的登录注册
- spring boot配置shiro安全框架及用户登录权限验证实现
- springboot+springsecurity+mybatis+thymeleaf实现用户登录和权限过程中的坑
- SpringBoot系列:获取当前登录用户IP
- Spring Boot +Shiro 用户角色权限设计
- (十二)Java B2B2C多用户商城 springboot架构-SSO单点登录之OAuth2.0
- Spring Boot写一个博客——实现用户登录与退出
- Spring Boot / Spring MVC 入门实践 (三) : 入门项目介绍与用户注册登录的实现
- Springboot+Shiro记录用户登录信息并获取当前登录用户信息的实现代码
- 8SpringBoot实战之用户管理的布局和分页
- SpringBoot2.0(九):SpringBoot2.0 实现微信授权登录并且获取用户信息
- spring boot MVC 三 用户注册和登录的实现
- Spring Boot + Vue 前后端分离项目如何踢掉已登录用户
- 基于SpringBoot简单实现SpringAop+Redis+cookie 单点登录 和 用户登录检测