springboot新闻web开发-登录和分类
2020-07-28 17:36
99 查看
1.实体类设计
首先就是需要用到的新闻类,新闻类需要跟的就是id、标题、内容等内容。
[code]package com.zr0726.news.po; import javax.persistence.*; import java.util.Date; @Entity @Table(name = "t_news") public class News { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; @Basic(fetch = FetchType.LAZY) @Lob private String content; private String firstPicture; private String flag; private String views; private boolean appreciation; private boolean shareStatement; private boolean commentabled; private boolean published; private boolean recommend; @Temporal(TemporalType.TIMESTAMP) private Date createTime; @Temporal(TemporalType.TIMESTAMP) private Date updateTime; @ManyToOne private Type type; @ManyToOne private User user; public News(){ } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getFirstPicture() { return firstPicture; } public void setFirstPicture(String firstPicture) { this.firstPicture = firstPicture; } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } public String getViews() { return views; } public void setViews(String views) { this.views = views; } public boolean isAppreciation() { return appreciation; } public void setAppreciation(boolean appreciation) { this.appreciation = appreciation; } public boolean isShareStatement() { return shareStatement; } public void setShareStatement(boolean shareStatement) { this.shareStatement = shareStatement; } public boolean isCommentabled() { return commentabled; } public void setCommentabled(boolean commentabled) { this.commentabled = commentabled; } public boolean isPublished() { return published; } public void setPublished(boolean published) { this.published = published; } public boolean isRecommend() { return recommend; } public void setRecommend(boolean recommend) { this.recommend = recommend; } 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 Type getType() { return type; } public void setType(Type type) { this.type = type; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString() { return "News{" + "id=" + id + ", title='" + title + '\'' + ", content='" + content + '\'' + ", firstPicture='" + firstPicture + '\'' + ", flag='" + flag + '\'' + ", views='" + views + '\'' + ", appreciation=" + appreciation + ", shareStatement=" + shareStatement + ", commentabled=" + commentabled + ", published=" + published + ", recommend=" + recommend + ", createTime=" + createTime + ", updateTime=" + updateTime + ", type=" + type + ", user=" + user + '}'; } }
[code]package com.zr0726.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 + '}'; } }
2.登录功能设计
首先要写的是UserRepository,用来提供用户相应的功能
[code]package com.zr0726.news.dao; import com.zr0726.news.po.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User,Long> { User findByUsernameAndPassword(String username,String password); }
还有UserServiceImpl用来实现具体的service服务
[code]package com.zr0726.news.service.impl; import com.zr0726.news.dao.UserRepository; import com.zr0726.news.po.User; import com.zr0726.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); } }
[code]package com.zr0726.news.service; import com.zr0726.news.po.User; public interface UserService { User checkUsers(String username,String password); }
[code]package com.zr0726.news.web.admin; import com.zr0726.news.po.User; import com.zr0726.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 private 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"; } }
[code]package com.zr0726.news; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class NewsApplication { public static void main(String[] args) { SpringApplication.run(NewsApplication.class, args); } }
3.类别分类展示
然后就是实现分类的功能,主要是根据type来分类
[code]package com.zr0726.news.service.impl; import com.zr0726.news.dao.TypeRepository; import com.zr0726.news.po.Type; import com.zr0726.news.service.TypeService; 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); } }
[code]package com.zr0726.news.service; import com.zr0726.news.po.Type; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface TypeService { Page<Type> listType(Pageable pageable); }
4.运行
前端代码略去,直接看最终的测试效果吧
5.总结
今天是写springboot的第二天,主要是实现新闻相关的模块,可以实现登录功能和新闻的分类查询功能,写的时候发现springboot和springmvc其实并没有太大的区别,不过springboot配置方面要比ssm简单很多,没有那么多xml文件,只有一个yml文件,springboot相关的配置都在里面配置,可以让程序员更加专注与写相应的逻辑功能,而不是配置相关的模块。
相关文章推荐
- IBM Rational AppScan 无法记录登录序列 分类: 数据安全 2015-03-18 16:46 158人阅读 评论(0) 收藏
- IBM Rational AppScan 无法记录登录序列 分类: 数据安全 2015-03-18 16:46 157人阅读 评论(0) 收藏
- 初试Scrapy(三)上---CSDN自动登录获取博客分类列表
- Laravel5.1 实现第三方登录认证(包括微博、QQ、微信、豆瓣) 2017-01-08 18:08 457人阅读 评论(0) 收藏 举报 分类: laravel框架(33) 目录(?)[
- (eblog)6、博客分类填充、登录注册逻辑
- 【中软实训】7.实体设计+用户登录+类别分类展示
- Spring MVC实现QQ第三方登录 博客分类: spring
- 初试Scrapy(三)下—CSDN自动登录获取博客分类列表
- 搜索引擎登录,网站分类目录登录地址
- mysql远程登录出错 分类: mysql 2014-01-27 14:37 386人阅读 评论(0) 收藏
- ubuntu14.04使用root用户登录桌面 分类: 学习笔记 linux ubuntu 2015-07-05 10:30 199人阅读 评论(0) 收藏
- ubuntu12 root账户自动登录 分类: ubuntu 2015-02-28 13:47 58人阅读 评论(0) 收藏
- 24讲项目实战:后台登录界面更换、站点栏目分类
- 05、面包屑分类,商品的分类管理CRUD,运营商的springSecurity安全框架(配置文件中定义用户名和密码角色),商家注册以非加密方式登录
- 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 615人阅读 评论(0) 收藏
- 微信小程序前台开发——实现登录,底部导航栏,顶部导航栏(分类显示)
- 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 614人阅读 评论(0) 收藏
- 登录DMOZ/如何登录DMOZ分类目录
- 取消windows XP用户密码验证登录方式 分类: 开发常见问题解决方案 2010-04-11 08:07 1107人阅读 评论(0) 收藏