Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示

Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示

  • 实体设计
  • 用户登录
  • 类别分页展示

Java方向(SSM+SpringBoot)项目实训
Day7(2020.7.27)

实体设计

新闻
分类
标签
用户
评论

实体间关系:
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第1张图片
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第2张图片
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第3张图片
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第4张图片
1.SpringBoot项目的pom.xml文件中将 properties~ /dependencies中的代码替换如下:

<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>8.0.21</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>

之后点击右侧的Maven,刷新导入项目所需要的的依赖;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第5张图片
JPA:
一套规范,Hibernate实现这套规范,Spring data jpa
侧重于实体类与数据库表的映射关系
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第6张图片
修改application.yml配置文件;
application.yml中添加:

  jpa:
    hibernate:
      ddl-auto: update
      use-new-id-generator-mappings: false
    show-sql: true

2.po下创建User、Type、News类;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第7张图片
User:

package com.roger0123.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) //Date年月日 Time时分秒 TimeStamp=Date+Time
    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 +
                '}';
    }
}

Type

package com.roger0123.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 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

package com.roger0123.news.po;

import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
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 +
                '}';
    }
}

用户登录

1.dao下创建UserRepository接口
在这里插入图片描述

UserRepository:

package com.roger0123.news.dao;

import com.roger0123.news.po.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User,Long> {
     

    User findByUsernameAndPassword(String username,String password);
    
}

2.service下创建UserService接口;
在这里插入图片描述
UserService:

package com.roger0123.news.service;

import com.roger0123.news.po.User;

public interface UserService {
     

    User checkUsers(String username,String password);
}

3.service.impl下创建UserServiceImpl类;
在这里插入图片描述
UserServiceImpl:

package com.roger0123.news.service.impl;

import com.roger0123.news.dao.UserRepository;
import com.roger0123.news.po.User;
import com.roger0123.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);
    }
}

4.web下创建admin包并且在admin下创建LoginController类;
在这里插入图片描述
LoginController

package com.roger0123.news.web.admin;

import com.roger0123.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.RequestMapping;

@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";
    }
}

5.运行项目,自动创建实体类中所需的sb_learn数据库表;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第8张图片
刷新Navicat,数据库表创建成功;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第9张图片
t_news:
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第10张图片
t_type:
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第11张图片
t_user:
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第12张图片
Test
1.本地数据库中插入测试数据;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第13张图片

2.运行程序,进入登录界面;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第14张图片
3.输入正确的用户名密码,点击登录,登录成功,跳转至新闻管理界面;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第15张图片

类别分页展示

1.dao下创建TypeRepository接口;
在这里插入图片描述
TypeRepository:

package com.roger0123.news.dao;

import com.roger0123.news.po.Type;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TypeRepository extends JpaRepository <Type,Long>{
     

}

2.service下创建TypeService接口;
在这里插入图片描述
TypeService:

package com.roger0123.news.service;

import com.roger0123.news.po.Type;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface TypeService {
     

    Page<Type> listType(Pageable pageable);

}

3.service.impl下创建TypeServiceImpl类;
在这里插入图片描述
TypeService:

package com.roger0123.news.service.impl;

import com.roger0123.news.dao.TypeRepository;
import com.roger0123.news.po.Type;
import com.roger0123.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);
    }
}

4.web.admin下创建TypeController;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第16张图片

TypeController:

package com.roger0123.news.web.admin;

import com.roger0123.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.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("page",typeService.listType(pageable));
        System.out.println("page"+typeService.listType(pageable));
        return "admin/types";
    }
}

Test
1.本地数据库中插入测试数据;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第17张图片

2.运行程序,登陆成功后,跳转至新闻管理界面,点击分类;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第18张图片

Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第19张图片
3.进入类别分页展示界面,点击上一页,下一页跳转成功;
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第20张图片
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第21张图片
Java方向(SSM+SpringBoot)项目实训Day7-SpringBoot(新闻管理系统) 实体设计+用户登录+类别分页展示_第22张图片

你可能感兴趣的:(learn,jpa,entity,spring,boot)