新闻系统
各个实体之间的关系的思维导图
新闻实体
(左侧为实体之间的关系 右侧为自己的功能属性)
User实体类
comment类
在pom.xml配置相关的依赖(这里就不截图了)
其中里面的JAVA代码靠JPA的一套规范其实就是Hibernate 叫Spring data.jpa
更侧重与实体类与表的映射关系 一些基本的操作是不需要写sql语句的
侧重于管理实体类,通过实体类创建数据库的表@Entity+@table
User实体类
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")//指定生成中间表 用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;
}
}
Type实体类
package com.zr0726.news.po;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@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 + '\'' +
'}';
}
}
News实体类
由于comment加载的数据过多,要用到懒加载
@Basic(fetch = FetchType.LAZY)//懒加载,当数据需要用到才会加载
@Lob//数据存储量大就需要用到
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 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 +
'}';
}
}
导入相关文件(内容略)
LoginController实现登录注销功能
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")//在端口8080才能进入Controller
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){
//登录成功的用户密码等信息存到session中
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";
}
}
//注销功能 就是对session进行操作 注销的话就是讲session里面的信息清除掉
@GetMapping("/logout")
public String logout(HttpSession session){
session.removeAttribute("user");
return "redirect:/admin";//返回到主页上
}
}
数据库的账号密码随便弄一个
在端口8080/admin进行登录操作
登录成功
2.分页功能
创建TypeController
package com.zr0726.news.web.admin;
import com.zr0726.news.service.TypeService;
import net.bytebuddy.TypeCache;
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.web.bind.annotation.RequestMapping;
@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("pages", typeService.listType(pageable));
return "admin/types";
}
}
创建TypeRepository接口
package com.zr0726.news.dao;
import com.zr0726.news.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;
//指定要使用实体的类型为Type,和主键的类型为Long
public interface TypeRepository extends JpaRepository<Type,Long> {
}
创建TypeService接口 利用SpringDate提供的分页接口Page<>
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 {
//利用SpringDate提供的分页接口Page<> 指定分页的对象为Pageable
Page<Type> listType(Pageable pageable);
}
创建TypeService类
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);
}
}
在自动创建的数据库添加信息
成功效果图: