基于springboot框架的新闻分类信息查询

7-27日Java实训感受

  1. 7-27号周五,是java实训的第八天,今天实训老师给我们系统的讲述了关于springboot框架的东西,让我了解到了一些新的知识,包括:jpa、orm、Hibernate与ssm框架中mybatis的区别,在今天的学习中收获很大。
  2. 在了解和学习了上述基础知识后,老师带我们使用springboot框架实现一个新闻系统登录以及信息分类查询,开启了我们的springboot学习之路。
  3. 此外,在老师讲完课后,自己在中午通过亲自实现老师早上讲的功能,对老师所讲的知识有了一个更加深刻的学习和体会,同时看到自己使用新框架实现的东西自己很开心。

springboot框架知识点

  1. orm:object relation mapper,对象关系映射,此框架实现了真正的面向对象,与ssm框架加不同的是,使用springboot框架不用在写相应的sql文件,而是直接对对象进行操作。
  2. Hibernate,这是springboot框架中操作对象的东西,在springboot框架中通过这个东西就可实现操作对象就相当于操作sql数据库表的效果,因此实现了真正的面向对象,而不像ssm框架中,通过写sql语句来实行啊,因此ssm框架中的mybatis是半面向对象的。
  3. jpa:java persistent api,实现java框架中持久的api接口或者方法。

springboot框架新闻信息查询实现流程

  1. 新建一个maven项目,并配置相应的pom文件,安装jar包。
    在这里插入图片描述
    在这里插入图片描述
  2. 创建springboot框架启动类(TNewsApplication)
package com.zr;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TNewsApplication {
    public static void main(String[] args){
        SpringApplication.run(TNewsApplication.class, args);
    }
}

  1. po层(User类、Type类)在这两个类中实现与数据库表的关联
package com.zr.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
    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 User() {
    }

    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 +
                '}';
    }
}

Type类,新闻信息实体类

package com.zr.po;


import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;


@Entity
@Table(name = "t_type")
public class Type {

    @Id
    @GeneratedValue
    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 + '\'' +
                '}';
    }
}

  1. dao层(User、Type)
package com.zr.dao;

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

public interface UserDao extends JpaRepository<User, Long> {
    User findByUsernameAndPassword(String username, String password);
//    void findByUsernameAndPassword(String username, String password);
}

Type类

package com.zr.dao;

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

public interface TypeDao extends JpaRepository<Type, Long> {
}

  1. Service层
    两个接口:
    User:
package com.zr.service;

import com.zr.po.User;

public interface UserService {
    User checkUser(String username, String password);
}

Type接口:

package com.zr.service;

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

public interface TypeService {

    Page<Type> listType(Pageable pageable);
}

  1. Service层
    两个实现方法:
    User:
package com.zr.service.impl;

import com.zr.dao.UserDao;
import com.zr.po.User;
import com.zr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;
    @Override
    public User checkUser(String username, String password) {
       return userDao.findByUsernameAndPassword(username, password);
//        return null;
    }
}

Type:

package com.zr.service.impl;

import com.zr.dao.TypeDao;
import com.zr.po.Type;
import com.zr.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 TypeDao typeDao;

    @Override
    public Page<Type> listType(Pageable pageable) {
        return typeDao.findAll(pageable);
//        return null;
    }
}

  1. Contorller层
    User:
package com.zr.controller;

import com.zr.po.User;
import com.zr.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.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("admin")
public class LoginController {
    @Autowired
    private UserService userService;

    @GetMapping
    public String ToLogin(){
        return "admin/login";
    }

    @PostMapping("login")
    public String login(String username, String password, HttpSession session, RedirectAttributes redirectAttributes){
        User user = userService.checkUser(username,password);
        if (user!=null){
            session.setAttribute("user",user);
            return "admin/index";
        }
        else {
            redirectAttributes.addFlashAttribute("message","用户名和密码错误1");
            return "redirect:/admin";
        }
    }

    @GetMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "admin/login";
    }
}

Type:

package com.zr.controller;

import com.zr.po.Type;
import com.zr.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;
import org.springframework.data.domain.Page;
//import sun.jvm.hotspot.debugger.Page;

//import java.awt.print.Pageable;

@Controller
@RequestMapping("/admin/types")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @RequestMapping
    public String list(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable, Model model){
        //Pageable中存放了一些查询条件
        Page<Type> page = typeService.listType(pageable);
        model.addAttribute("page",page);
        return "admin/types";

    }
}

springboot框架用户登录及新闻信息查询实现效果

  1. 用户登录:
    在这里插入图片描述
  2. 新闻系统主页面
    在这里插入图片描述
  3. 新闻信息数据库表:
    在这里插入图片描述
  4. 新闻信息查询及分页功能实现:
    在这里插入图片描述
    在这里插入图片描述
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值