修改sql
This commit is contained in:
@@ -26,7 +26,8 @@ CREATE TABLE `article_tag`
|
|||||||
(
|
(
|
||||||
`at_id` bigint(20) primary key auto_increment,
|
`at_id` bigint(20) primary key auto_increment,
|
||||||
`a_id` bigint(20) not null comment '文章id',
|
`a_id` bigint(20) not null comment '文章id',
|
||||||
`t_id` bigint not null comment 'tag/category 的id'
|
`t_id` bigint not null comment 'tag/category 的id',
|
||||||
|
`is_delete` boolean not null default false comment '该数据是否被删除'
|
||||||
) comment '文章标签表';
|
) comment '文章标签表';
|
||||||
|
|
||||||
CREATE TABLE `tag_category`
|
CREATE TABLE `tag_category`
|
||||||
|
|||||||
@@ -2,14 +2,12 @@ package cn.celess.blog.service.serviceimpl;
|
|||||||
|
|
||||||
import cn.celess.blog.enmu.LevelEnum;
|
import cn.celess.blog.enmu.LevelEnum;
|
||||||
import cn.celess.blog.enmu.ResponseEnum;
|
import cn.celess.blog.enmu.ResponseEnum;
|
||||||
|
import cn.celess.blog.enmu.RoleEnum;
|
||||||
import cn.celess.blog.entity.*;
|
import cn.celess.blog.entity.*;
|
||||||
import cn.celess.blog.entity.model.ArticleModel;
|
import cn.celess.blog.entity.model.ArticleModel;
|
||||||
import cn.celess.blog.entity.request.ArticleReq;
|
import cn.celess.blog.entity.request.ArticleReq;
|
||||||
import cn.celess.blog.exception.MyException;
|
import cn.celess.blog.exception.MyException;
|
||||||
import cn.celess.blog.mapper.ArticleMapper;
|
import cn.celess.blog.mapper.*;
|
||||||
import cn.celess.blog.mapper.CategoryMapper;
|
|
||||||
import cn.celess.blog.mapper.CommentMapper;
|
|
||||||
import cn.celess.blog.mapper.TagMapper;
|
|
||||||
import cn.celess.blog.service.ArticleService;
|
import cn.celess.blog.service.ArticleService;
|
||||||
import cn.celess.blog.service.UserService;
|
import cn.celess.blog.service.UserService;
|
||||||
import cn.celess.blog.util.DateFormatUtil;
|
import cn.celess.blog.util.DateFormatUtil;
|
||||||
@@ -50,6 +48,8 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
CommentMapper commentMapper;
|
CommentMapper commentMapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
|
ArticleTagMapper articleTagMapper;
|
||||||
|
@Autowired
|
||||||
UserService userService;
|
UserService userService;
|
||||||
@Autowired
|
@Autowired
|
||||||
HttpServletRequest request;
|
HttpServletRequest request;
|
||||||
@@ -79,12 +79,20 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
if (reqBody.getCategory() == null || reqBody.getCategory().replaceAll(" ", "").isEmpty()) {
|
if (reqBody.getCategory() == null || reqBody.getCategory().replaceAll(" ", "").isEmpty()) {
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||||
}
|
}
|
||||||
if (reqBody.getTags() == null || reqBody.getTags().replaceAll(" ", "").isEmpty()) {
|
if (reqBody.getTags() == null || reqBody.getTags().length == 0) {
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||||
}
|
}
|
||||||
|
if (articleMapper.existsByTitle(reqBody.getTitle())) {
|
||||||
|
throw new MyException(ResponseEnum.ARTICLE_HAS_EXIST);
|
||||||
|
}
|
||||||
|
// 查看是否存在已有的分类
|
||||||
|
Category category = (Category) categoryMapper.findCategoryByName(reqBody.getCategory());
|
||||||
|
if (category == null) {
|
||||||
|
throw new MyException(ResponseEnum.CATEGORY_NOT_EXIST);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//写入数据库的数据
|
// 构建 需要写入数据库的对象数据
|
||||||
Article article = new Article();
|
Article article = new Article();
|
||||||
article.setTitle(reqBody.getTitle());
|
article.setTitle(reqBody.getTitle());
|
||||||
article.setOpen(reqBody.getOpen());
|
article.setOpen(reqBody.getOpen());
|
||||||
@@ -92,282 +100,155 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
article.setUrl(reqBody.getUrl());
|
article.setUrl(reqBody.getUrl());
|
||||||
article.setType(reqBody.getType());
|
article.setType(reqBody.getType());
|
||||||
|
|
||||||
article.setAuthorId(redisUserUtil.get().getId());
|
article.setUser(redisUserUtil.get());
|
||||||
article.setPublishDate(new Date());
|
article.setPublishDate(new Date());
|
||||||
|
|
||||||
//防止出现 “null,xxx”这种情况
|
|
||||||
article.setTagsId("");
|
|
||||||
|
|
||||||
|
|
||||||
//是否需要更新上一篇文章
|
|
||||||
boolean isUpdatePreArticle = true;
|
|
||||||
|
|
||||||
Article preArticle = null;
|
|
||||||
|
|
||||||
|
|
||||||
if (articleMapper.count() == 0) {
|
|
||||||
isUpdatePreArticle = false;
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
|
||||||
//获取最新的一条数据
|
|
||||||
preArticle = articleMapper.getLastestArticle();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isUpdatePreArticle) {
|
|
||||||
logger.info("上一篇文章的id为:" + preArticle.getId());
|
|
||||||
//设置上一篇文章的id
|
|
||||||
article.setPreArticleId(preArticle.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
//markdown->html->summary
|
//markdown->html->summary
|
||||||
String str = StringFromHtmlUtil.getString(MDTool.markdown2Html(article.getMdContent()));
|
String str = StringFromHtmlUtil.getString(MDTool.markdown2Html(article.getMdContent()));
|
||||||
//获取摘要 摘要长度为255个字符
|
//获取摘要 摘要长度为255个字符
|
||||||
String summary = str.length() > 240 ? str.substring(0, 240) + "......" : str;
|
String summary = str.length() > 240 ? str.substring(0, 240) + "......" : str;
|
||||||
|
|
||||||
//去除转换后存在的空格
|
|
||||||
String tagStr = reqBody.getTags().replaceAll(" ", "");
|
|
||||||
article.setSummary(summary);
|
article.setSummary(summary);
|
||||||
|
|
||||||
if (articleMapper.existsByTitle(article.getTitle())) {
|
article.setCategory(category);
|
||||||
throw new MyException(ResponseEnum.ARTICLE_HAS_EXIST);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//将分类写入数据库
|
|
||||||
Category category1 = categoryMapper.findCategoryByName(reqBody.getCategory());
|
|
||||||
if (category1 == null) {
|
|
||||||
category1 = new Category();
|
|
||||||
category1.setArticles("");
|
|
||||||
category1.setName(reqBody.getCategory());
|
|
||||||
categoryMapper.insert(category1);
|
|
||||||
}
|
|
||||||
|
|
||||||
article.setCategoryId(category1.getId());
|
|
||||||
|
|
||||||
//文章存数据库
|
//文章存数据库
|
||||||
articleMapper.insert(article);
|
articleMapper.insert(article);
|
||||||
//获取新增的文章
|
|
||||||
|
|
||||||
if (isUpdatePreArticle) {
|
|
||||||
//更新上一篇文章的“下一篇文章ID”
|
|
||||||
articleMapper.updateNextArticleId(preArticle.getId(), article.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
//无效
|
|
||||||
// articleMapper.updatePreArticleId(article.getId(), preArticle == null ? -1 : preArticle.getId());
|
|
||||||
article.setPreArticleId(preArticle == null ? -1 : preArticle.getId());
|
|
||||||
|
|
||||||
category1.setArticles(category1.getArticles() + article.getId() + ",");
|
|
||||||
categoryMapper.update(category1);
|
|
||||||
|
|
||||||
|
|
||||||
//将标签写入数据库
|
//将标签写入数据库
|
||||||
for (String t : tagStr.split(",")) {
|
for (String tagName : reqBody.getTags()) {
|
||||||
if (t.replaceAll(" ", "").length() == 0) {
|
if (tagName.replaceAll(" ", "").length() == 0) {
|
||||||
//单个标签只含空格
|
//单个标签只含空格
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Tag tag = tagMapper.findTagByName(t);
|
Tag tag = (Tag) tagMapper.findTagByName(tagName);
|
||||||
if (tag == null) {
|
if (tag == null) {
|
||||||
tag = new Tag();
|
tag = new Tag();
|
||||||
tag.setName(t);
|
tag.setName(tagName);
|
||||||
tag.setArticles("");
|
|
||||||
tagMapper.insert(tag);
|
tagMapper.insert(tag);
|
||||||
}
|
}
|
||||||
tag.setArticles(tag.getArticles() + article.getId() + ",");
|
ArticleTag articleTag = new ArticleTag(article, tag);
|
||||||
article.setTagsId(article.getTagsId() + tag.getId() + ",");
|
articleTagMapper.insert(articleTag);
|
||||||
tagMapper.update(tag);
|
|
||||||
}
|
}
|
||||||
articleMapper.update(article);
|
return fullTransform(article);
|
||||||
return fullTransform(articleMapper.getLastestArticle());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public boolean delete(long articleID) {
|
public boolean delete(long articleId) {
|
||||||
|
Article articleForDel = articleMapper.findArticleById(articleId);
|
||||||
Article articleForDel = articleMapper.findArticleById(articleID);
|
|
||||||
|
|
||||||
if (articleForDel == null) {
|
if (articleForDel == null) {
|
||||||
throw new MyException(ResponseEnum.ARTICLE_NOT_EXIST);//文章不存在
|
//文章不存在
|
||||||
|
throw new MyException(ResponseEnum.ARTICLE_NOT_EXIST);
|
||||||
}
|
}
|
||||||
|
|
||||||
Article preArticle = articleMapper.findArticleById(articleForDel.getPreArticleId());
|
//对访问情况进行判断 非admin 权限不可删除文章
|
||||||
Article nextArticle = articleMapper.findArticleById(articleForDel.getNextArticleId());
|
|
||||||
|
|
||||||
//对访问情况进行判断 非博主/非自己文章 拒绝访问
|
|
||||||
User user = redisUserUtil.get();
|
User user = redisUserUtil.get();
|
||||||
if (!user.getRole().contains("admin") && !articleForDel.getAuthorId().equals(user.getId())) {
|
if (!RoleEnum.ADMIN_ROLE.getRoleName().equals(user.getRole())) {
|
||||||
throw new MyException(ResponseEnum.PERMISSION_ERROR);
|
throw new MyException(ResponseEnum.PERMISSION_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
//删除的文章处于中间位置
|
|
||||||
if (nextArticle != null && preArticle != null) {
|
|
||||||
|
|
||||||
//修改上一篇文章的“下一篇文章”y
|
|
||||||
articleMapper.updateNextArticleId(articleForDel.getPreArticleId(), articleForDel.getNextArticleId());
|
|
||||||
|
|
||||||
//修改下一篇文章的 “上一篇文章”
|
|
||||||
articleMapper.updatePreArticleId(articleForDel.getNextArticleId(), articleForDel.getPreArticleId());
|
|
||||||
}
|
|
||||||
if (preArticle == null && nextArticle != null) {
|
|
||||||
//删除的是第一篇文章
|
|
||||||
articleMapper.updatePreArticleId(nextArticle.getId(), -1);
|
|
||||||
}
|
|
||||||
if (nextArticle == null && preArticle != null) {
|
|
||||||
//删除的是最后一篇文章
|
|
||||||
articleMapper.updateNextArticleId(preArticle.getId(), -1);
|
|
||||||
}
|
|
||||||
// delete count 为删除的数据数量
|
|
||||||
int deleteCount = commentMapper.deleteByArticleId(articleID);
|
|
||||||
|
|
||||||
//删除标签中的文章id
|
|
||||||
String tag = articleForDel.getTagsId();
|
|
||||||
if (tag.length() > 0) {
|
|
||||||
String[] tags = tag.split(",");
|
|
||||||
for (String t : tags) {
|
|
||||||
if (t != null) {
|
|
||||||
//查询标签
|
|
||||||
Tag tag1 = tagMapper.findTagById(Long.parseLong(t));
|
|
||||||
//去除标签中的articleId中的待删除的文章id
|
|
||||||
String s = tag1.getArticles().replaceAll(articleForDel.getId() + ",", "");
|
|
||||||
tag1.setArticles(s);
|
|
||||||
tagMapper.update(tag1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//删除分类中的文章id
|
|
||||||
//获取文章的分类
|
|
||||||
long categoryId = articleForDel.getCategoryId();
|
|
||||||
Category category = categoryMapper.findCategoryById(categoryId);
|
|
||||||
//删除文章id
|
|
||||||
category.setArticles(category.getArticles().replaceAll(articleForDel.getId() + ",", ""));
|
|
||||||
//更新
|
|
||||||
categoryMapper.update(category);
|
|
||||||
|
|
||||||
//删除指定文章
|
//删除指定文章
|
||||||
articleMapper.delete(articleID);
|
articleMapper.delete(articleId);
|
||||||
|
|
||||||
|
articleTagMapper.deleteByArticleId(articleId);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
@Override
|
@Override
|
||||||
public ArticleModel update(ArticleReq reqBody) {
|
public ArticleModel update(ArticleReq reqBody) {
|
||||||
if (reqBody == null) {
|
if (reqBody == null || reqBody.getId() == null) {
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||||
}
|
}
|
||||||
|
// 查找数据
|
||||||
|
Article article = articleMapper.findArticleById(reqBody.getId());
|
||||||
|
|
||||||
//数据判断
|
//数据判断
|
||||||
if (reqBody.getTitle() == null || reqBody.getTitle().replaceAll(" ", "").isEmpty()) {
|
if (reqBody.getTitle() != null && !reqBody.getTitle().replaceAll(" ", "").isEmpty()) {
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
if (articleMapper.existsByTitle(reqBody.getTitle())) {
|
||||||
} else if (reqBody.getMdContent() == null || reqBody.getMdContent().replaceAll(" ", "").isEmpty()) {
|
throw new MyException(ResponseEnum.ARTICLE_HAS_EXIST);
|
||||||
|
}
|
||||||
|
article.setTitle(reqBody.getTitle());
|
||||||
|
}
|
||||||
|
if (reqBody.getMdContent() != null && !reqBody.getMdContent().replaceAll(" ", "").isEmpty()) {
|
||||||
|
article.setMdContent(reqBody.getMdContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
//转载 判断链接
|
||||||
|
if (reqBody.getType() != null) {
|
||||||
|
if (!reqBody.getType() && reqBody.getUrl() == null) {
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||||
}
|
}
|
||||||
//转载 判断链接
|
|
||||||
if (!reqBody.getType()) {
|
if (!RegexUtil.urlMatch(reqBody.getUrl())) {
|
||||||
if (reqBody.getUrl() == null || reqBody.getUrl().replaceAll(" ", "").isEmpty()) {
|
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
|
||||||
} else if (!RegexUtil.urlMatch(reqBody.getUrl())) {
|
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_URL_ERROR);
|
throw new MyException(ResponseEnum.PARAMETERS_URL_ERROR);
|
||||||
}
|
}
|
||||||
|
article.setType(reqBody.getType());
|
||||||
|
article.setUrl(reqBody.getUrl());
|
||||||
}
|
}
|
||||||
if (reqBody.getCategory() == null || reqBody.getCategory().replaceAll(" ", "").isEmpty()) {
|
if (reqBody.getCategory() != null && !reqBody.getCategory().replaceAll(" ", "").isEmpty()) {
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
Category category = (Category) categoryMapper.findCategoryByName(reqBody.getCategory());
|
||||||
|
if (category == null) {
|
||||||
|
category = new Category();
|
||||||
|
category.setName(reqBody.getCategory());
|
||||||
|
categoryMapper.insert(category);
|
||||||
}
|
}
|
||||||
// 暂时不更新tags
|
article.setCategory(category);
|
||||||
if (reqBody.getTags() == null || reqBody.getTags().replaceAll(" ", "").isEmpty()) {
|
}
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
|
||||||
|
if (reqBody.getTags() != null && reqBody.getTags().length != 0) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//写入数据库的数据
|
//写入数据库的数据
|
||||||
Article article = new Article();
|
article.setOpen(reqBody.getOpen() ? article.getOpen() : reqBody.getOpen());
|
||||||
if (reqBody.getId() == null) {
|
|
||||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR.getCode(), "id不能为空");
|
|
||||||
}
|
|
||||||
article.setId(reqBody.getId());
|
|
||||||
article.setTitle(reqBody.getTitle());
|
|
||||||
article.setOpen(reqBody.getOpen());
|
|
||||||
article.setMdContent(reqBody.getMdContent());
|
|
||||||
article.setUrl(reqBody.getUrl());
|
|
||||||
article.setType(reqBody.getType());
|
|
||||||
|
|
||||||
|
|
||||||
Article oldArticle = articleMapper.findArticleById(reqBody.getId());
|
|
||||||
|
|
||||||
Category category = categoryMapper.findCategoryById(oldArticle.getCategoryId());
|
|
||||||
if (!(category.getName()).equals(reqBody.getCategory())) {
|
|
||||||
//修改更新之前数据 的分类
|
|
||||||
category.setArticles(category.getArticles().replace(reqBody.getId() + ",", ""));
|
|
||||||
//更新
|
|
||||||
categoryMapper.update(category);
|
|
||||||
|
|
||||||
//更新 更新之后的分类
|
|
||||||
Category category1 = categoryMapper.findCategoryByName(reqBody.getCategory());
|
|
||||||
if (category1 == null) {
|
|
||||||
category1 = new Category();
|
|
||||||
category1.setName(reqBody.getCategory());
|
|
||||||
category1.setArticles(reqBody.getId() + ",");
|
|
||||||
categoryMapper.insert(category1);
|
|
||||||
}
|
|
||||||
article.setCategoryId(category1.getId());
|
|
||||||
} else {
|
|
||||||
article.setCategoryId(oldArticle.getCategoryId());
|
|
||||||
}
|
|
||||||
|
|
||||||
String[] newTags = reqBody.getTags().split(",");
|
|
||||||
String[] tagIds = oldArticle.getTagsId().split(",");
|
|
||||||
//防止出现 ‘null2’这种情况
|
|
||||||
article.setTagsId("");
|
|
||||||
for (String t : newTags) {
|
|
||||||
Tag tag = tagMapper.findTagByName(t);
|
|
||||||
if (tag == null) {
|
|
||||||
tag = new Tag();
|
|
||||||
tag.setName(t);
|
|
||||||
tag.setArticles(oldArticle.getId() + ",");
|
|
||||||
int status = tagMapper.insert(tag);
|
|
||||||
if (status == 0) {
|
|
||||||
// 插入失败
|
|
||||||
throw new MyException(ResponseEnum.FAILURE);
|
|
||||||
}
|
|
||||||
article.setTagsId(article.getTagsId() + tag.getId() + ",");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
article.setTagsId(article.getTagsId() + tag.getId() + ",");
|
|
||||||
}
|
|
||||||
for (String tagId : tagIds) {
|
|
||||||
Tag tagById = tagMapper.findTagById(Long.parseLong(tagId));
|
|
||||||
// 在新更新的tag中是否有原有的tag
|
|
||||||
boolean isOldTag = false;
|
|
||||||
for (String s : newTags) {
|
|
||||||
if (s.equals(tagById.getName())) {
|
|
||||||
isOldTag = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!isOldTag) {
|
|
||||||
tagById.setArticles(tagById.getArticles().replace(oldArticle.getId() + ",", ""));
|
|
||||||
}
|
|
||||||
tagMapper.update(tagById);
|
|
||||||
}
|
|
||||||
//
|
|
||||||
// // TODO:::: tag的更新
|
|
||||||
// article.setTagsId(oldArticle.getTagsId());
|
|
||||||
|
|
||||||
|
|
||||||
article.setUpdateDate(new Date());
|
|
||||||
// TODO::::换用beansUtil
|
|
||||||
// 设置不定参数
|
|
||||||
article.setReadingNumber(oldArticle.getReadingNumber());
|
|
||||||
article.setPublishDate(oldArticle.getPublishDate());
|
|
||||||
article.setAuthorId(redisUserUtil.get().getId());
|
|
||||||
article.setPreArticleId(oldArticle.getPreArticleId());
|
|
||||||
article.setNextArticleId(oldArticle.getNextArticleId());
|
|
||||||
String str = StringFromHtmlUtil.getString(MDTool.markdown2Html(article.getMdContent()));
|
String str = StringFromHtmlUtil.getString(MDTool.markdown2Html(article.getMdContent()));
|
||||||
article.setSummary(str.length() > 240 ? str.substring(0, 240) + "......" : str);
|
article.setSummary(str.length() > 240 ? str.substring(0, 240) + "......" : str);
|
||||||
articleMapper.update(article);
|
articleMapper.update(article);
|
||||||
|
|
||||||
|
|
||||||
|
List<ArticleTag> allByArticleId = articleTagMapper.findAllByArticleId(article.getId());
|
||||||
|
List<ArticleTag> updateList = new ArrayList<>();
|
||||||
|
List<ArticleTag> deleteList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (String tag : reqBody.getTags()) {
|
||||||
|
boolean contain = allByArticleId.stream().anyMatch(articleTag -> articleTag.getTag().getName().equals(tag));
|
||||||
|
if (!contain) {
|
||||||
|
ArticleTag articleTag = new ArticleTag();
|
||||||
|
articleTag.setArticle(article);
|
||||||
|
Tag tagByName = (Tag) tagMapper.findTagByName(tag);
|
||||||
|
if (tagByName == null) {
|
||||||
|
tagByName = new Tag(tag);
|
||||||
|
tagMapper.insert(tagByName);
|
||||||
|
}
|
||||||
|
articleTag.setTag(tagByName);
|
||||||
|
updateList.add(articleTag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allByArticleId.forEach(articleTag -> {
|
||||||
|
boolean contain = false;
|
||||||
|
for (String tag : reqBody.getTags()) {
|
||||||
|
if (articleTag.getTag().getName().equals(tag)) {
|
||||||
|
contain = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!contain) {
|
||||||
|
deleteList.add(articleTag);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (updateList.size() != 0) {
|
||||||
|
updateList.forEach(articleTag -> articleTagMapper.insert(articleTag));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deleteList.size() != 0) {
|
||||||
|
articleTagMapper.deleteMultiById(deleteList);
|
||||||
|
}
|
||||||
|
|
||||||
//更新完成移除
|
//更新完成移除
|
||||||
request.getSession().removeAttribute("article4update");
|
request.getSession().removeAttribute("article4update");
|
||||||
return fullTransform(article);
|
return fullTransform(article);
|
||||||
@@ -420,7 +301,7 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageInfo findByCategory(String name, int page, int count) {
|
public PageInfo findByCategory(String name, int page, int count) {
|
||||||
Long idByName = categoryMapper.getIDByName(name);
|
Long idByName = categoryMapper.getIdByName(name);
|
||||||
if (idByName == null) {
|
if (idByName == null) {
|
||||||
throw new MyException(ResponseEnum.CATEGORY_NOT_EXIST);
|
throw new MyException(ResponseEnum.CATEGORY_NOT_EXIST);
|
||||||
}
|
}
|
||||||
@@ -431,13 +312,13 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PageInfo findByTag(String name, int page, int count) {
|
public PageInfo findByTag(String name, int page, int count) {
|
||||||
Tag tag = tagMapper.findTagByName(name);
|
Tag tag = (Tag) tagMapper.findTagByName(name);
|
||||||
if (tag == null) {
|
if (tag == null) {
|
||||||
throw new MyException(ResponseEnum.TAG_NOT_EXIST);
|
throw new MyException(ResponseEnum.TAG_NOT_EXIST);
|
||||||
}
|
}
|
||||||
|
// TODO :
|
||||||
PageHelper.startPage(page, count);
|
PageHelper.startPage(page, count);
|
||||||
String[] split = tag.getArticles().split(",");
|
List<String> list = Arrays.asList(null);
|
||||||
List<String> list = Arrays.asList(split);
|
|
||||||
List<Article> articleList = articleMapper.getSimpleInfoByTag(list);
|
List<Article> articleList = articleMapper.getSimpleInfoByTag(list);
|
||||||
PageInfo pageInfo = new PageInfo(articleList);
|
PageInfo pageInfo = new PageInfo(articleList);
|
||||||
return pageInfo;
|
return pageInfo;
|
||||||
@@ -500,19 +381,19 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
*/
|
*/
|
||||||
private ArticleModel suitableTransform(Article a) {
|
private ArticleModel suitableTransform(Article a) {
|
||||||
ArticleModel model = simpleTransform(a);
|
ArticleModel model = simpleTransform(a);
|
||||||
model.setAuthorName(userService.getNameById(a.getAuthorId()));
|
// model.setAuthor(a.getUser());
|
||||||
model.setPublishDateFormat(DateFormatUtil.get(a.getPublishDate()));
|
// model.setPublishDateFormat(DateFormatUtil.get(a.getPublishDate()));
|
||||||
model.setOriginal(a.getType());
|
// model.setOriginal(a.getType());
|
||||||
model.setCategory(categoryMapper.getNameById(a.getCategoryId()));
|
// model.setCategory(categoryMapper.getNameById(a.getCategoryId()));
|
||||||
String[] split = a.getTagsId().split(",");
|
// String[] split = a.getTagsId().split(",");
|
||||||
String[] tags = new String[split.length];
|
// String[] tags = new String[split.length];
|
||||||
for (int i = 0; i < split.length; i++) {
|
// for (int i = 0; i < split.length; i++) {
|
||||||
if (split[i] == null || "".equals(split[i])) {
|
// if (split[i] == null || "".equals(split[i])) {
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
tags[i] = tagMapper.getNameById(Long.parseLong(split[i]));
|
// tags[i] = tagMapper.getNameById(Long.parseLong(split[i]));
|
||||||
}
|
// }
|
||||||
model.setTags(tags);
|
// model.setTags(tags);
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -527,12 +408,12 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
*/
|
*/
|
||||||
private ArticleModel suitableTransformForAdmin(Article a) {
|
private ArticleModel suitableTransformForAdmin(Article a) {
|
||||||
ArticleModel model = simpleTransform(a);
|
ArticleModel model = simpleTransform(a);
|
||||||
model.setPublishDateFormat(DateFormatUtil.get(a.getPublishDate()));
|
// model.setPublishDateFormat(DateFormatUtil.get(a.getPublishDate()));
|
||||||
model.setUpdateDateFormat(DateFormatUtil.get(a.getUpdateDate()));
|
// model.setUpdateDateFormat(DateFormatUtil.get(a.getUpdateDate()));
|
||||||
model.setReadingNumber(a.getReadingNumber());
|
// model.setReadingNumber(a.getReadingNumber());
|
||||||
model.setOpen(a.getOpen());
|
// model.setOpen(a.getOpen());
|
||||||
model.setOriginal(a.getType());
|
// model.setOriginal(a.getType());
|
||||||
model.setSummary(null);
|
// model.setSummary(null);
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -547,15 +428,15 @@ public class ArticleServiceImpl implements ArticleService {
|
|||||||
*/
|
*/
|
||||||
private ArticleModel fullTransform(Article a) {
|
private ArticleModel fullTransform(Article a) {
|
||||||
ArticleModel model = suitableTransform(a);
|
ArticleModel model = suitableTransform(a);
|
||||||
model.setUpdateDateFormat(DateFormatUtil.get(a.getUpdateDate()));
|
// model.setUpdateDateFormat(DateFormatUtil.get(a.getUpdateDate()));
|
||||||
model.setMdContent(a.getMdContent());
|
// model.setMdContent(a.getMdContent());
|
||||||
model.setNextArticleId(a.getNextArticleId());
|
// model.setNextArticleId(a.getNextArticleId());
|
||||||
model.setNextArticleTitle(a.getNextArticleId() == -1 ? "无" : articleMapper.getTitleById(a.getNextArticleId()));
|
// model.setNextArticleTitle(a.getNextArticleId() == -1 ? "无" : articleMapper.getTitleById(a.getNextArticleId()));
|
||||||
model.setPreArticleId(a.getPreArticleId());
|
// model.setPreArticleId(a.getPreArticleId());
|
||||||
model.setPreArticleTitle(a.getPreArticleId() == -1 ? "无" : articleMapper.getTitleById(a.getPreArticleId()));
|
// model.setPreArticleTitle(a.getPreArticleId() == -1 ? "无" : articleMapper.getTitleById(a.getPreArticleId()));
|
||||||
model.setOpen(a.getOpen());
|
// model.setOpen(a.getOpen());
|
||||||
model.setUrl(a.getUrl());
|
// model.setUrl(a.getUrl());
|
||||||
model.setReadingNumber(a.getReadingNumber());
|
// model.setReadingNumber(a.getReadingNumber());
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,84 +1,91 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?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" >
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
<mapper namespace="cn.celess.blog.mapper.CategoryMapper">
|
<mapper namespace="cn.celess.blog.mapper.CategoryMapper">
|
||||||
<resultMap id="categoryResultMap" type="cn.celess.blog.entity.Category">
|
<resultMap id="categoryResultMap" type="cn.celess.blog.entity.TagCategory">
|
||||||
<id column="t_id" property="id"/>
|
<id column="t_id" property="id"/>
|
||||||
<result column="t_name" property="name"/>
|
<result column="t_name" property="name"/>
|
||||||
|
<result column="is_category" property="category"/>
|
||||||
|
<result column="is_delete" property="deleted"/>
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<insert id="insert">
|
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
||||||
insert into category (c_name, articles)
|
insert into tag_category (t_name, is_category)
|
||||||
values (#{name}, #{articles});
|
values (#{name}, true);
|
||||||
<selectKey resultType="java.lang.Long" keyProperty="id">
|
|
||||||
SELECT LAST_INSERT_ID() AS id
|
|
||||||
</selectKey>
|
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
<update id="update">
|
<update id="update">
|
||||||
update category
|
update tag_category
|
||||||
set c_name=#{name},
|
set t_name=#{name}
|
||||||
articles=#{articles}
|
where t_id = #{id}
|
||||||
where c_id = #{id}
|
and is_category = true;
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
<delete id="delete">
|
<update id="delete">
|
||||||
delete
|
update tag_category
|
||||||
from category
|
set is_delete= true
|
||||||
where c_id = #{id}
|
where t_id = #{id}
|
||||||
</delete>
|
and is_category = true;
|
||||||
|
</update>
|
||||||
|
|
||||||
<select id="findCategoryByName" resultMap="categoryResultMap">
|
<select id="findCategoryByName" resultMap="categoryResultMap">
|
||||||
select *
|
select *
|
||||||
from category
|
from tag_category
|
||||||
where c_name = #{name}
|
where t_name = #{name}
|
||||||
|
and is_category = true;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="findCategoryById" resultMap="categoryResultMap">
|
<select id="findCategoryById" resultMap="categoryResultMap">
|
||||||
select *
|
select *
|
||||||
from category
|
from tag_category
|
||||||
where c_id = #{id}
|
where t_id = #{id}
|
||||||
|
and is_category = true;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="findAll" resultMap="categoryResultMap">
|
<select id="findAll" resultMap="categoryResultMap">
|
||||||
select *
|
select *
|
||||||
from category
|
from tag_category
|
||||||
|
where is_category = true;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getAllName" resultType="java.lang.String">
|
<select id="getAllName" resultType="java.lang.String">
|
||||||
select c_name
|
select t_name
|
||||||
from category
|
from tag_category
|
||||||
|
where is_category = true;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getNameById" resultType="java.lang.String">
|
<select id="getNameById" resultType="java.lang.String">
|
||||||
select c_name
|
select t_name
|
||||||
from category
|
from tag_category
|
||||||
where c_id = #{id}
|
where is_category = true;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getIDByName" resultType="java.lang.Long">
|
<select id="getIdByName" resultType="java.lang.Long">
|
||||||
select c_id
|
select t_id
|
||||||
from category
|
from tag_category
|
||||||
where c_name = #{name}
|
where is_category = true
|
||||||
|
and t_name = #{name}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="existsByName" resultType="java.lang.Boolean">
|
<select id="existsByName" resultType="java.lang.Boolean">
|
||||||
SELECT EXISTS(SELECT * FROM category WHERE c_name = #{name})
|
SELECT EXISTS(SELECT * FROM tag_category WHERE t_name = #{name} and is_category = true)
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="existsById" resultType="java.lang.Boolean">
|
<select id="existsById" resultType="java.lang.Boolean">
|
||||||
SELECT EXISTS(SELECT * FROM category WHERE c_id = #{id})
|
SELECT EXISTS(SELECT * FROM tag_category WHERE t_id = #{id})
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getLastestCategory" resultMap="categoryResultMap">
|
<select id="getLastestCategory" resultMap="categoryResultMap">
|
||||||
select *
|
select *
|
||||||
from category
|
from tag_category
|
||||||
order by c_id desc
|
where is_category = true
|
||||||
|
order by t_id desc
|
||||||
limit 1;
|
limit 1;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="count" resultType="java.lang.Long">
|
<select id="count" resultType="java.lang.Long">
|
||||||
select count(*)
|
select count(*)
|
||||||
from category;
|
from tag_category
|
||||||
|
where is_category = true;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
<result column="a_summary" property="summary"/>
|
<result column="a_summary" property="summary"/>
|
||||||
<result column="a_md_content" property="mdContent"/>
|
<result column="a_md_content" property="mdContent"/>
|
||||||
<result column="a_url" property="url"/>
|
<result column="a_url" property="url"/>
|
||||||
<result column="a_author_id" property="authorId"/>
|
<result column="a_author_id" property="user.id"/>
|
||||||
<result column="a_is_open" property="open"/>
|
<result column="a_is_open" property="open"/>
|
||||||
<result column="a_is_original" property="type"/>
|
<result column="a_is_original" property="type"/>
|
||||||
<!-- <result column="next_a_id" property="nextArticleId"/>-->
|
<!-- <result column="next_a_id" property="nextArticleId"/>-->
|
||||||
@@ -15,11 +15,12 @@
|
|||||||
<result column="a_reading_number" property="readingNumber"/>
|
<result column="a_reading_number" property="readingNumber"/>
|
||||||
<result column="a_publish_date" property="publishDate"/>
|
<result column="a_publish_date" property="publishDate"/>
|
||||||
<result column="a_update_date" property="updateDate"/>
|
<result column="a_update_date" property="updateDate"/>
|
||||||
<association property="category" column="a_category_id" javaType="cn.celess.blog.entity.Category"
|
<result column="is_delete" property="deleted"/>
|
||||||
|
<association property="category" column="a_category_id" javaType="cn.celess.blog.entity.TagCategory"
|
||||||
resultMap="cn.celess.blog.mapper.CategoryMapper.categoryResultMap">
|
resultMap="cn.celess.blog.mapper.CategoryMapper.categoryResultMap">
|
||||||
</association>
|
</association>
|
||||||
<collection property="tags" ofType="cn.celess.blog.entity.Article"
|
<collection property="tags" ofType="cn.celess.blog.entity.TagCategory"
|
||||||
resultMap="cn.celess.blog.mapper.TagMapper.tagResultMap">
|
resultMap="cn.celess.blog.mapper.CategoryMapper.categoryResultMap">
|
||||||
</collection>
|
</collection>
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
@@ -37,6 +38,7 @@
|
|||||||
<result column="readingCount" property="readingNumber"/>
|
<result column="readingCount" property="readingNumber"/>
|
||||||
<result column="publishDate" property="publishDate"/>
|
<result column="publishDate" property="publishDate"/>
|
||||||
<result column="updateDate" property="updateDate"/>
|
<result column="updateDate" property="updateDate"/>
|
||||||
|
<result column="isDelete" property="deleted"/>
|
||||||
<association property="category" column="categoryId" javaType="cn.celess.blog.entity.Category">
|
<association property="category" column="categoryId" javaType="cn.celess.blog.entity.Category">
|
||||||
<id column="categoryId" property="id"/>
|
<id column="categoryId" property="id"/>
|
||||||
<result column="categoryName" property="name"/>
|
<result column="categoryName" property="name"/>
|
||||||
@@ -55,10 +57,9 @@
|
|||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="cn.celess.blog.entity.Article">
|
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="cn.celess.blog.entity.Article">
|
||||||
insert into article (a_author_id, a_category_id, a_md_content, a_publish_date,
|
insert into article (a_author_id, a_category_id, a_md_content, a_is_original,
|
||||||
a_summary, a_title, a_url)
|
a_summary, a_title, a_url)
|
||||||
values (#{user.id}, #{category.id}, #{mdContent}, #{publishDate},
|
values (#{user.id}, #{category.id}, #{mdContent}, #{type}, #{summary}, #{title}, #{url})
|
||||||
#{summary}, #{title}, #{url})
|
|
||||||
</insert>
|
</insert>
|
||||||
<update id="delete">
|
<update id="delete">
|
||||||
update article
|
update article
|
||||||
@@ -85,24 +86,18 @@
|
|||||||
where a_id = #{id}
|
where a_id = #{id}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
<select id="getLastestArticle" resultMap="articleResultMap" resultType="cn.celess.blog.entity.Article">
|
<select id="getLastestArticle" resultMap="articleViewResultMap" resultType="cn.celess.blog.entity.Article">
|
||||||
select *
|
select *
|
||||||
from article,
|
from articleView
|
||||||
tag_category
|
order by articleId desc
|
||||||
where tag_category.is_category = true
|
|
||||||
and article.a_category_id = tag_category.t_id
|
|
||||||
order by a_id desc
|
|
||||||
limit 1
|
limit 1
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
<select id="findArticleById" resultMap="articleResultMap">
|
<select id="findArticleById" resultMap="articleViewResultMap">
|
||||||
select *
|
select *
|
||||||
from article,
|
from articleView
|
||||||
tag_category
|
where articleId = #{id}
|
||||||
where a_id = #{id}
|
|
||||||
and tag_category.is_category = true
|
|
||||||
and article.a_category_id = tag_category.t_id
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="existsByTitle" resultType="boolean">
|
<select id="existsByTitle" resultType="boolean">
|
||||||
@@ -113,37 +108,40 @@
|
|||||||
select is_delete
|
select is_delete
|
||||||
from article
|
from article
|
||||||
WHERE a_id = #{id}
|
WHERE a_id = #{id}
|
||||||
# SELECT EXISTS(SELECT * FROM article
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="findAllByAuthorId" resultMap="articleResultMap">
|
<select id="findAllByAuthorId" resultMap="articleViewResultMap">
|
||||||
select *
|
select *
|
||||||
from article
|
from articleView
|
||||||
where a_author_id = #{authorID}
|
where authorId = #{authorID}
|
||||||
order by a_id desc
|
and isDelete = false
|
||||||
|
order by articleId desc
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="findAllByOpen" resultMap="articleResultMap">
|
<select id="findAllByOpen" resultMap="articleViewResultMap">
|
||||||
select *
|
select *
|
||||||
from article
|
from articleView
|
||||||
where a_is_open = #{isOpen}
|
where isOpen = #{isOpen}
|
||||||
order by a_id desc
|
and isDelete = false
|
||||||
|
order by articleId desc
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
<select id="getTitleById" resultType="string">
|
<select id="getTitleById" resultType="string">
|
||||||
SELECT a_title
|
SELECT title
|
||||||
from article
|
from articleView
|
||||||
where a_id = #{id}
|
where articleId = #{id}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="findAllByCategoryId" resultMap="articleResultMap">
|
<select id="findAllByCategoryId" resultMap="articleViewResultMap">
|
||||||
select a_id, a_title, a_summary
|
select *
|
||||||
from article
|
from articleView
|
||||||
where a_category_id = #{id}
|
where categoryId = #{id}
|
||||||
order by a_id desc
|
and isDelete = false
|
||||||
|
order by articleId desc
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
<select id="findAll" resultMap="articleViewResultMap">
|
<select id="findAll" resultMap="articleViewResultMap">
|
||||||
select *
|
select *
|
||||||
from articleView
|
from articleView
|
||||||
@@ -151,33 +149,11 @@
|
|||||||
order by articleId desc
|
order by articleId desc
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getSimpleInfo" resultMap="articleResultMap">
|
|
||||||
select a_id, a_summary, a_title
|
|
||||||
from article
|
|
||||||
where a_id = #{id}
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<select id="getSimpleInfoByCategory" resultMap="articleResultMap">
|
|
||||||
select a_id, a_summary, a_title
|
|
||||||
from article
|
|
||||||
where a_category_id = #{categoryId}
|
|
||||||
order by a_id desc
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<select id="getSimpleInfoByTag" resultMap="articleResultMap">
|
|
||||||
Select
|
|
||||||
a_id, a_summary, a_title
|
|
||||||
from article where a_id in
|
|
||||||
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
|
|
||||||
#{item}
|
|
||||||
</foreach>
|
|
||||||
order by a_id desc
|
|
||||||
</select>
|
|
||||||
|
|
||||||
|
|
||||||
<select id="count" resultType="long">
|
<select id="count" resultType="long">
|
||||||
select count(*)
|
select count(*)
|
||||||
from article;
|
from article;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -1,75 +1,66 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?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" >
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
<mapper namespace="cn.celess.blog.mapper.TagMapper">
|
<mapper namespace="cn.celess.blog.mapper.TagMapper">
|
||||||
<resultMap id="tagResultMap" type="cn.celess.blog.entity.Tag">
|
|
||||||
<id column="tag_id" property="id"/>
|
<resultMap id="tagResultMap" type="cn.celess.blog.entity.Tag"
|
||||||
<result column="tag_name" property="name"/>
|
extends="cn.celess.blog.mapper.CategoryMapper.categoryResultMap">
|
||||||
|
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<insert id="insert">
|
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
||||||
insert into tag (tag_name, articles)
|
insert into tag_category (t_name, is_category)
|
||||||
VALUES (#{name}, #{articles});
|
VALUES (#{name}, false);
|
||||||
<selectKey resultType="java.lang.Long" keyProperty="id">
|
|
||||||
SELECT LAST_INSERT_ID() AS id
|
|
||||||
</selectKey>
|
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
<update id="update">
|
<update id="update">
|
||||||
update tag
|
update tag_category
|
||||||
set tag_name=#{name},
|
set t_name=#{name}
|
||||||
articles=#{articles}
|
where t_id = #{id}
|
||||||
where tag_id = #{id}
|
and is_category = false;
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
<delete id="delete">
|
<update id="delete">
|
||||||
delete
|
update tag_category
|
||||||
from tag
|
set is_delete = true
|
||||||
where tag_id = #{id}
|
where t_id = #{id}
|
||||||
</delete>
|
and is_category = false;
|
||||||
|
</update>
|
||||||
|
|
||||||
<select id="findTagById" resultMap="tagResultMap">
|
<select id="findTagById" resultMap="tagResultMap">
|
||||||
select *
|
select *
|
||||||
from tag
|
from tag_category
|
||||||
where tag_id = #{id}
|
where t_id = #{id}
|
||||||
|
and is_category = false;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="findTagByName" resultMap="tagResultMap">
|
<select id="findTagByName" resultMap="tagResultMap">
|
||||||
select *
|
select *
|
||||||
from tag
|
from tag_category
|
||||||
where tag_name = #{name}
|
where t_name = #{name}
|
||||||
|
and is_category = false;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="existsByName" resultType="boolean">
|
<select id="existsByName" resultType="boolean">
|
||||||
SELECT EXISTS(SELECT * FROM tag WHERE tag_name = #{name})
|
SELECT EXISTS(SELECT * FROM tag_category WHERE t_name = #{name} and is_category = false)
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="getIDByName" resultType="long">
|
|
||||||
select tag_id
|
|
||||||
from tag
|
|
||||||
where tag_name = #{name}
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<select id="getNameById" resultType="string">
|
|
||||||
select tag_name
|
|
||||||
from tag
|
|
||||||
where tag_id = #{id}
|
|
||||||
</select>
|
|
||||||
|
|
||||||
|
|
||||||
<select id="getLastestTag" resultMap="tagResultMap">
|
<select id="getLastestTag" resultMap="tagResultMap">
|
||||||
select *
|
select *
|
||||||
from tag
|
from tag_category
|
||||||
order by tag_id desc
|
where is_category = false
|
||||||
|
order by t_id desc
|
||||||
limit 1
|
limit 1
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="findAll" resultMap="tagResultMap">
|
<select id="findAll" resultMap="tagResultMap">
|
||||||
select *
|
select *
|
||||||
from tag
|
from tag_category
|
||||||
|
where is_category = false;
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="count" resultType="long">
|
<select id="count" resultType="long">
|
||||||
select count(*)
|
select count(*)
|
||||||
from tag;
|
from tag_category
|
||||||
|
where is_category = false;;
|
||||||
</select>
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
package cn.celess.blog.mapper;
|
package cn.celess.blog.mapper;
|
||||||
|
|
||||||
import cn.celess.blog.BaseTest;
|
import cn.celess.blog.BaseTest;
|
||||||
import cn.celess.blog.entity.Article;
|
import cn.celess.blog.entity.*;
|
||||||
import cn.celess.blog.entity.Category;
|
|
||||||
import cn.celess.blog.entity.Tag;
|
|
||||||
import cn.celess.blog.entity.User;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
@@ -16,37 +13,38 @@ import static org.junit.Assert.*;
|
|||||||
public class ArticleMapperTest extends BaseTest {
|
public class ArticleMapperTest extends BaseTest {
|
||||||
@Autowired
|
@Autowired
|
||||||
ArticleMapper articleMapper;
|
ArticleMapper articleMapper;
|
||||||
|
@Autowired
|
||||||
|
TagMapper tagMapper;
|
||||||
|
@Autowired
|
||||||
|
ArticleTagMapper articleTagMapper;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void insert() {
|
public void insert() {
|
||||||
|
|
||||||
Article article = generateArticle();
|
Article article = generateArticle().getArticle();
|
||||||
|
|
||||||
articleMapper.insert(article);
|
|
||||||
assertNotNull(article.getId());
|
assertNotNull(article.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void delete() {
|
public void delete() {
|
||||||
Article article = generateArticle();
|
Article article = generateArticle().getArticle();
|
||||||
|
|
||||||
articleMapper.insert(article);
|
|
||||||
assertFalse(articleMapper.isDeletedById(article.getId()));
|
assertFalse(articleMapper.isDeletedById(article.getId()));
|
||||||
assertEquals(1, articleMapper.delete(article.getId()));
|
assertEquals(1, articleMapper.delete(article.getId()));
|
||||||
assertTrue(articleMapper.isDeletedById(article.getId()));
|
assertTrue(articleMapper.isDeletedById(article.getId()));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void update() {
|
public void update() {
|
||||||
Article article = generateArticle();
|
Article article = generateArticle().getArticle();
|
||||||
String randomText = UUID.randomUUID().toString();
|
String randomText = UUID.randomUUID().toString();
|
||||||
|
|
||||||
// 此字段不会通过insert被写入数据库而是使用插入数据的默认值 数据库中该字段默认为true
|
// 此字段不会通过insert被写入数据库而是使用插入数据的默认值 数据库中该字段默认为true
|
||||||
article.setOpen(true);
|
article.setOpen(true);
|
||||||
|
|
||||||
articleMapper.insert(article);
|
|
||||||
|
|
||||||
article.setTitle("test update " + randomText);
|
article.setTitle("test update " + randomText);
|
||||||
article.setMdContent("test update ");
|
article.setMdContent("test update ");
|
||||||
article.setSummary("test update ");
|
article.setSummary("test update ");
|
||||||
@@ -93,8 +91,7 @@ public class ArticleMapperTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateReadCount() {
|
public void updateReadCount() {
|
||||||
Article article = generateArticle();
|
Article article = generateArticle().getArticle();
|
||||||
articleMapper.insert(article);
|
|
||||||
Article articleById = articleMapper.findArticleById(article.getId());
|
Article articleById = articleMapper.findArticleById(article.getId());
|
||||||
assertEquals(Long.valueOf(0), articleById.getReadingNumber());
|
assertEquals(Long.valueOf(0), articleById.getReadingNumber());
|
||||||
articleMapper.updateReadingNumber(articleById.getId());
|
articleMapper.updateReadingNumber(articleById.getId());
|
||||||
@@ -105,17 +102,16 @@ public class ArticleMapperTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getLastestArticle() {
|
public void getLastestArticle() {
|
||||||
Article article = generateArticle();
|
Article article = generateArticle().getArticle();
|
||||||
articleMapper.insert(article);
|
|
||||||
Article lastestArticle = articleMapper.getLastestArticle();
|
Article lastestArticle = articleMapper.getLastestArticle();
|
||||||
assertNotNull(lastestArticle);
|
assertNotNull(lastestArticle);
|
||||||
assertEquals(article.getId(), lastestArticle.getId());
|
assertEquals(article.getId(), lastestArticle.getId());
|
||||||
// assertNotNull(lastestArticle.getCategory());
|
assertNotNull(lastestArticle.getCategory());
|
||||||
// assertNotEquals(0, lastestArticle.getTags().size());
|
assertNotEquals(0, lastestArticle.getTags().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Article generateArticle() {
|
private ArticleTag generateArticle() {
|
||||||
String randomText = UUID.randomUUID().toString();
|
String randomText = UUID.randomUUID().toString();
|
||||||
|
|
||||||
Article article = new Article();
|
Article article = new Article();
|
||||||
@@ -125,10 +121,20 @@ public class ArticleMapperTest extends BaseTest {
|
|||||||
article.setTitle(" unity test for article " + randomText);
|
article.setTitle(" unity test for article " + randomText);
|
||||||
article.setMdContent("# unity test for article");
|
article.setMdContent("# unity test for article");
|
||||||
article.setSummary("unity test for article");
|
article.setSummary("unity test for article");
|
||||||
|
|
||||||
|
Tag tag = tagMapper.findTagByName("随笔");
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setId(1L);
|
user.setId(1L);
|
||||||
article.setUser(user);
|
article.setUser(user);
|
||||||
article.setType(true);
|
article.setType(true);
|
||||||
return article;
|
|
||||||
|
articleMapper.insert(article);
|
||||||
|
|
||||||
|
ArticleTag articleTag = new ArticleTag();
|
||||||
|
articleTag.setArticle(article);
|
||||||
|
articleTag.setTag(tag);
|
||||||
|
articleTagMapper.insert(articleTag);
|
||||||
|
|
||||||
|
return articleTag;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user