在这一讲中有几个很重要的地方需要注意:
1.在查询帖子的时候需要把帖子类型id带到帖子类型表中把类型查询出来
2.在字帖子查询语句中用limt限制查询那个阶段的帖子
3.在界面显示的时候需要用到字符串截取标签和日期时间标签
2.代码实现
2.1创建帖子类:
package com.open1111.entity;
import java.util.Date;
/**
* 帖子实体类
* @author user
*
*/
public class Article {
private Integer id; // 编号
private String title; // 标题
private Date publishDate; // 发布日期
private String content; // 内容
private String summary; // 摘要
private String titleColor; // 标题颜色 默认黑色
private Integer click=0; // 阅读次数
private Integer isRecommend=0; // 是否推荐帖子 1 是 0 否
private Integer isSlide=0; // 是否是幻灯帖子 1 是 0 否
private ArcType arcType; // 帖子类型
private String keyWords; // 关键字
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getTitleColor() {
return titleColor;
}
public void setTitleColor(String titleColor) {
this.titleColor = titleColor;
}
public Integer getClick() {
return click;
}
public void setClick(Integer click) {
this.click = click;
}
public Integer getIsRecommend() {
return isRecommend;
}
public void setIsRecommend(Integer isRecommend) {
this.isRecommend = isRecommend;
}
public Integer getIsSlide() {
return isSlide;
}
public void setIsSlide(Integer isSlide) {
this.isSlide = isSlide;
}
public ArcType getArcType() {
return arcType;
}
public void setArcType(ArcType arcType) {
this.arcType = arcType;
}
public String getKeyWords() {
return keyWords;
}
public void setKeyWords(String keyWords) {
this.keyWords = keyWords;
}
}
2.2:创建帖子到对应的mapper
<?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.open1111.dao.ArticleDao">
<resultMap type="Article" id="ArticleResult">
<result property="id" column="id"/>
<result property="title" column="title"/>
<result property="publishDate" column="publishDate"/>
<result property="content" column="content"/>
<result property="summary" column="summary"/>
<result property="titleColor" column="titleColor"/>
<result property="click" column="click"/>
<result property="isRecommend" column="isRecommend"/>
<result property="isSlide" column="isSlide"/>
<result property="keyWords" column="keyWords"/>
<association property="arcType" column="typeId" select="com.open1111.dao.ArcTypeDao.findById"></association>
</resultMap>
<select id="getNewest" resultMap="ArticleResult">
select * from t_article order by publishDate desc limit 0,7
</select>
</mapper>
2.3:创建帖子的dao:
package com.open1111.dao;
import java.util.List;
import com.open1111.entity.Article;
/**
* 帖子Dao接口
* @author user
*
*/
public interface ArticleDao {
/**
* 获取最新的7条帖子
* @return
*/
public List<Article> getNewest();
}
2.4:由于帖子需要查询帖子类型所在帖子类型mapper中添加一个根据id查询帖子类型的方法:
<?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.open1111.dao.ArcTypeDao">
<resultMap type="ArcType" id="ArcTypeResult">
<result property="id" column="id"/>
<result property="typeName" column="typeName"/>
<result property="sortNo" column="sortNo"/>
<result property="keywords" column="keywords"/>
<result property="description" column="description"/>
</resultMap>
<select id="list" parameterType="Map" resultMap="ArcTypeResult">
select * from t_arcType
order by sortNo asc
</select>
<select id="findById" parameterType="Integer" resultMap="ArcTypeResult">
select * from t_arcType where id=#{id}
</select>
</mapper>
2.5:更改mapper时候更改对应的dao
package com.open1111.dao;
import java.util.List;
import java.util.Map;
import com.open1111.entity.ArcType;
/**
* 帖子类别Dao接口
* @author user
*
*/
public interface ArcTypeDao {
/**
* 根据条件分页查询帖子类别集合
* @param map
* @return
*/
public List<ArcType> list(Map<String,Object> map);
/**
* 根据id查询实体
* @param id
* @return
*/
public ArcType findById(Integer id);
}
2.6创建帖子service:
package com.open1111.service;
import java.util.List;
import java.util.Map;
import com.open1111.entity.ArcType;
/**
* 帖子类别Service接口
* @author user
*
*/
public interface ArcTypeService {
/**
* 根据条件分页查询帖子类别集合
* @param map
* @return
*/
public List<ArcType> list(Map<String,Object> map);
}
2.7创建帖子的serviceImpl
package com.open1111.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.open1111.dao.ArticleDao;
import com.open1111.entity.Article;
import com.open1111.service.ArticleService;
/**
* 帖子Service实现类
* @author user
*
*/
@Service("articleService")
public class ArticleServiceImpl implements ArticleService{
@Resource
private ArticleDao articleDao;
public List<Article> getNewest() {
return articleDao.getNewest();
}
}
2.8:把查询到的数据放到application中
package com.open1111.service.impl;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import com.open1111.entity.ArcType;
import com.open1111.entity.Article;
import com.open1111.entity.Link;
import com.open1111.service.ArcTypeService;
import com.open1111.service.ArticleService;
import com.open1111.service.LinkService;
/**
* 初始化组件
* @author user
*
*/
@Component("initComponet")
public class InitComponet implements ApplicationContextAware,ServletContextListener{
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
this.applicationContext=applicationContext;
System.out.println(this);
}
/**
* 刷新application缓存方法
* @param application
*/
public void refreshSystem(ServletContext application){
LinkService linkService=(LinkService) applicationContext.getBean("linkService");
List<Link> linkList=linkService.list(null);
application.setAttribute("linkList", linkList);
ArcTypeService arcTypeService=(ArcTypeService) applicationContext.getBean("arcTypeService");
List<ArcType> arcTypeList=arcTypeService.list(null);
application.setAttribute("arcTypeList", arcTypeList);
ArticleService articleService=(ArticleService) applicationContext.getBean("articleService");
List<Article> newestArticleList=articleService.getNewest(); // 获取最新7条帖子
application.setAttribute("newestArticleList", newestArticleList);
}
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
ServletContext application=sce.getServletContext();
refreshSystem(application);
}
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
}
2.9:前台循环出帖子:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Open1111官方网站 -南通小锋网络科技有限公司</title>
<META NAME="Author" CONTENT="Java1234_小锋老师">
<meta name="keywords"
content="Java内容管理系统,JavaCMS,Java开源,Java爬虫,JavaSEO,Java CMS" />
<meta name="description"
content="Open1111专注于研发Java建站,Java爬虫,JavaSEO教程.传授广大学员用Java技术来实现互联网资源的搬运,整合汇聚互联网资源建站,顺便获取一些搬运费补贴家用." />
<link rel="stylesheet"
href="${pageContext.request.contextPath}/static/css/open1111.css">
<script src="${pageContext.request.contextPath}/static/js/jQuery.js"></script>
</head>
<body>
<jsp:include page="/common/head.jsp"></jsp:include>
<jsp:include page="/common/menu.jsp"></jsp:include>
<div class="content">
<div class="w960">
<div class="left">
<DIV style="width: 330px; height: 228px;" class="tuhuo">
<A href="" target="_blank"><IMG
style="width: 330px; height: 208px;" id="fou_img" src=""></A> <A
href="#"> <IMG style="display: none;" class="tu_img"
src="${pageContext.request.contextPath}/static/userImages/1.jpg"
width="330" height="208" />
</A> <A href="#"> <IMG style="display: none;" class="tu_img"
src="${pageContext.request.contextPath}/static/userImages/2.jpg"
width="330" height="208" />
</A> <A href="#"> <IMG style="display: none;" class="tu_img"
src="${pageContext.request.contextPath}/static/userImages/3.jpg"
width="330" height="208" />
</A> <A href="#"> <IMG style="display: none;" class="tu_img"
src="${pageContext.request.contextPath}/static/userImages/4.jpg"
width="330" height="208" />
</A> <A href="#"> <IMG style="display: none;" class="tu_img"
src="${pageContext.request.contextPath}/static/userImages/5.jpg"
width="330" height="208" />
</A>
<P style="height: 20px;" class="tc">
<A href="#" target="_blank" title="1">1</A>
</P>
<P style="height: 20px;" class="tc">
<A href="#" target="_blank" title="2">2</A>
</P>
<P style="height: 20px;" class="tc">
<A href="#" target="_blank" title="3">3</A>
</P>
<P style="height: 20px;" class="tc">
<A href="#" target="_blank" title="4">4</A>
</P>
<P style="height: 20px;" class="tc">
<A href="#" target="_blank" title="5">5</A>
</P>
<UL>
<LI class="fouce">1</LI>
<LI>2</LI>
<LI>3</LI>
<LI>4</LI>
<LI>5</LI>
</UL>
</DIV>
</div>
<div class="middle">
<div class="data_list">
<div class="dataHeader">最近更新</div>
<div class="datas">
<ul>
<c:forEach var="article" items="${newestArticleList }">
<li><a target="_blank" href="${pageContext.request.contextPath}/article/${article.id}.html"
title="${article.title }">[<fmt:formatDate value="${article.publishDate }" pattern="MM-dd"/>] <font
color="${article.titleColor }">${fn:substring(article.title,0,20) }</font></a></li>
</c:forEach>
</ul>
</div>
</div>
</div>
<div class="right">
<div class="data_list">
<div class="dataHeader">站长推荐</div>
<div class="datas">
<ul>
<li><a target="_blank" href="/article/152.html"
title="做活动,领取Java爬虫-Java包下载网源码"><font color="#FF0000">做活动,领取Java爬虫-Jav</font></a></li>
<li><a target="_blank" href="/article/86.html"
title="做活动,领取百度云搜索引擎V2.0源码"><font color="#FF0000">做活动,领取百度云搜索引擎V2.</font></a></li>
<li><a target="_blank" href="/article/73.html" title="关于我们"><font
color="#FF0000">关于我们</font></a></li>
<li><a target="_blank" href="/article/77.html"
title="如何发布Web项目到互联网"><font color="#FF0000">如何发布Web项目到互联网</font></a></li>
<li><a target="_blank" href="/article/74.html"
title="广告联盟推荐-七七联盟"><font color="#FF0000">广告联盟推荐-七七联盟</font></a></li>
<li><a target="_blank" href="/article/68.html"
title="Java HelloWorld实现及Java运行原理介绍"><font color="#FF0000">Java
HelloWorld实</font></a></li>
<li><a target="_blank" href="/article/67.html"
title="Java运行环境搭建之Jdk配置"><font color="#FF0000">Java运行环境搭建之Jdk配置</font></a></li>
</ul>
</div>
</div>
</div>
<div class="w960 article_row">
<div class="data_list article_list">
<div class="dataHeader">
Java基础<span class="more"><a href="/arcType/1.html">更多...</a></span>
</div>
<div class="datas">
<ul>
<li>[11-05] <a href="/article/72.html"
title="Java基本数据类型-数据类型分类"><font color="">Java基本数据类型-数据类型分类</font></a></li>
<li>[11-05] <a href="/article/71.html"
title="Java中的关键字"><font color="">Java中的关键字</font></a></li>
<li>[11-05] <a href="/article/70.html"
title="Java标识符命名"><font color="">Java标识符命名</font></a></li>
<li>[11-05] <a href="/article/69.html"
title="java注释"><font color="">java注释</font></a></li>
<li>[11-05] <a href="/article/68.html"
title="Java HelloWorld实现及Java运行原理介绍"><font color="#FF0000">Java
HelloWorld实现及</font></a></li>
<li>[11-05] <a href="/article/67.html"
title="Java运行环境搭建之Jdk配置"><font color="#FF0000">Java运行环境搭建之Jdk配置</font></a></li>
<li>[11-05] <a href="/article/66.html"
title="Java简介"><font color="">Java简介</font></a></li>
<li>[11-05] <a href="/article/65.html"
title="Java HelloWorld实现"><font color="">Java
HelloWorld实现</font></a></li>
</ul>
</div>
</div>
<div class="data_list article_list" style="margin-left: 12px">
<div class="dataHeader">
网页基础<span class="more"><a href="/arcType/2.html">更多...</a></span>
</div>
<div class="datas">
<ul>
<li>[11-28] <a href="/article/143.html"
title="css 属性选择器"><font color="">css 属性选择器</font></a></li>
<li>[11-28] <a href="/article/142.html"
title="css 类选择器"><font color="">css 类选择器</font></a></li>
<li>[11-28] <a href="/article/141.html"
title="css id 选择器"><font color="">css id 选择器</font></a></li>
<li>[11-08] <a href="/article/82.html"
title="HTML文本排版"><font color="">HTML文本排版</font></a></li>
<li>[11-08] <a href="/article/81.html"
title="HTML入门程序"><font color="">HTML入门程序</font></a></li>
<li>[11-08] <a href="/article/80.html"
title="Web基础"><font color="">Web基础</font></a></li>
<li>[11-06] <a href="/article/76.html"
title="基本的 HTML 标签"><font color="">基本的 HTML 标签</font></a></li>
<li>[11-06] <a href="/article/75.html"
title="HTML 简介"><font color="">HTML 简介</font></a></li>
</ul>
</div>
</div>
<div class="data_list article_list" style="margin-left: 12px">
<div class="dataHeader">
Java建站<span class="more"><a href="/arcType/4.html">更多...</a></span>
</div>
<div class="datas">
<ul>
<li>[01-24] <a href="/article/86.html"
title="做活动,领取百度云搜索引擎V2.0源码"><font color="#FF0000">做活动,领取百度云搜索引擎V2.0源</font></a></li>
<li>[01-03] <a href="/article/151.html"
title="Tomcat OutOfMemoryError PermGen space 的解决办法"><font
color="">Tomcat OutOfMemory</font></a></li>
<li>[12-04] <a href="/article/147.html"
title="http content-type常见文件格式类型"><font color="">http
content-type常</font></a></li>
<li>[12-04] <a href="/article/145.html"
title="主机"><font color="">主机</font></a></li>
<li>[12-03] <a href="/article/144.html"
title="域名"><font color="">域名</font></a></li>
<li>[11-09] <a href="/article/85.html"
title="解决Url带中文参数乱码问题"><font color="">解决Url带中文参数乱码问题</font></a></li>
<li>[11-09] <a href="/article/84.html"
title="Tomcat多域名配置"><font color="">Tomcat多域名配置</font></a></li>
<li>[11-06] <a href="/article/77.html"
title="如何发布Web项目到互联网"><font color="#FF0000">如何发布Web项目到互联网</font></a></li>
</ul>
</div>
</div>
</div>
<div class="w960 article_row">
<div class="data_list article_list">
<div class="dataHeader">
Java爬虫<span class="more"><a href="/arcType/5.html">更多...</a></span>
</div>
<div class="datas">
<ul>
<li>[01-24] <a href="/article/152.html"
title="做活动,领取Java爬虫-Java包下载网源码"><font color="#FF0000">做活动,领取Java爬虫-Java包</font></a></li>
<li>[12-04] <a href="/article/146.html"
title="透明代理、匿名代理、混淆代理、高匿代理有什么区别?"><font color="">透明代理、匿名代理、混淆代理、高匿代</font></a></li>
<li>[11-21] <a href="/article/140.html"
title="HttpClient HTTP缓存"><font color="">HttpClient
HTTP缓存</font></a></li>
<li>[11-21] <a href="/article/139.html"
title="HttpClient 响应处理"><font color="">HttpClient
响应处理</font></a></li>
<li>[11-21] <a href="/article/138.html"
title="Easy to use facade API"><font color="">Easy
to use facade</font></a></li>
<li>[11-20] <a href="/article/137.html"
title="HttpClient SPNEGO/Kerberos认证"><font color="">HttpClient
SPNEGO/</font></a></li>
<li>[11-20] <a href="/article/136.html"
title="HttpClient NTLM连接持久性"><font color="">HttpClient
NTLM连接持</font></a></li>
<li>[11-20] <a href="/article/135.html"
title="HttpClient NTLM认证"><font color="">HttpClient
NTLM认证</font></a></li>
</ul>
</div>
</div>
<div class="data_list article_list" style="margin-left: 12px">
<div class="dataHeader">
网站SEO<span class="more"><a href="/arcType/6.html">更多...</a></span>
</div>
<div class="datas">
<ul>
<li>[11-08] <a href="/article/83.html"
title="搜索引擎工作原理大体过程"><font color="">搜索引擎工作原理大体过程</font></a></li>
<li>[11-07] <a href="/article/79.html"
title="为什么要做SEO"><font color="">为什么要做SEO</font></a></li>
<li>[11-07] <a href="/article/78.html"
title="什么是SEO"><font color="">什么是SEO</font></a></li>
</ul>
</div>
</div>
<div class="data_list article_list" style="margin-left: 12px">
<div class="dataHeader">
广告联盟<span class="more"><a href="/arcType/7.html">更多...</a></span>
</div>
<div class="datas">
<ul>
<li>[12-18] <a href="/article/150.html"
title="广告联盟推荐-搜狗联盟"><font color="">广告联盟推荐-搜狗联盟</font></a></li>
<li>[12-18] <a href="/article/149.html"
title="广告联盟推荐-谷歌联盟"><font color="#FF0000">广告联盟推荐-谷歌联盟</font></a></li>
<li>[12-18] <a href="/article/148.html"
title="广告联盟推荐-百度联盟"><font color="#FF0000">广告联盟推荐-百度联盟</font></a></li>
<li>[11-19] <a href="/article/121.html"
title="广告联盟推荐-大圣联盟(安徽)"><font color="">广告联盟推荐-大圣联盟(安徽)</font></a></li>
<li>[11-19] <a href="/article/120.html"
title="广告联盟推荐-百游联盟"><font color="">广告联盟推荐-百游联盟</font></a></li>
<li>[11-05] <a href="/article/74.html"
title="广告联盟推荐-七七联盟"><font color="#FF0000">广告联盟推荐-七七联盟</font></a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<jsp:include page="/common/link.jsp"></jsp:include>
<jsp:include page="/common/foot.jsp"></jsp:include>
</body>
<script type="text/javascript">
var auto;
var index = 0;
$(‘.tuhuo ul li‘).hover(function() {
clearTimeout(auto);
index = $(this).index();
move(index);
}, function() {
auto = setTimeout(‘autogo(‘ + index + ‘)‘, 3000);
});
function autogo() {
if (index < 3) {
move(index);
index++;
} else {
index = 0;
move(index);
index++;
}
}
function move(l) {
var src = $(‘.tu_img‘).eq(index).attr(‘src‘);
$("#fou_img").css({
"opacity" : "0"
});
$(‘#fou_img‘).attr(‘src‘, src);
$(‘#fou_img‘).stop(true).animate({
opacity : ‘1‘
}, 1000);
$(‘.tuhuo ul li‘).removeClass(‘fouce‘);
$(‘.tuhuo ul li‘).eq(index).addClass(‘fouce‘);
$(‘.tuhuo p‘).hide();
$(‘.tuhuo p‘).eq(index).show();
var ao = $(‘.tuhuo p‘).eq(index).children(‘a‘).attr(‘href‘);
$(‘#fou_img‘).parent(‘a‘).attr("href", ao);
}
autogo();
setInterval(‘autogo()‘, 3000);
</script>
</html>