欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java基于Springboot实现实体设计和用户登陆和分页显示

程序员文章站 2024-01-07 22:07:46
Java开发学习.Day7基于Springboot实现 实体设计+用户登陆+分页显示实体设计用户登录分页显示基于Springboot实现 实体设计+用户登陆+分页显示实体设计针对系统功能,我们构建了Type, News, Tags, User, Comment五大类其中一种Type包含着多条News一条News可以有多个Tag一种Tag可能包含多个News一个User可以发多个News一条News有多个Comment用户登录首先,在pom.xml添加依赖



基于Springboot实现 实体设计+用户登陆+分页显示

实体设计

针对系统功能,我们构建了Type, News, Tags, User, Comment五大实体类

其中

  1. 一种Type包含着多条News
  2. 一条News可以有多个Tag
  3. 一种Tag可能包含多个News
  4. 一个User可以发多个News
  5. 一条News有多个Comment

Java基于Springboot实现实体设计和用户登陆和分页显示

用户登录

  1. 首先,在pom.xml添加依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.atlassian.commonmark</groupId> <artifactId>commonmark</artifactId> <version>0.10.0</version> </dependency> <dependency> <groupId>com.atlassian.commonmark</groupId> <artifactId>commonmark-ext-heading-anchor</artifactId> <version>0.10.0</version> </dependency> <dependency> <groupId>com.atlassian.commonmark</groupId> <artifactId>commonmark-ext-gfm-tables</artifactId> <version>0.10.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> <!--<scope>runtime</scope>--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> </dependencies> 
  1. 创建文件目录如下
    Java基于Springboot实现实体设计和用户登陆和分页显示

  2. 具体代码实现
    先在po中创建User类

package com.xxxxbt.news.po; import javax.persistence.*; import java.util.ArrayList; import java.util.Date; import java.util.List; @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中创建接口UserRepository, jpa将根据User类型和name属性从数据库中获取指定对象

package com.xxxxbt.news.dao; import com.xxxxbt.news.po.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User,Long> { User findByUsernameAndPassword(String username, String password); } 

在service中创建接口UserService

package com.xxxxbt.news.service; import com.xxxxbt.news.po.User; public interface UserService { User checkUsers(String username, String password); } 

在service.impl中实现UserService接口

package com.xxxxbt.news.service.impl; import com.xxxxbt.news.dao.UserRepository; import com.xxxxbt.news.po.User; import com.xxxxbt.news.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public User checkUsers(String username, String password) { return userRepository.findByUsernameAndPassword(username,password); } } 

最后在web.admin下添加控制器LoginController

package com.xxxxbt.news.web.admin; import com.xxxxbt.news.po.User; import com.xxxxbt.news.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.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.servlet.http.HttpSession; @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"; } } 
  1. 界面展示( 登录前需要在数据库中手动添加用户数据

Java基于Springboot实现实体设计和用户登陆和分页显示
Java基于Springboot实现实体设计和用户登陆和分页显示

分页显示

该功能基于Type类实现

  1. 具体代码实现
    先在po中创建Type 类
package com.xxxxbt.news.po; import javax.persistence.*; import javax.validation.constraints.NotBlank; import java.util.ArrayList; import java.util.List; @Entity @Table(name = "t_type") public class Type { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) //自增 private Long id; @NotBlank(message = "分类名称不能为空") 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

package com.xxxxbt.news.dao; import com.xxxxbt.news.po.Type; import org.springframework.data.jpa.repository.JpaRepository; public interface TypeRepository extends JpaRepository<Type,Long> { } 

在service中创建接口TypeService

package com.xxxxbt.news.service; import com.xxxxbt.news.po.Type; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface TypeService { Page<Type> listType(Pageable pageable); } 

在service.impl中实现TypeService 接口

package com.xxxxbt.news.service.impl; import com.xxxxbt.news.dao.TypeRepository; import com.xxxxbt.news.po.Type; import com.xxxxbt.news.service.TypeService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @Service public class TypeServiceImpl implements TypeService { @Autowired private TypeRepository typeRepository; @Override public Page<Type> listType(Pageable pageable) { return typeRepository.findAll(pageable); //findAll方法来自jpa } } 

最后在web.admin下添加控制器TypeController

package com.xxxxbt.news.web.admin; import com.xxxxbt.news.po.Type; import com.xxxxbt.news.service.TypeService; import org.springframework.beans.factory.annotation.Autowired; 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.validation.BindingResult; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.validation.Valid; @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"; } } 
  1. 界面展示
    数据展示界面
    Java基于Springboot实现实体设计和用户登陆和分页显示
    url中的page参数将决定当前页的数据
    Java基于Springboot实现实体设计和用户登陆和分页显示

本文地址:https://blog.csdn.net/xxxxxbt/article/details/107650825

上一篇:

下一篇: