提交 443216d9 编写于 作者: S smileNicky

Merge remote-tracking branch 'origin/1.0.0'

package net.myblog.entity;
import javax.persistence.*;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* 博客系统文章信息的实体类
* @author Nicky
......@@ -46,7 +36,10 @@ public class Article {
/** 文章类别。0为私有,1为公开,2为仅好友查看**/
private int articleType;
private int typeId;
private ArticleSort articleSort;
@GeneratedValue(strategy=GenerationType.IDENTITY)
......@@ -129,6 +122,14 @@ public class Article {
this.articleType = articleType;
}
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}
@JoinColumn(name="articleId",insertable = false, updatable = false)
@ManyToOne(fetch=FetchType.LAZY)
public ArticleSort getArticleSort() {
......
package net.myblog.entity.dto;
import java.io.Serializable;
import java.util.Date;
/**
* <pre>
*
* </pre>
*
* @author nicky
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期: 2019年01月12日 修改内容:
* </pre>
*/
public class ArticleDto implements Serializable{
/** 文章Id,自增**/
private int articleId;
/** 文章名称**/
private String articleName;
/** 文章发布时间**/
private Date articleTime;
/** 图片路径,测试**/
private String imgPath;
/** 文章内容**/
private String articleContent;
/** 查看人数**/
private int articleClick;
/** 是否博主推荐。0为否;1为是**/
private int articleSupport;
/** 是否置顶。0为;1为是**/
private int articleUp;
/** 文章类别。0为私有,1为公开,2为仅好友查看**/
private int articleType;
/** typeID,外键**/
private int typeId;
public int getArticleId() {
return articleId;
}
public void setArticleId(int articleId) {
this.articleId = articleId;
}
public String getArticleName() {
return articleName;
}
public void setArticleName(String articleName) {
this.articleName = articleName;
}
public Date getArticleTime() {
return articleTime;
}
public void setArticleTime(Date articleTime) {
this.articleTime = articleTime;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
public String getArticleContent() {
return articleContent;
}
public void setArticleContent(String articleContent) {
this.articleContent = articleContent;
}
public int getArticleClick() {
return articleClick;
}
public void setArticleClick(int articleClick) {
this.articleClick = articleClick;
}
public int getArticleSupport() {
return articleSupport;
}
public void setArticleSupport(int articleSupport) {
this.articleSupport = articleSupport;
}
public int getArticleUp() {
return articleUp;
}
public void setArticleUp(int articleUp) {
this.articleUp = articleUp;
}
public int getArticleType() {
return articleType;
}
public void setArticleType(int articleType) {
this.articleType = articleType;
}
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}
}
package net.myblog.repository;
import net.myblog.entity.dto.ArticleSortDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import javax.persistence.QueryHint;
import java.util.List;
/**
* <pre>
*
* </pre>
*
* @author nicky
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期: 2019年01月12日 修改内容:
* </pre>
*/
public interface ArticleSortDtoRepository extends JpaRepository<ArticleSortDto,Integer> {
/**启用setHint缓存,查询一次,之后缓存处理**/
@Query("FROM ArticleSortDto")
@QueryHints({@QueryHint(name=org.hibernate.ejb.QueryHints.HINT_CACHEABLE,value="true")})
List<ArticleSortDto> getAll();
}
......@@ -11,10 +11,10 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
public interface ArticleSortRepository extends JpaRepository<ArticleSortDto,Integer>{
public interface ArticleSortRepository extends JpaRepository<ArticleSort,Integer>{
/**启用setHint缓存,查询一次,之后缓存处理**/
@Query("FROM ArticleSortDto")
@Query("FROM ArticleSort")
@QueryHints({@QueryHint(name=org.hibernate.ejb.QueryHints.HINT_CACHEABLE,value="true")})
List<ArticleSortDto> getAll();
}
package net.myblog.service;
import java.util.Date;
import java.util.List;
import net.myblog.entity.Article;
import net.myblog.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
......@@ -13,6 +9,9 @@ import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
@Service
public class ArticleService {
......@@ -72,11 +71,8 @@ public class ArticleService {
return articles;
}
/**获取站长推荐的文章
* @param num
* @param size
* @param dir
* @param str
/**
* 获取站长推荐的文章
* @return
*/
@Transactional
......@@ -103,5 +99,14 @@ public class ArticleService {
public List<Article> findArticleByMonth(Date month){
return articleRepository.findArticleByMonth(month);
}
/**
* 保存文章信息
* @param article
* @return
*/
@Transactional
public Article saveOrUpdateArticle(Article article) {
return articleRepository.save(article);
}
}
package net.myblog.service;
import net.myblog.entity.ArticleSort;
import net.myblog.entity.dto.ArticleSortDto;
import net.myblog.repository.ArticleSortDtoRepository;
import net.myblog.repository.ArticleSortRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -12,6 +14,8 @@ import java.util.List;
public class ArticleSortService {
@Autowired ArticleSortRepository articleSortRepository;
@Autowired
ArticleSortDtoRepository articleSortDtoRepository;
/**
* 获取所有的博客标签(类别)信息
......@@ -19,7 +23,12 @@ public class ArticleSortService {
*/
@Transactional(readOnly=true)
public List<ArticleSortDto> findAll(){
return articleSortRepository.findAll();
return articleSortDtoRepository.findAll();
}
@Transactional
public ArticleSort getArticleSort(int typeId) {
return articleSortRepository.findOne(typeId);
}
}
package net.myblog.web.controller;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
public class BaseController {
Logger LOGGER = LoggerFactory.getLogger(getClass());
public void debug(String message){
LOGGER.debug(message);
}
public void debug(String message , Exception e){
LOGGER.debug(message , e);
}
public void info(String message) {
LOGGER.info(message);
}
public void info(String message,Exception e){
LOGGER.info(message , e);
}
public void error(String message) {
LOGGER.error(message);
}
public void error(String message,Exception e){
LOGGER.error(message , e);
}
/**
* 得到request对象
*/
......
package net.myblog.web.controller.admin;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import net.myblog.core.Constants;
import net.myblog.entity.Article;
import net.myblog.service.ArticleService;
import net.myblog.service.ArticleSortService;
import net.myblog.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
......@@ -14,14 +13,22 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
@Controller
@RequestMapping("/article")
public class ArticleAdminController extends BaseController{
@Autowired ArticleService articleService;
@Autowired
ArticleService articleService;
@Autowired
ArticleSortService articleSortService;
/**
* 跳转到文章列表页面
......@@ -55,5 +62,32 @@ public class ArticleAdminController extends BaseController{
mv.setViewName("admin/article/article_write");
return mv;
}
/**
* 修改更新文章
*/
@RequestMapping(value = "/saveOrUpdateArticle", method = RequestMethod.POST)
@ResponseBody
public String saveOrUpdateArticle (@RequestParam("title")String title , @RequestParam("content")String content,
@RequestParam("typeId")String typeIdStr) {
int typeId = Integer.parseInt(typeIdStr);
Article article = new Article();
article.setArticleName(title);
article.setArticleContent(content);
article.setArticleTime(new Date());
article.setTypeId(typeId);
//ArticleSort articleSort = this.articleSortService.getArticleSort(typeId);
//article.setArticleSort(articleSort);
JSONObject result = new JSONObject();
try {
this.articleService.saveOrUpdateArticle(article);
result.put("result","success");
return result.toJSONString();
} catch (Exception e) {
error("保存文章报错:{}"+e);
result.put("result","error");
return result.toJSONString();
}
}
}
......@@ -38,15 +38,15 @@
<body>
<div id="layout">
<header>
文章标题:<input type="text" id="articleTitle" />
文章标题:<input type="text" id="articleTitle" value="title" />
类别:
<select id="articleCategory"></select>
<span id="btnList">
<button type="button" id="publishArticle" class="btn btn-info">发布文章</button>
<button type="button" id="publishArticle" onclick="writeArticle.doSubmit();" class="btn btn-info">发布文章</button>
</span>
</header>
<div id="test-editormd">
<textarea style="display: none;">[TOC]
<textarea id="articleContent" style="display: none;">[TOC]
#### Disabled options
......@@ -97,9 +97,10 @@
});
categorySelect.init();
});
/* 文章类别下拉框数据绑定 */
var categorySelect = {
init: function () {
init: function () {//初始化数据
$.ajax({
type: "GET",
url: 'articleSort/listArticleCategory.do',
......@@ -113,8 +114,8 @@
}
});
},
buildOption: function (data) {
debugger;
buildOption: function (data) {//构建下拉框数据
//debugger;
var optionStr ="";
for(var i=0 ; i < data.length; i ++) {
optionStr += "<option value="+data[i].typeId+">";
......@@ -124,6 +125,49 @@
$("#articleCategory").append(optionStr);
}
}
/* 发送文章*/
var writeArticle = {
doSubmit: function () {//提交
if (writeArticle.doCheck()) {
//debugger;
var title = $("#articleTitle").val();
var content = $("#articleContent").val();
var typeId = $("#articleCategory").val();
$.ajax({
type: "POST",
url: '<%=basePath %>article/saveOrUpdateArticle.do',
data: {'title':title,'content':content,'typeId':typeId},
dataType:'json',
//contentType:"application/json",
cache: false,
success: function(data){
//debugger;
if ("success"== data.result) {
alert("保存成功!");
window.local.close();
}
}
});
}
},
doCheck: function() {//校验
//debugger;
var title = $("#articleTitle").val();
var content = $("#articleContent").val();
if (typeof(title) == undefined || title == null || title == "" ) {
alert("请填写文章标题!");
return false;
}
if(typeof (content) == undefined || content == null || content == "") {
alert("请填写文章内容!");
return false;
}
return true;
}
}
</script>
</body>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册