//创建归档类
@Controller
public class IndexController {
@Autowired
private BlogService blogService;
@Autowired
private AlbumService albumService;
@RequestMapping("/findAllArchives")
public ModelAndView archives(Model model) {
//准备数据:博客 相册
List<Blog> blogList = blogService.findAllBlogOrderByCreateTime();
List<Album> albumList = albumService.findAllAlbumOrderByCreateTime();
return new ModelAndView("/archives");
}
}
@Service
public interface BlogService {
...
List<Blog> findAllBlogOrderByCreateTime();
}
@Component
public class BlogServiceImpl implements BlogService{
@Autowired
private BlogRepository blogRepository;
...
@Override
public List<Blog> findAllBlogOrderByCreateTime() {
// TODO Auto-generated method stub
return blogRepository.findAllBlogOrderByCreateTime();
}
}
public interface BlogRepository extends CrudRepository<Blog, Long>{
@Query(value = "select * from blog order by create_time desc",nativeQuery = true)
List<Blog> findAllBlogOrderByCreateTime();
}
AlbumService、AlbumServiceImpl、AlbumRepository的编写类似。
@RequestMapping("/findAllArchives")
public ModelAndView archives(Model model) {
//准备数据:博客 相册
List<Blog> blogList = blogService.findAllBlogOrderByCreateTime();
List<Album> albumList = albumService.findAllAlbumOrderByCreateTime();
//预处理数据
preList(blogList, albumList);
return new ModelAndView("/archives");
}
//预处理数据
private void preList(List<Blog> blogList,List<Album> albumList) {
//yyyy-MM-dd HH:mm
//MM-dd
for (Blog blog : blogList) {
blog.setCreateTime(blog.getCreateTime().substring(5, 10));
}
for (Album album : albumList) {
album.setCreateTime(album.getCreateTime().substring(5, 10));
}
}
@RequestMapping("/findAllArchives")
public ModelAndView archives(Model model) {
//准备数据:博客 相册
List<Blog> blogList = blogService.findAllBlogOrderByCreateTime();
List<Album> albumList = albumService.findAllAlbumOrderByCreateTime();
//准备时间线
List<String> blogTimeLine = blogService.findBlogTimeLine();
List<String> albumTimeLine = albumService.findAlbumTimeLine();
//预处理数据
preList(blogList, albumList);
return new ModelAndView("/archives");
}
@Service
public interface BlogService {
...
List<String> findBlogTimeLine();
}
@Component
public class BlogServiceImpl implements BlogService{
@Autowired
private BlogRepository blogRepository;
...
@Override
public List<String> findBlogTimeLine() {
// TODO Auto-generated method stub
return blogRepository.findBlogTimeLine();
}
}
public interface BlogRepository extends CrudRepository<Blog, Long>{
...
@Query(value = "select create_time from blog order by create_time desc",nativeQuery = true)
List<String> findBlogTimeLine();
}
AlbumService、AlbumServiceImpl、AlbumRepository的编写类似。
//创建归档类
@Controller
public class IndexController {
@Autowired
private BlogService blogService;
@Autowired
private AlbumService albumService;
@RequestMapping("/findAllArchives")
public ModelAndView archives(Model model) {
//准备数据:博客 相册
List<Blog> blogList = blogService.findAllBlogOrderByCreateTime();
List<Album> albumList = albumService.findAllAlbumOrderByCreateTime();
//准备时间线
List<String> blogTimeLine = blogService.findBlogTimeLine();
List<String> albumTimeLine = albumService.findAlbumTimeLine();
//预处理数据
preList(blogList, albumList);
//预处理时间线
blogTimeLine = preTimeLine(blogTimeLine);
albumTimeLine = preTimeLine(albumTimeLine);
return new ModelAndView("/archives");
}
//预处理数据
...
//预处理时间线
private List<String> preTimeLine(List<String> list) {
List<String> targetList = new ArrayList<String>();
//yyyy-MM-dd HH-mm
//yyyy-MM
for (String str : list) {
targetList.add(str.substring(0, 7));
}
return targetList;
}
}
//时间线归并排序
private List<String> getTimeLine(List<String> blogTimeLine, List<String> albumTimeLine) {
List<String> target = new ArrayList<String>();
//i用于操作博客“指针”,j用于操作相册“指针”,k用于操作目标数组“指针”
int i=0,j=0,k=0;
while (i<blogTimeLine.size() || j<albumTimeLine.size()) {
//到达边界
if(i>=blogTimeLine.size()) {
k = addString(target, albumTimeLine.get(j), k);
j++;
continue;
}
if (j>=albumTimeLine.size()) {
k = addString(target, blogTimeLine.get(i), k);
i++;
continue;
}
if(blogTimeLine.get(i).equals(albumTimeLine.get(j))) {
k = addString(target, blogTimeLine.get(i), k);
//target.add(blogTimeLine.get(i));
i++;
j++;
//k++;
}else if (blogTimeLine.get(i).compareTo(albumTimeLine.get(j)) > 0) {
k = addString(target, blogTimeLine.get(i), k);
//target.add(blogTimeLine.get(i));
//k++;
i++;
}else {
k = addString(target, albumTimeLine.get(j), k);
//target.add(albumTimeLine.get(j));
//k++;
j++;
}
}
return target;
}
//归并添加,数据去重
private void addString(List<String> targetList,String timeLineStr,int k) {
if(k-1 == -1) {
targetList.add(timeLineStr);
return;
}else if(targetList.get(k-1).equals(timeLineStr)) {
return;
}else {
targetList.add(timeLineStr);
return;
}
}
//预处理时间线
blogTimeLine = preTimeLine(blogTimeLine);
albumTimeLine = preTimeLine(albumTimeLine);
//时间线归并排序
List<String> timeLine = getTimeLine(blogTimeLine,albumTimeLine);
//测试时间线代码
for (String str : timeLine) {
System.out.println(str);
}
//修改为0到10
//预处理数据
private void preList(List<Blog> blogList,List<Album> albumList) {
//yyyy-MM-dd HH:mm
//yyyy-MM-dd
for (Blog blog : blogList) {
blog.setCreateTime(blog.getCreateTime().substring(0, 10));
}
for (Album album : albumList) {
album.setCreateTime(album.getCreateTime().substring(0, 10));
}
}
// public ModelAndView archives(Model model)放置显示数据
model.addAttribute("timeLine",timeLine);
model.addAttribute("blogList",blogList);
model.addAttribute("albumList",albumList);
return new ModelAndView("/archives","indexModel",model);
前端显示
<div id="archives">
<div th:each="time:${timeLine}" class='archive-title' id='arti-2016-8'>
<h3 th:text="${time}">2016-8h3>
<div class='archives archives-8' data-date='2016-8'>
<div class="brick" th:each="blog:${blogList}" th:if="${#strings.substring(blog.createTime,0,7) eq time}">
<a th:href="@{/findBlogById(id=${blog.id})}">
<span class="time" th:text="${#strings.substring(blog.createTime,5,10)}">8-15span>
<span th:text="${blog.title}">span>
<span th:text="|(${blog.viewNumber})|">(10)span>
a>
div>
<div class="brick" th:each="album:${albumList}" th:if="${#strings.substring(album.createTime,0,7) eq time}">
<a th:href="@{/albumDetail(id=${album.id})}">
<span class="time" th:text="${#strings.substring(album.createTime,5,10)}">8-15span>
<span th:text="${album.title}">span>
<span >( 相册 )span>
a>
div>
div>
div>
div>
// model.addAttribute("blogList",blogList);
// model.addAttribute("albumList",albumList);
//次时间线归并排序
List<TimeLineObject> timeLineObjectList = getTimeLineObject(blogList,albumList);
model.addAttribute("timeLineObjectList", timeLineObjectList);
//次时间线归并排序
private List<TimeLineObject> getTimeLineObject(List<Blog> blogList, List<Album> albumList) {
List<TimeLineObject> target = new ArrayList<TimeLineObject>();
//i用于操作博客“指针”,j用于操作相册“指针”,k用于操作目标数组“指针”
int i=0,j=0,k=0;
while (i<blogList.size() || j<albumList.size()) {
//到达边界
if(i>=blogList.size()) {
k = addTimeLineObject(target, albumList.get(j), k);
j++;
continue;
}
if (j>=albumList.size()) {
k = addTimeLineObject(target, blogList.get(i), k);
i++;
continue;
}
if(blogList.get(i).getCreateTime().equals(albumList.get(j).getCreateTime())) {
k = addTimeLineObject(target, blogList.get(i), k);
k = addTimeLineObject(target, albumList.get(j), k);
//target.add(blogTimeLine.get(i));
i++;
j++;
//k++;
}else if (blogList.get(i).getCreateTime().compareTo(albumList.get(j).getCreateTime()) > 0) {
k = addTimeLineObject(target, blogList.get(i), k);
//target.add(blogTimeLine.get(i));
//k++;
i++;
}else {
k = addTimeLineObject(target, albumList.get(j), k);
//target.add(albumTimeLine.get(j));
//k++;
j++;
}
}
return target;
}
private int addTimeLineObject(List<TimeLineObject> target, Blog blog, int k) {
TimeLineObject object = new TimeLineObject();
object.setCreateTime(blog.getCreateTime());
object.setTitle(blog.getTitle());
object.setViewNumber(blog.getViewNumber()+"");
object.setHref("/findBlogById?id="+blog.getId());
target.add(object);
k++;
return k;
}
private int addTimeLineObject(List<TimeLineObject> target, Album album, int k) {
TimeLineObject object = new TimeLineObject();
object.setCreateTime(album.getCreateTime());
object.setTitle(album.getTitle());
object.setViewNumber("相册");
object.setHref("/albumDetail?id="+album.getId());
target.add(object);
k++;
return k;
}
修改archives.html:
<div class="brick" th:each="timeLineObject:${timeLineObjectList}" th:if="${#strings.substring(timeLineObject.createTime,0,7) eq time}">
<a th:href="@{${timeLineObject.href}}">
<span class="time" th:text="${#strings.substring(timeLineObject.createTime,5,10)}">8-15span>
<span th:text="${timeLineObject.title}">span>
<span th:text="|(${timeLineObject.viewNumber})|">(10)span>
a>
div>