新闻管理系统——首页实现

目  录

1、左侧新闻分类列表

①首先查询列表的话一般都是分页查询,需要引入分页插件

②国内新闻分类列表(分类查询新闻信息)

③编写业务逻辑代码——定义service接口

④编写接口实现类

⑤编写控制器代码

⑦效果图

2、新闻标题——获取首页的新闻列表信息

①在NewsMapper.xml映射文件中进行编写sql语句

②编写dao数据库访问层代码,添加关于查询新闻列表的抽象方法,通过mybatis框架跟sql语句关联起来

③service业务层代码

④编写service实现类

⑤控制器代码

⑥修改html页面信息

⑦效果图

 3、新闻分类——分页查询新闻列表

①dao层

②service接口

③service实现类

④控制器代码——加入从mybatis-plus中实现的通用方法来获取列表信息

⑤效果图


1、左侧新闻分类列表

①首先查询列表的话一般都是分页查询,需要引入分页插件

配置类:MybatisPlusConfig.java

package com.gec.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

②国内新闻分类列表(分类查询新闻信息)

其实质是按照新闻分类来查询新闻列表信息,用sql语句去编写根据新闻分类的编号去查询出具体的新闻信息。

NewsMapper.java

package com.gec.newsproject.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gec.newsproject.entity.News;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

public interface NewsMapper extends BaseMapper {

    @Select("select * from news where ntid = #{topicId} order by ncreateDate desc limit ${number}")
    List getNewsListByTopicId(@Param("topicId") int topicId, @Param("number") int number);
}

③编写业务逻辑代码——定义service接口

NewsService.java

package com.gec.newsproject.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.gec.newsproject.entity.News;

import java.util.List;

public interface NewsService extends IService {

    /**
     * 通过新闻分类来获取新闻
     * @param topicId
     * @param number
     * @return
     */
    public List getNewsListByTopicId(int topicId,int number);
}

④编写接口实现类

NewsServicelmpl.java

package com.gec.newsproject.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.newsproject.dao.NewsMapper;
import com.gec.newsproject.entity.News;
import com.gec.newsproject.service.NewsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class NewsServiceImpl extends ServiceImpl implements NewsService {

    @Override
    public List getNewsListByTopicId(int topicId, int number) {
        return  baseMapper.getNewsListByTopicId(topicId,number);
    }
}

⑤编写控制器代码

HelloController.java

package com.gec.newsproject.controller;

import com.gec.newsproject.entity.News;
import com.gec.newsproject.service.NewsService;
import com.gec.newsproject.vo.NewsQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class HomeController {

    @Autowired
    NewsService newsService;

    @RequestMapping(value = {"/","/index.html","/home.html"})
    public String index(Model model, NewsQueryVo newsQueryVo){
        //先查询出来左侧的分类标题
        List newsList1 = newsService.getNewsListByTopicId(1, 6);
        List newsList2 = newsService.getNewsListByTopicId(2, 6);
        List newsList3 = newsService.getNewsListByTopicId(3, 6);
        model.addAttribute("newsList1",newsList1);
        model.addAttribute("newsList2",newsList2);
        model.addAttribute("newsList3",newsList3);
        return "index";
    }
}

⑥修改前面编写的index.html,在首页加入新闻列表信息

index.html




    
    新闻管理系统
    





⑦效果图

新闻管理系统——首页实现_第1张图片

2、新闻标题——获取首页的新闻列表信息

①在NewsMapper.xml映射文件中进行编写sql语句

NewsMapper.xml





    
    
        
        
        
        
        
        
        
        
        
    

    
    
        nid, ntid, ntitle, nauthor, ncreateDate, npicPath, ncontent, nmodifyDate, nsummary
    

    


②编写dao数据库访问层代码,添加关于查询新闻列表的抽象方法,通过mybatis框架跟sql语句关联起来

 NewsMapper.java

package com.gec.newsproject.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gec.newsproject.entity.News;
import com.gec.newsproject.vo.NewsQueryVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface NewsMapper extends BaseMapper {

    //分类查询新闻信息
    @Select("select * from news where ntid = #{topicId} order by ncreateDate desc limit ${number}")
    List getNewsListByTopicId(@Param("topicId") int topicId, @Param("number") int number);

    //分页查询新闻列表
    IPage getNewsListByPage(@Param("page")IPage page, @Param("news")NewsQueryVo newsQueryVo);
}

③service业务层代码

NewsService.java

package com.gec.newsproject.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.gec.newsproject.entity.News;
import com.gec.newsproject.vo.NewsQueryVo;

import java.util.List;

public interface NewsService extends IService {

    /**
     * 通过新闻分类来获取新闻
     * @param topicId
     * @param number
     * @return
     */
    public List getNewsListByTopicId(int topicId,int number);

    /**
     * 分页查询新闻列表
     * @param page
     * @param newsQueryVo
     * @return
     */
    IPage getNewsListByPage(IPage page, NewsQueryVo newsQueryVo);
}

④编写service实现类

NewsServiceImpl.java

package com.gec.newsproject.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.newsproject.dao.NewsMapper;
import com.gec.newsproject.entity.News;
import com.gec.newsproject.service.NewsService;
import com.gec.newsproject.vo.NewsQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class NewsServiceImpl extends ServiceImpl implements NewsService {

    @Override
    public List getNewsListByTopicId(int topicId, int number) {
        return  baseMapper.getNewsListByTopicId(topicId,number);
    }

    @Override
    public IPage getNewsListByPage(IPage page, NewsQueryVo newsQueryVo) {
        return baseMapper.getNewsListByPage(page,newsQueryVo);
    }
}

⑤控制器代码

HomeController.java

package com.gec.newsproject.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.newsproject.entity.News;
import com.gec.newsproject.service.NewsService;
import com.gec.newsproject.vo.NewsQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class HomeController {

    @Autowired
    NewsService newsService;

    @RequestMapping(value = {"/","/index.html","/home.html"})
    public String index(Model model, NewsQueryVo newsQueryVo){
        //先查询出来左侧的分类标题
        List newsList1 = newsService.getNewsListByTopicId(1, 6);
        List newsList2 = newsService.getNewsListByTopicId(2, 6);
        List newsList3 = newsService.getNewsListByTopicId(3, 6);
        model.addAttribute("newsList1",newsList1);
        model.addAttribute("newsList2",newsList2);
        model.addAttribute("newsList3",newsList3);

        //创建分页查询对象 帮助分页查询信息
        //设置页码和分页的 数量
        IPage page = new Page<>(newsQueryVo.getPageNo(),newsQueryVo.getPageSize());
        page = newsService.getNewsListByPage(page, newsQueryVo);
        model.addAttribute("page",page);
        //设置分页信息参数
        model.addAttribute("newVo",newsQueryVo);
        return "index";
    }
}

⑥修改html页面信息

index.html




    
    新闻管理系统
    







⑦效果图

新闻管理系统——首页实现_第2张图片

 3、新闻分类——分页查询新闻列表

①dao层

TopicMapper.java

package com.gec.newsproject.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gec.newsproject.entity.Topic;

public interface TopicMapper extends BaseMapper {
}

②service接口

TopicService.java

package com.gec.newsproject.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.gec.newsproject.entity.Topic;

public interface TopicService extends IService {
}

③service实现类

TopicServiceImpl.java

package com.gec.newsproject.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.newsproject.dao.TopicMapper;
import com.gec.newsproject.entity.Topic;
import com.gec.newsproject.service.TopicService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TopicServiceImpl extends ServiceImpl implements TopicService {
}

④控制器代码——加入从mybatis-plus中实现的通用方法来获取列表信息

HomeController.java

package com.gec.newsproject.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.newsproject.entity.News;
import com.gec.newsproject.entity.Topic;
import com.gec.newsproject.service.NewsService;
import com.gec.newsproject.service.TopicService;
import com.gec.newsproject.vo.NewsQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class HomeController {

    @Autowired
    NewsService newsService;

    @Autowired
    TopicService topicService;

    @RequestMapping(value = {"/","/index.html","/home.html"})
    public String index(Model model, NewsQueryVo newsQueryVo){
        //先查询出来左侧的分类标题
        List newsList1 = newsService.getNewsListByTopicId(1, 6);
        List newsList2 = newsService.getNewsListByTopicId(2, 6);
        List newsList3 = newsService.getNewsListByTopicId(3, 6);
        //将数据存入模型中
        model.addAttribute("newsList1",newsList1);
        model.addAttribute("newsList2",newsList2);
        model.addAttribute("newsList3",newsList3);

        //查询出所有分类信息
        List topicList = topicService.list();
        model.addAttribute("topicList",topicList);

        //创建分页查询对象 帮助分页查询信息
        //设置页码和分页的 数量
        IPage page = new Page<>(newsQueryVo.getPageNo(),newsQueryVo.getPageSize());
        page = newsService.getNewsListByPage(page, newsQueryVo);
        model.addAttribute("page",page);
        //设置分页信息参数
        model.addAttribute("newVo",newsQueryVo);
        return "index";
    }
}

⑤效果图

新闻管理系统——首页实现_第3张图片

你可能感兴趣的:(java,spring,boot,intellij-idea,html)