8000 修复 tag 重复的问题 by zbl19962 · Pull Request #8 · JamesZBL/FS-Blog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

修复 tag 重复的问题 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/me/zbl/fullstack/mapper/TagMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ public interface TagMapper extends IMyMapper<Tag> {
"GROUP BY tag_article.tag_id"
})
List<TagView> selectAllTagView();

@Select({
"SELECT id,",
"`name` FROM tag WHERE `name` = #{tagName}"
})
Tag selectTagByName(String tagName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,24 @@ public void blogAdd(BlogAddForm form) {
Integer articleId = article.getId();
// 处理 tags
String[] tags = form.getRawTags().split(",");
List<Integer> tagIds = new ArrayList<>();
for (int i = 0; i < tags.length; i++) {
Tag tag = new Tag();
tag.setName(tags[i]);
mTagMapper.insertSelective(tag);
Integer tagId = tag.getId();
tagIds.add(tagId);
}
for (Integer tagId : tagIds) {
TagArticle tagArticle = new TagArticle();
tagArticle.setTagId(tagId);
tagArticle.setArticleId(articleId);
mTagArticleMapper.insertSelective(tagArticle);
for (String item : tags) {
Tag expected = mTagMapper.selectTagByName(item);
if (null != expected) {
Integer id = expected.getId();
TagArticle tagArticle = new TagArticle();
tagArticle.setTagId(id);
tagArticle.setArticleId(articleId);
mTagArticleMapper.insertSelective(tagArticle);
} else {
Tag tag = new Tag();
tag.setName(item);
mTagMapper.insertSelective(tag);
Integer tagId = tag.getId();
TagArticle tagArticle = new TagArticle();
tagArticle.setTagId(tagId);
tagArticle.setArticleId(articleId);
mTagArticleMapper.insertSelective(tagArticle);
}
}
}

Expand Down
0