首页 > 代码库 > cms-帖子管理

cms-帖子管理

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>

<select id="getRecommend" resultMap="ArticleResult">
select * from t_article where isRecommend=1 order by publishDate desc limit 0,7
</select>

<select id="getSlide" resultMap="ArticleResult">
select * from t_article where isSlide=1 order by publishDate desc limit 0,5
</select>

<select id="getIndex" parameterType="Integer" resultMap="ArticleResult">
select * from t_article where typeId=#{typeId} order by publishDate desc limit 0,8
</select>

<select id="findById" parameterType="Integer" resultMap="ArticleResult">
select * from t_article where id=#{id}
</select>

<select id="getLastArticle" parameterType="Integer" resultMap="ArticleResult">
SELECT * FROM t_article WHERE id &lt; #{id} ORDER BY id DESC LIMIT 1;
</select>

<select id="getNextArticle" parameterType="Integer" resultMap="ArticleResult">
SELECT * FROM t_article WHERE id &gt; #{id} ORDER BY id ASC LIMIT 1;
</select>

<update id="update" parameterType="Article">
update t_article
<set>
<if test="click!=0">
click=#{click},
</if>
</set>
where id=#{id}
</update>

<select id="list" parameterType="Map" resultMap="ArticleResult">
select * from t_article
<where>
<if test="typeId!=null">
and typeId=#{typeId}
</if>
</where>
order by publishDate desc
<if test="start!=null and size!=null">
limit #{start},#{size}
</if>
</select>

<select id="getTotal" parameterType="Map" resultType="Long">
select count(*) from t_article
<where>
<if test="typeId!=null">
and typeId=#{typeId}
</if>
</where>
</select>

<insert id="add" parameterType="Article">
insert into t_article values(null,#{title},#{publishDate},#{content},#{summary},#{titleColor},#{click},#{isRecommend},#{isSlide},#{slideImage},#{arcType.id},#{keyWords})
</insert>

</mapper>

dao:

package com.open1111.dao;

import java.util.List;
import java.util.Map;

import com.open1111.entity.Article;

/**
* 帖子Dao接口
* @author user
*
*/
public interface ArticleDao {

/**
* 获取最新的7条帖子
* @return
*/
public List<Article> getNewest();

/**
* 获取最新7条推荐的帖子
* @return
*/
public List<Article> getRecommend();

/**
* 获取最新5条幻灯的帖子
* @return
*/
public List<Article> getSlide();

/**
* 根据帖子类别来查找最新的8条数据
* @param typeId
* @return
*/
public List<Article> getIndex(Integer typeId);

/**
* 通过id查询帖子
* @param id
* @return
*/
public Article findById(Integer id);

/**
* 获取上一个帖子
* @param id
* @return
*/
public Article getLastArticle(Integer id);

/**
* 获取下一个帖子
* @param id
* @return
*/
public Article getNextArticle(Integer id);

/**
* 更新帖子
* @param article
* @return
*/
public Integer update(Article article);

/**
* 根据条件分页查询帖子
* @param map
* @return
*/
public List<Article> list(Map<String,Object> map);

/**
* 获取总记录数
* @param map
* @return
*/
public Long getTotal(Map<String,Object> map);

/**
* 添加帖子
* @param article
* @return
*/
public Integer add(Article article);
}

serviceImpl:

package com.open1111.service.impl;

import java.util.List;
import java.util.Map;

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();
}

public List<Article> getRecommend() {
return articleDao.getRecommend();
}

public List<Article> getSlide() {
return articleDao.getSlide();
}

public List<Article> getIndex(Integer typeId) {
return articleDao.getIndex(typeId);
}

public Article findById(Integer id) {
return articleDao.findById(id);
}

public Article getLastArticle(Integer id) {
return articleDao.getLastArticle(id);
}

public Article getNextArticle(Integer id) {
return articleDao.getNextArticle(id);
}

public Integer update(Article article) {
return articleDao.update(article);
}

public List<Article> list(Map<String, Object> map) {
return articleDao.list(map);
}

public Long getTotal(Map<String, Object> map) {
return articleDao.getTotal(map);
}

public Integer add(Article article) {
return articleDao.add(article);
}

}

comtroller:

package com.open1111.controller.admin;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.MultipartFile;

import com.open1111.entity.Article;
import com.open1111.entity.PageBean;
import com.open1111.service.ArticleService;
import com.open1111.service.impl.InitComponent;
import com.open1111.util.DateUtil;
import com.open1111.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
* 帖子后台管理Controller层
* @author user
*
*/
@Controller
@RequestMapping("/admin/article")
public class ArticleAdminController {

@Resource
private ArticleService articleService;

@Resource
private InitComponent initComponent;

/**
* 添加或者修改帖子信息
* @param article
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/save")
public String save(@RequestParam("slideImageFile") MultipartFile slideImageFile, Article article,HttpServletResponse response,HttpServletRequest request)throws Exception{
if(!slideImageFile.isEmpty()){
String filePath=request.getServletContext().getRealPath("/");
String imageName=DateUtil.getCurrentDateStr()+"."+slideImageFile.getOriginalFilename().split("\\.")[1];
slideImageFile.transferTo(new File(filePath+"static/userImages/"+imageName));
article.setSlideImage(imageName);
}
int resultTotal=0; // 操作的记录条数
article.setPublishDate(new Date());
if(article.getId()==null){ // 添加
resultTotal=articleService.add(article);
}else{ // 修改

}
StringBuffer result=new StringBuffer();
if(resultTotal>0){
initComponent.refreshSystem(ContextLoader.getCurrentWebApplicationContext().getServletContext());
result.append("<script language=‘javascript‘>alert(‘提交成功‘);</script>");
}else{
result.append("<script language=‘javascript‘>alert(‘提交失败,请联系管理员‘);</script>");
}
ResponseUtil.write(response, result);
return null;
}

/**
* 根据条件分页查询帖子信息
* @param page
* @param rows
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/list")
public String list(@RequestParam(value="http://www.mamicode.com/page",required=false)String page,@RequestParam(value="http://www.mamicode.com/rows",required=false)String rows,HttpServletResponse response)throws Exception{
PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
Map<String,Object> map=new HashMap<String,Object>();
map.put("start", pageBean.getStart());
map.put("size", pageBean.getPageSize());
List<Article> articleList=articleService.list(map);
Long total=articleService.getTotal(map);
JSONObject result=new JSONObject();
JsonConfig jsonConfig=new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
JSONArray jsonArray=JSONArray.fromObject(articleList, jsonConfig);
result.put("rows", jsonArray);
result.put("total", total);
ResponseUtil.write(response, result);
return null;
}
}

时间处理工具:

package com.open1111.controller.admin;

import java.text.SimpleDateFormat;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
* json-lib 日期处理类
* @author Administrator
*
*/
public class DateJsonValueProcessor implements JsonValueProcessor{

private String format;

public DateJsonValueProcessor(String format){
this.format = format;
}

public Object processArrayValue(Object value, JsonConfig jsonConfig) {
// TODO Auto-generated method stub
return null;
}

public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
if(value =http://www.mamicode.com/= null)
{
return "";
}
if(value instanceof java.sql.Timestamp)
{
String str = new SimpleDateFormat(format).format((java.sql.Timestamp)value);
return str;
}
if (value instanceof java.util.Date)
{
String str = new SimpleDateFormat(format).format((java.util.Date) value);
return str;
}

return value.toString();
}

}

后台页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>帖子管理页面</title>
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript" src="http://www.mamicode.com/${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="http://www.mamicode.com/${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="http://www.mamicode.com/${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">

function formatTitle(val,row){
return "&nbsp;<a target=‘_blank‘ href=http://www.mamicode.com/‘${pageContext.request.contextPath}/article/"+row.id+".html‘>"+val+"</a>";
}

function formatArcType(val,row){
return val.typeName;
}

</script>
</head>
<body style="margin: 1px">
<table id="dg" title="帖子管理" class="easyui-datagrid"
fitColumns="true" pagination="true" rownumbers="true"
url="${pageContext.request.contextPath}/admin/article/list.do" fit="true">
<thead>
<tr>
<th field="cb" checkbox="true" align="center"></th>
<th field="id" width="20" align="center">编号</th>
<th field="title" width="200" align="center" formatter="formatTitle">标题</th>
<th field="publishDate" width="50" align="center">发布日期</th>
<th field="arcType" width="50" align="center" formatter="formatArcType">博客类别</th>
<th field="click" width="50" align="center">阅读次数</th>
</tr>
</thead>
</table>
</body>
</html>

 

cms-帖子管理