(一)创建一张新闻表和对应java的pojo类
1、创建t_newa表
CREATE TABLE `t_news` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`news_id` char(255) NOT NULL,
`author_id` int(11) NOT NULL,
`news_title` varchar(255) NOT NULL,
`news_content` text NOT NULL,
`news_imgs` text,
`create_time` varchar(255) NOT NULL DEFAULT '0000-00-00 00:00:00',
`update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `newsId` (`news_id`)
)
2、 新闻表所对应的pojo类
import lombok.Data;
@Data
public class NewsPO {
private int id;
private String newsId;
private int authorId;
private String newsTitle;
private String newsContent;
private String newsImgs;
private long createTime;、
}
3、 分页查询数据包装类
public class BasePage {
//当前页码
private Integer pageNum=1;
//总页数
private Integer pageTotal;
//每页的条数
private Integer pageSize=10;
//总条数
private Integer totalSize;
//起始索引
private Integer startIndex;
//是否有更多
private boolean isMore;
private List list;
public BasePage(){
super();
}
public BasePage(Integer totalSize,Integer pageNum,Integer pageSize){
this.pageNum=pageNum;
this.pageSize=pageSize;
this.totalSize=totalSize;
this.pageTotal=(this.totalSize+this.pageSize-1)/this.pageSize;
this.startIndex=(this.pageNum-1)*pageSize;
this.isMore=this.pageNum
(二)SpringBoot集成PagerHelper插件
1、pom.xml文件添加pagerhelper的坐标依赖
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
稳妥起见,pagerhelper的版本使用1.2.3
2、在SpringBoot项目的启动入口加入pagerhelper的注入配置
@SpringBootApplication(scanBasePackages = {"cn.fcw.bb.webapply"})
@MapperScan(basePackages = {"cn.fcw.bb.webapply.mapper"})
public class WebApplyApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplyApplication.class, args);
}
/**
* https://github.com/pagehelper/Mybatis-PageHelper
* 注入PagerHelper的配置
*/
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper=new PageHelper();
Properties properties=new Properties();
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("reasonable","true");
properties.setProperty("dialect","mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
}
3、在application.properties中加入pagerhelper的启动配置
#分页插件
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
4、创建mapper接口和mapper.xml文件
public interface INewsMapper {
List findAllNews();
Integer getTotalNum();
}
实现mapper接口的xml文件
5、servie的业务逻辑
public interface INewsService {
@Transactional
BasePage findAllNews(Integer pageNum, Integer pageSize);
}
@Service
public class NewsServiceImpl implements INewsService {
@Autowired
private INewsMapper newsMapper;
@Override
public BasePage findAllNews(Integer pageNum, Integer pageSize) {
int total=newsMapper.getTotalNum();
PageHelper.startPage(pageNum,pageSize);
List list=newsMapper.findAllNews();
BasePage basePage=new BasePage<>(total,pageNum,pageSize);
basePage.setList(list);
return basePage;
}
}
6、controller代码
@RestController
@RequestMapping("/news")
public class NewsController {
@Autowired
private INewsService newsService;
@RequestMapping(value = "/getNewsList",method = {RequestMethod.GET})
public BasePage getNewsList(@Param(value = "pageNum")Integer pageNum, @Param(value ="pageSize")Integer pageSize){
if(pageNum==null) pageNum=1;
if(pageSize==null) pageSize=40;
BasePage basePage=newsService.findAllNews(pageNum,pageSize);
return basePage;
}
}
7、管理后台测试页面
新闻列表
新闻编号
新闻标题
新闻内容
新闻图片
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
1、环境搭建,centos安装 ElasticSearch
(1)官网下载es的tar压缩文件文件(https://www.elastic.co/downloads/elasticsearch),上传至centos服务器目录
执行解压命令:tar -xvzf elasticsearch-6.4.0.tar.gz
(2)修改配置文件:
进入es的配置文件: cd elasticsearch/config
修改es的host和端口:vi elasticsearch.yml
(3)启动ES服务(如果启动报错可以参考这篇博客https://www.cnblogs.com/xxoome/p/6663993.html)
启动命令:
启动成功的信息:
浏览器测试:
2、后台springboot集成Elasticsearch
(1)pom.xml文件加入Elasticsearch的maven坐标
org.springframework.boot
spring-boot-starter-data-elasticsearch
(2)application.properties添加es的配置
#客户端访问es服务必须使用9300端口
spring.data.elasticsearch.cluster-nodes=192.168.137.133:9300
(3) java后台代码
需求:把新闻表中的记录存放到es上,然后根据标题关键字或内容关键字查询新闻
1>创建新闻的文档类(用于存储到es数据库上)
/**
* 新闻的es文档类
* @Document 文档类注解
* indexName 文档索引
* type 文档类型
*/
@Document(indexName = "newsdb",type = "news")
public class EsNews implements Serializable {
@Id //标示主键
private String id; //id必须是字符串
private String newsTitle;
private String newsContent;
private String newsImgs;
protected EsNews(){ } //JPA 空参构造方法 编写规范
public EsNews(String id,String newsTitle,String newsContent,String newsImgs){
this.id=id;
this.newsTitle=newsTitle;
this.newsContent=newsContent;
this.newsImgs=newsImgs;
}
}
2>创建新闻文档的资源库
public interface EsNewsRepository extends ElasticsearchRepository {
//按照含有标题关键字或者内容关键字搜索新闻
List findDistinctEsNewsByNewsTitleContainingOrNewsContentContaining(String title, String content);
}
3>controller层的业务代码,查询分页数据的时候讲分页数据存入es中:
@RestController
@RequestMapping("/news")
public class NewsController {
@Autowired
private INewsService newsService;
@Autowired
private EsNewsRepository esNewsRepository;
@RequestMapping("/getNewsCommentDetailById")
public BaseBean getNewsCommentDetailById(){
return newsService.findNewsDetailById(1);
}
@RequestMapping(value = "/getNewsList",method = {RequestMethod.GET})
public BasePage getNewsList(@Param(value = "pageNum")Integer pageNum, @Param(value ="pageSize")Integer pageSize){
if(pageNum==null) pageNum=1;
if(pageSize==null) pageSize=40;
BasePage basePage=newsService.findAllNews(pageNum,pageSize);
saveNews2ES(basePage);//调用es存储方法
return basePage;
}
/**
* 将分页查出来的新闻数据添加到es中
*/
private void saveNews2ES(BasePage basePage){
List list=basePage.getList();
esNewsRepository.deleteAll();//先清空es存储
EsNews news;
for (NewsPO newsPO : list) {
news=new EsNews(newsPO.getNewsId(),newsPO.getNewsTitle(),newsPO.getNewsContent(),newsPO.getNewsImgs());
esNewsRepository.save(news);
}
}
/**
*通过关键字搜索新闻
* @param title
* @param content
* @return
*/
@RequestMapping("/searchNewsList")
public List searchNews(@Param(value = "title")String title,@Param(value = "content")String content) {
return esNewsRepository.findDistinctEsNewsByNewsTitleContainingOrNewsContentContaining(title,content);
}
}
4>后台页面代码:
新闻列表
新闻编号
新闻标题
新闻内容
新闻图片
运行效果: