public interface NewsRepository extends JpaRepository<News,Long>,JpaSpecificationExecutor{}
让NewsRepository继承JpaSpecificationExecutor就可以调用条件分页查询了
public Page<News> getAllNewsCriteria(int page, int size){
Pageable pageable = new PageRequest(page, size,new Sort(Sort.Direction.DESC,"id"));
Specification<News> spec = new Specification<News>() {
public Predicate toPredicate(Root<News> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate p = cb.isNotNull(root.get("type"));//root相当于sql中的from
query.where(p);//添加条件
return query.getRestriction();
}
};
return newsRepository.findAll(spec,pageable);
}
这样就查出了类型不为空的新闻,因为在我的设计里有类型了便代表审核过了。
Criteria具体参考http://blog.csdn.net/han1196639488/article/details/54909100
修改NewsController
/**
* 初始获取20条新闻
* @param
* @return
*/
@RequestMapping(value = "",method = RequestMethod.GET)
public String showNews(ModelMap map) {
Page<News> newsPage = newsService.getAllNewsCriteria(0,20);
List<News> newsList = newsPage.getContent();
map.addAttribute("News",newsList);
int totalPages = newsPage.getTotalPages()-1;
map.addAttribute("totalPages",totalPages);
return "index";
}
/**
* 加载更多新闻
* @param pageNum 新闻页
* @return
*/
@RequestMapping(value = "/loadmore")
@ResponseBody
public List<News> loadmore(@RequestParam(value = "pageNum",required = false) String pageNum){
int pageNumber = Integer.parseInt(pageNum);
Page<News> newsPage = newsService.getAllNewsCriteria(pageNumber,20);
List<News> newsList = newsPage.getContent();
return newsList;
}
html
<body align="center">
<div th:include="header"></div>
<div id="news">
<div class="news" th:each="n : ${News}">
<div class="line">|</div>
<a th:href="@{'/'+${n.id}}" th:text="${n.title}">标题</a>
<div class="time" th:text="${#dates.format(n.publicationdate,'yyyy-MM-dd HH:mm')}">时间</div>
</div>
</div>
<input id="pageNum" type="hidden" value="1">
<button id="loadmore" onclick="loadmore()">加载更多</button>
<div th:include="footer"></div>
</body>
掩藏input记录页数。
jQuery
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script type="text/javascript" th:inline="javascript">
/*<![CDATA[*/
function formatDate(time) {
var year=time.getFullYear();
var month=time.getMonth()+1;
var date=time.getDate();
var hour=time.getHours();
var minute=time.getMinutes();
var second=time.getSeconds();
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
}
function loadmore() {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
var pageNum = document.getElementById("pageNum");
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var txt = "";
var newsList = JSON.parse(xmlhttp.responseText);
for(var i=0;i<newsList.length;i++){
txt += "<div class=\"news\">" +
"<div class=\"line\">|</div>";
txt += "<a href=\"/" + newsList[i].id + "\">" + newsList[i].title + "";
txt += "<div class=\"time\">" + formatDate(new Date(newsList[i].publicationdate)) + "</div>" +
"</div>";
}
$("#pageNum").before(txt);
if(pageNum.value<[[${totalPages}]]){
pageNum.value = parseInt(pageNum.value) + 1;
}else{
$("#loadmore").remove();
$("#pageNum").before("没有更多了");
}
}
}
xmlhttp.open("GET","/loadmore?pageNum="+pageNum.value,true);
xmlhttp.send();
}
/*]]>*/
</script>
在head里面加上<base target="_blank" />
让所有超链接在新页面打开,用户体验更好。