mapper层:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.java1234.dao.BlogDao"><resultMap type="Blog" id="BlogResult">
<result property="id" column="id"/> <result property="title" column="title"/> <result property="summary" column="summary"/> <result property="releaseDate" column="releaseDate"/> <result property="clickHit" column="clickHit"/> <result property="replyHit" column="replyHit"/> <result property="content" column="content"/> <result property="keyWord" column="keyWord"/> <association property="blogType" column="typeId" select="com.java1234.dao.BlogTypeDao.findById"></association> </resultMap> <select id="countList" resultMap="BlogResult"> SELECT DATE_FORMAT(releaseDate,'%Y年%m月') AS releaseDateStr,COUNT(*) AS blogCount FROM t_blog GROUP BY DATE_FORMAT(releaseDate,'%Y年%m月') ORDER BY DATE_FORMAT(releaseDate,'%Y年%m月') DESC; </select> <select id="list" parameterType="Map" resultMap="BlogResult"> select * from t_blog <where> <if test="typeId!=null and typeId!='' "> and typeId=#{typeId} </if> <if test="releaseDateStr!=null and releaseDateStr!='' "> and DATE_FORMAT(releaseDate,'%Y年%m月')=#{releaseDateStr} </if> </where> order by releaseDate desc <if test="start!=null and size!=null"> limit #{start},#{size} </if> </select> <select id="getTotal" parameterType="Map" resultType="Long"> select count(*) from t_blog <where> <if test="typeId!=null and typeId!='' "> and typeId=#{typeId} </if> <if test="releaseDateStr!=null and releaseDateStr!='' "> and DATE_FORMAT(releaseDate,'%Y年%m月')=#{releaseDateStr} </if> </where> </select> <select id="findById" parameterType="Integer" resultMap="BlogResult"> select * from t_blog where id=#{id} </select> <update id="update" parameterType="Blog"> update t_blog <set> <if test="clickHit!=null"> clickHit=#{clickHit}, </if> <if test="replyHit!=null"> replyHit=#{replyHit}, </if> </set> where id=#{id} </update> <select id="getLastBlog" parameterType="Integer" resultMap="BlogResult"> SELECT * FROM t_blog WHERE id<#{id} ORDER BY id DESC LIMIT 1; </select> <select id="getNextBlog" parameterType="Integer" resultMap="BlogResult"> SELECT * FROM t_blog WHERE id>#{id} ORDER BY id ASC LIMIT 1; </select><insert id="add" parameterType="Blog"> insert into t_blog values(null,#{title},#{summary},now(),0,0,#{content},#{blogType.id},#{keyWord}) </insert></mapper>dao:
package com.java1234.dao;
import java.util.List;
import java.util.Map;import com.java1234.entity.Blog;
/**
* 博客Dao接口 * @author Administrator * */public interface BlogDao {/**
* 根据日期分月分组查询 * @return */ public List<Blog> countList(); /** * 分页查询博客 * @param map * @return */ public List<Blog> list(Map<String,Object> map); /** * 获取总记录数 * @param map * @return */ public Long getTotal(Map<String,Object> map); /** * 根据id查找实体 * @param id * @return */ public Blog findById(Integer id); /** * 更新博客信息 * @param blog * @return */ public Integer update(Blog blog); /** * 获取上一个博客 * @param id * @return */ public Blog getLastBlog(Integer id); /** * 获取下一个博客 * @param id * @return */ public Blog getNextBlog(Integer id); /** * 添加博客信息 * @param blog * @return */public Integer add(Blog blog);}service
package com.java1234.service;
import java.util.List;
import java.util.Map;import com.java1234.entity.Blog;
/**
* 博客Service接口 * @author Administrator * */public interface BlogService {/**
* 根据日期分月分组查询 * @return */ public List<Blog> countList(); /** * 分页查询博客 * @param map * @return */ public List<Blog> list(Map<String,Object> map); /** * 获取总记录数 * @param map * @return */ public Long getTotal(Map<String,Object> map); /** * 根据id查找实体 * @param id * @return */ public Blog findById(Integer id); /** * 更新博客信息 * @param blog * @return */ public Integer update(Blog blog); /** * 获取上一个博客 * @param id * @return */ public Blog getLastBlog(Integer id); /** * 获取下一个博客 * @param id * @return */ public Blog getNextBlog(Integer id); /** * 添加博客信息 * @param blog * @return */public Integer add(Blog blog);}serviceImpl
package com.java1234.service.impl;
import java.util.List;
import java.util.Map;import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.java1234.dao.BlogDao;
import com.java1234.entity.Blog;import com.java1234.service.BlogService;/**
* 博客Service实现类 * @author Administrator * */@Service("blogService")public class BlogServiceImpl implements BlogService{@Resource
private BlogDao blogDao; public List<Blog> countList() { return blogDao.countList(); }public List<Blog> list(Map<String, Object> map) {
return blogDao.list(map); }public Long getTotal(Map<String, Object> map) {
return blogDao.getTotal(map); }public Blog findById(Integer id) {
return blogDao.findById(id); }public Integer update(Blog blog) {
return blogDao.update(blog); }public Blog getLastBlog(Integer id) {
return blogDao.getLastBlog(id); }public Blog getNextBlog(Integer id) {
return blogDao.getNextBlog(id); }public Integer add(Blog blog) {
return blogDao.add(blog); }}
controller:
package com.java1234.controller.admin;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import com.java1234.entity.Blog;
import com.java1234.service.BlogService;import com.java1234.util.ResponseUtil;import net.sf.json.JSONObject;
/**
* 管理员博客Controller层 * @author Administrator * */@Controller@RequestMapping("/admin/blog")public class BlogAdminController {@Resource
private BlogService blogService; /** * 添加或者修改博客信息 * @param blog * @param response * @return * @throws Exception */ @RequestMapping("/save")public String save(Blog blog,HttpServletResponse response)throws Exception{ int resultTotal=0; if(blog.getId()==null){ resultTotal=blogService.add(blog); }else{ } JSONObject result=new JSONObject(); if(resultTotal>0){ result.put("success", true); }else{ result.put("success", false); } ResponseUtil.write(response, result); return null; }}页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><!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="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/default/easyui.css"><link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/icon.css"><script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.min.js"></script><script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script><script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script><script type="text/javascript" charset="gbk" src="${pageContext.request.contextPath}/static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="gbk" src="${pageContext.request.contextPath}/static/ueditor/ueditor.all.min.js"> </script><!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败--><!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文--><script type="text/javascript" charset="gbk" src="${pageContext.request.contextPath}/static/ueditor/lang/zh-cn/zh-cn.js"></script><script type="text/javascript">
function submitData(){ var title=$("#title").val(); var blogTypeId=$("#blogTypeId").combobox("getValue") var content=UE.getEditor('editor').getContent() var keyWord=$("#keyWord").val(); if(title==null || title==''){ alert("请输入标题!"); }else if(blogTypeId==null || blogTypeId==''){ alert("请选择博客类别!"); }else if(content==null || content==''){ alert("请填写内容!"); }else{ $.post("${pageContext.request.contextPath}/admin/blog/save.do",{'title':title,'blogType.id':blogTypeId, 'content':content,'summary':UE.getEditor('editor').getContentTxt().substr(0,155),'keyWord':keyWord},function(result){ if(result.success){ alert("博客发布成功!"); resultValue(); }else{ alert("博客发布失败!"); } },"json"); } } function resultValue(){ $("#title").val(""); $("#blogTypeId").combobox("setValue",""); UE.getEditor('editor').setContent(''); $("#keyWord").val(""); }</script></head><body style="margin: 10px"><div id="p" class="easyui-panel" title="编写博客" style="padding: 10px">
<table cellspacing="20px"> <tr> <td width="80px">博客标题:</td> <td> <input type="text" id="title" name="title" style="width: 400px"/> </td> </tr> <tr> <td>所属类别:</td> <td><select class="easyui-combobox" style="width: 154px" id="blogTypeId" name="blogType.id" editable="false" panelHeight="auto"> <option value="">请选择博客类别...</option> <c:forEach var="blogType" items="${blogTypeCountList }"> <option value="${blogType.id }">${blogType.typeName }</option> </c:forEach> </select> </td> </tr> <tr> <td valign="top">博客内容:</td> <td> <script id="editor" name="content" type="text/plain" style="width:100%;height:500px;"></script> </td> </tr> <tr> <td>关键字:</td> <td> <input type="text" id="keyWord" name="keyWord" style="width: 400px"/> (多个关键字中间用空格隔开) </td> </tr> <tr> <td></td> <td><a href="javascript:submitData()" class="easyui-linkbutton" data-options="iconCls:'icon-submit'">发布博客</a> </td> </tr> </table></div><!-- 实例化编辑器 -->
<script type="text/javascript">var ue = UE.getEditor('editor');</script>
</body>
</html>