Skip to content

Commit

Permalink
增加新闻阅读数
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyang committed Jul 26, 2021
1 parent b99b6ae commit 1cffbae
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 14 deletions.
1 change: 1 addition & 0 deletions doc/sql/20210726.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table news add column `read_count` BIGINT NOT NULL DEFAULT '0' COMMENT '阅读量' after content;
4 changes: 3 additions & 1 deletion doc/sql/novel_plus.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1838,4 +1838,6 @@ INSERT INTO `sys_role_menu`(`id`, `role_id`, `menu_id`) VALUES (4888, 1, 234);

delete from sys_menu where menu_id = 202;

INSERT INTO crawl_source(`id`, `source_name`, `crawl_rule`, `source_status`, `create_time`, `update_time`) VALUES (16, 'i笔趣阁', '{\"bookListUrl\":\"http://m.ibiquge.net/xclass/{catId}/{page}.html\",\"catIdRule\":{\"catId1\":\"1\",\"catId2\":\"2\",\"catId3\":\"3\",\"catId4\":\"4\",\"catId5\":\"6\",\"catId6\":\"5\",\"catId7\":\"7\"},\"bookIdPatten\":\"href=\\\"/(\\\\d+_\\\\d+)/\\\"\",\"pagePatten\":\"value=\\\"(\\\\d+)/\\\\d+\\\"\",\"totalPagePatten\":\"value=\\\"\\\\d+/(\\\\d+)\\\"\",\"bookDetailUrl\":\"http://m.ibiquge.net/{bookId}/\",\"bookNamePatten\":\"<span class=\\\"title\\\">([^/]+)</span>\",\"authorNamePatten\":\"<a href=\\\"/author/\\\\d+/\\\">([^/]+)</a>\",\"picUrlPatten\":\"<img src=\\\"([^>]+)\\\"\\\\s+onerror=\\\"this.src=\",\"picUrlPrefix\":\"http://m.ibiquge.net\",\"statusPatten\":\">状态:([^/]+)</li>\",\"bookStatusRule\":{\"连载\":0,\"完结\":1},\"visitCountPatten\":\">点击:(\\\\d+)</li>\",\"descStart\":\"<p class=\\\"review\\\">\",\"descEnd\":\"</p>\",\"bookIndexUrl\":\"http://www.ibiquge.net/{bookId}/\",\"bookIndexStart\":\"正文</dt>\",\"indexIdPatten\":\"<a\\\\s+style=\\\"\\\"\\\\s+href=\\\"/\\\\d+_\\\\d+/(\\\\d+)\\\\.html\\\">[^/]+</a>\",\"indexNamePatten\":\"<a\\\\s+style=\\\"\\\"\\\\s+href=\\\"/\\\\d+_\\\\d+/\\\\d+\\\\.html\\\">([^/]+)</a>\",\"bookContentUrl\":\"http://www.ibiquge.net/{bookId}/{indexId}.html\",\"contentStart\":\"</p>\",\"contentEnd\":\"<div align=\\\"center\\\">\"}', 0, '2021-02-04 21:31:23', '2021-02-04 21:31:23');
INSERT INTO crawl_source(`id`, `source_name`, `crawl_rule`, `source_status`, `create_time`, `update_time`) VALUES (16, 'i笔趣阁', '{\"bookListUrl\":\"http://m.ibiquge.net/xclass/{catId}/{page}.html\",\"catIdRule\":{\"catId1\":\"1\",\"catId2\":\"2\",\"catId3\":\"3\",\"catId4\":\"4\",\"catId5\":\"6\",\"catId6\":\"5\",\"catId7\":\"7\"},\"bookIdPatten\":\"href=\\\"/(\\\\d+_\\\\d+)/\\\"\",\"pagePatten\":\"value=\\\"(\\\\d+)/\\\\d+\\\"\",\"totalPagePatten\":\"value=\\\"\\\\d+/(\\\\d+)\\\"\",\"bookDetailUrl\":\"http://m.ibiquge.net/{bookId}/\",\"bookNamePatten\":\"<span class=\\\"title\\\">([^/]+)</span>\",\"authorNamePatten\":\"<a href=\\\"/author/\\\\d+/\\\">([^/]+)</a>\",\"picUrlPatten\":\"<img src=\\\"([^>]+)\\\"\\\\s+onerror=\\\"this.src=\",\"picUrlPrefix\":\"http://m.ibiquge.net\",\"statusPatten\":\">状态:([^/]+)</li>\",\"bookStatusRule\":{\"连载\":0,\"完结\":1},\"visitCountPatten\":\">点击:(\\\\d+)</li>\",\"descStart\":\"<p class=\\\"review\\\">\",\"descEnd\":\"</p>\",\"bookIndexUrl\":\"http://www.ibiquge.net/{bookId}/\",\"bookIndexStart\":\"正文</dt>\",\"indexIdPatten\":\"<a\\\\s+style=\\\"\\\"\\\\s+href=\\\"/\\\\d+_\\\\d+/(\\\\d+)\\\\.html\\\">[^/]+</a>\",\"indexNamePatten\":\"<a\\\\s+style=\\\"\\\"\\\\s+href=\\\"/\\\\d+_\\\\d+/\\\\d+\\\\.html\\\">([^/]+)</a>\",\"bookContentUrl\":\"http://www.ibiquge.net/{bookId}/{indexId}.html\",\"contentStart\":\"</p>\",\"contentEnd\":\"<div align=\\\"center\\\">\"}', 0, '2021-02-04 21:31:23', '2021-02-04 21:31:23');

alter table news add column `read_count` BIGINT NOT NULL DEFAULT '0' COMMENT '阅读量' after content;
19 changes: 15 additions & 4 deletions novel-common/src/main/java/com/java2nb/novel/entity/News.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.java2nb.novel.entity;


import javax.annotation.Generated;
import java.io.Serializable;
import java.util.Date;
import javax.annotation.Generated;

public class News implements Serializable {
public class News {
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private Long id;

Expand All @@ -21,6 +19,9 @@ public class News implements Serializable {
@Generated("org.mybatis.generator.api.MyBatisGenerator")
private String title;

@Generated("org.mybatis.generator.api.MyBatisGenerator")
private Long readCount;

@Generated("org.mybatis.generator.api.MyBatisGenerator")
private Date createTime;

Expand Down Expand Up @@ -86,6 +87,16 @@ public void setTitle(String title) {
this.title = title == null ? null : title.trim();
}

@Generated("org.mybatis.generator.api.MyBatisGenerator")
public Long getReadCount() {
return readCount;
}

@Generated("org.mybatis.generator.api.MyBatisGenerator")
public void setReadCount(Long readCount) {
this.readCount = readCount;
}

@Generated("org.mybatis.generator.api.MyBatisGenerator")
public Date getCreateTime() {
return createTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public final class NewsDynamicSqlSupport {
@Generated("org.mybatis.generator.api.MyBatisGenerator")
public static final SqlColumn<String> title = news.title;

@Generated("org.mybatis.generator.api.MyBatisGenerator")
public static final SqlColumn<Long> readCount = news.readCount;

@Generated("org.mybatis.generator.api.MyBatisGenerator")
public static final SqlColumn<Date> createTime = news.createTime;

Expand Down Expand Up @@ -52,6 +55,8 @@ public static final class News extends SqlTable {

public final SqlColumn<String> title = column("title", JDBCType.VARCHAR);

public final SqlColumn<Long> readCount = column("read_count", JDBCType.BIGINT);

public final SqlColumn<Date> createTime = column("create_time", JDBCType.TIMESTAMP);

public final SqlColumn<Long> createUserId = column("create_user_id", JDBCType.BIGINT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@Mapper
public interface NewsMapper {
@Generated("org.mybatis.generator.api.MyBatisGenerator")
BasicColumn[] selectList = BasicColumn.columnList(id, catId, catName, sourceName, title, createTime, createUserId, updateTime, updateUserId, content);
BasicColumn[] selectList = BasicColumn.columnList(id, catId, catName, sourceName, title, readCount, createTime, createUserId, updateTime, updateUserId, content);

@Generated("org.mybatis.generator.api.MyBatisGenerator")
@SelectProvider(type=SqlProviderAdapter.class, method="select")
Expand Down Expand Up @@ -66,6 +66,7 @@ public interface NewsMapper {
@Result(column="cat_name", property="catName", jdbcType=JdbcType.VARCHAR),
@Result(column="source_name", property="sourceName", jdbcType=JdbcType.VARCHAR),
@Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
@Result(column="read_count", property="readCount", jdbcType=JdbcType.BIGINT),
@Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
@Result(column="create_user_id", property="createUserId", jdbcType=JdbcType.BIGINT),
@Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
Expand Down Expand Up @@ -103,6 +104,7 @@ default int insert(News record) {
.map(catName).toProperty("catName")
.map(sourceName).toProperty("sourceName")
.map(title).toProperty("title")
.map(readCount).toProperty("readCount")
.map(createTime).toProperty("createTime")
.map(createUserId).toProperty("createUserId")
.map(updateTime).toProperty("updateTime")
Expand All @@ -119,6 +121,7 @@ default int insertMultiple(Collection<News> records) {
.map(catName).toProperty("catName")
.map(sourceName).toProperty("sourceName")
.map(title).toProperty("title")
.map(readCount).toProperty("readCount")
.map(createTime).toProperty("createTime")
.map(createUserId).toProperty("createUserId")
.map(updateTime).toProperty("updateTime")
Expand All @@ -135,6 +138,7 @@ default int insertSelective(News record) {
.map(catName).toPropertyWhenPresent("catName", record::getCatName)
.map(sourceName).toPropertyWhenPresent("sourceName", record::getSourceName)
.map(title).toPropertyWhenPresent("title", record::getTitle)
.map(readCount).toPropertyWhenPresent("readCount", record::getReadCount)
.map(createTime).toPropertyWhenPresent("createTime", record::getCreateTime)
.map(createUserId).toPropertyWhenPresent("createUserId", record::getCreateUserId)
.map(updateTime).toPropertyWhenPresent("updateTime", record::getUpdateTime)
Expand Down Expand Up @@ -177,6 +181,7 @@ static UpdateDSL<UpdateModel> updateAllColumns(News record, UpdateDSL<UpdateMode
.set(catName).equalTo(record::getCatName)
.set(sourceName).equalTo(record::getSourceName)
.set(title).equalTo(record::getTitle)
.set(readCount).equalTo(record::getReadCount)
.set(createTime).equalTo(record::getCreateTime)
.set(createUserId).equalTo(record::getCreateUserId)
.set(updateTime).equalTo(record::getUpdateTime)
Expand All @@ -191,6 +196,7 @@ static UpdateDSL<UpdateModel> updateSelectiveColumns(News record, UpdateDSL<Upda
.set(catName).equalToWhenPresent(record::getCatName)
.set(sourceName).equalToWhenPresent(record::getSourceName)
.set(title).equalToWhenPresent(record::getTitle)
.set(readCount).equalToWhenPresent(record::getReadCount)
.set(createTime).equalToWhenPresent(record::getCreateTime)
.set(createUserId).equalToWhenPresent(record::getCreateUserId)
.set(updateTime).equalToWhenPresent(record::getUpdateTime)
Expand All @@ -205,6 +211,7 @@ default int updateByPrimaryKey(News record) {
.set(catName).equalTo(record::getCatName)
.set(sourceName).equalTo(record::getSourceName)
.set(title).equalTo(record::getTitle)
.set(readCount).equalTo(record::getReadCount)
.set(createTime).equalTo(record::getCreateTime)
.set(createUserId).equalTo(record::getCreateUserId)
.set(updateTime).equalTo(record::getUpdateTime)
Expand All @@ -221,6 +228,7 @@ default int updateByPrimaryKeySelective(News record) {
.set(catName).equalToWhenPresent(record::getCatName)
.set(sourceName).equalToWhenPresent(record::getSourceName)
.set(title).equalToWhenPresent(record::getTitle)
.set(readCount).equalToWhenPresent(record::getReadCount)
.set(createTime).equalToWhenPresent(record::getCreateTime)
.set(createUserId).equalToWhenPresent(record::getCreateUserId)
.set(updateTime).equalToWhenPresent(record::getUpdateTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</javaClientGenerator>

<!--生成全部表tableName设为%-->
<table tableName="book"/>
<table tableName="news"/>

<!-- 指定数据库表 -->
<!--<table schema="jly" tableName="job_position" domainObjectName="JobPositionTest"/>-->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.java2nb.novel.controller;

import com.github.pagehelper.PageInfo;
import com.java2nb.novel.core.bean.PageBean;
import com.java2nb.novel.core.bean.ResultBean;
import com.java2nb.novel.entity.News;
import com.java2nb.novel.service.NewsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @author 11797
*/
Expand All @@ -22,18 +26,27 @@ public class NewsController {
* 查询首页新闻
* */
@GetMapping("listIndexNews")
public ResultBean listIndexNews(){
public ResultBean<List<News>> listIndexNews(){
return ResultBean.ok(newsService.listIndexNews());
}

/**
* 分页查询新闻列表
* */
@GetMapping("listByPage")
public ResultBean listByPage(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "5") int pageSize){
public ResultBean<PageBean<News>> listByPage(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "5") int pageSize){
return ResultBean.ok(newsService.listByPage(page,pageSize));
}

/**
* 增加新闻阅读量
* */
@PostMapping("addReadCount")
public ResultBean<Void> addReadCount(@RequestParam(value = "newsId") Integer newsId){
newsService.addReadCount(newsId);
return ResultBean.ok();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.java2nb.novel.mapper;

import com.java2nb.novel.entity.Book;
import com.java2nb.novel.vo.BookSpVO;
import com.java2nb.novel.vo.BookVO;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
* @author Administrator
*/
public interface FrontNewsMapper extends NewsMapper {

/**
* 增加新闻阅读量
* @param newsId 新闻ID
* */
void addReadCount(@Param("newsId") Integer newsId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public interface BookService {
/**
* 增加点击次数
* @param bookId 书籍ID
* @param visitCount*/
* @param visitCount 点击量
* */
void addVisitCount(Long bookId, Integer visitCount);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public interface NewsService {
* @return 新闻分页数据
* */
PageBean<News> listByPage(int page, int pageSize);

/**
* 增加新闻阅读量
* @param newsId 新闻ID
* */
void addReadCount(Integer newsId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ public void addBook(Book book, Long authorId, String penName) {
//该作者发布过此书名的小说
throw new BusinessException(ResponseStatus.BOOKNAME_EXISTS);
}
;
book.setAuthorName(penName);
book.setAuthorId(authorId);
book.setVisitCount(0L);
Expand Down Expand Up @@ -566,7 +565,7 @@ public void addBookContent(Long bookId, String indexName, String content, Byte i
.render(RenderingStrategies.MYBATIS3));

//计算价格
int bookPrice = new BigDecimal(wordCount).divide(bookPriceConfig.getWordCount()).multiply(bookPriceConfig.getValue()).intValue();
int bookPrice = new BigDecimal(wordCount).multiply(bookPriceConfig.getValue()).divide(bookPriceConfig.getWordCount(),0,BigDecimal.ROUND_DOWN).intValue();

//更新小说目录表
int indexNum = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.pagehelper.PageHelper;
import com.java2nb.novel.core.bean.PageBean;
import com.java2nb.novel.core.utils.BeanUtil;
import com.java2nb.novel.mapper.FrontNewsMapper;
import com.java2nb.novel.service.NewsService;
import com.java2nb.novel.core.cache.CacheKey;
import com.java2nb.novel.core.cache.CacheService;
Expand All @@ -27,7 +28,7 @@
@RequiredArgsConstructor
public class NewsServiceImpl implements NewsService {

private final NewsMapper newsMapper;
private final FrontNewsMapper newsMapper;

private final CacheService cacheService;

Expand Down Expand Up @@ -72,4 +73,9 @@ public PageBean<News> listByPage(int page, int pageSize) {
pageBean.setList(BeanUtil.copyList(news,NewsVO.class));
return pageBean;
}

@Override
public void addReadCount(Integer newsId) {
newsMapper.addReadCount(newsId);
}
}
9 changes: 9 additions & 0 deletions novel-front/src/main/resources/mybatis/mapping/NewsMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.java2nb.novel.mapper.FrontNewsMapper">
<update id="addReadCount">
update news set read_count = read_count + 1
where id = #{newsId}
</update>
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<div th:replace="common/top :: top('')">
</div>
<input type="hidden" id="newsId" th:value="${news.id}"/>
<div class="main box_center cf">
<div class="newsMain cf">
<div class="nav_sub">
Expand All @@ -19,7 +20,7 @@
<div class="news_title">
<h2 th:utext="${news.title}"></h2>
<!--while [[...]] corresponds to th:text (i.e. result will be HTML-escaped), [(...)] corresponds to th:utext-->
<p class="from">来源:[[${news.sourceName}]] <span class="time" th:text="'时间:'+${#dates.format(news.createTime, 'yy-MM-dd')}"></span> <span class="time" th:text="'点击:'+'888'"></span></p>
<p class="from">来源:[[${news.sourceName}]] <span class="time" th:text="'时间:'+${#dates.format(news.createTime, 'yy-MM-dd')}"></span> <span class="time" th:text="'阅读量:'+${news.readCount}"></span></p>
</div>
<div class="news_info" th:utext="${news.content}">
</div>
Expand All @@ -33,4 +34,8 @@ <h2 th:utext="${news.title}"></h2>
</body>
<div th:replace="common/js :: js"></div>
<script src="/javascript/bookdetail.js" type="text/javascript"></script>
<script type="text/javascript">
$.post("/news/addReadCount", {"newsId": $("#newsId").val()}, function () {
});
</script>
</html>

0 comments on commit 1cffbae

Please sign in to comment.