Search in sources :

Example 1 with Relation

use of com.myblog.model.Relation in project newblogback by Zephery.

the class BlogServiceImpl method updateByPrimaryKeySelective.

@Override
public Integer updateByPrimaryKeySelective(Blog blog) {
    blog.setSummary(blog.getContent().length() > 120 ? blog.getContent().substring(0, 120) : blog.getContent());
    Matcher matcher = Pattern.compile(IMGSRC_REG).matcher(blog.getContent());
    String image_url = null;
    while (matcher.find()) {
        image_url = matcher.group();
    }
    blog.setImageurl(getImageURl(image_url));
    String[] array = blog.getTagforsplit().split(",");
    List<Tag> tags = new ArrayList<>();
    for (String string : array) {
        if (tagMapper.selectByName(string) == null) {
            Tag tag = new Tag();
            tag.settName(string);
            // TODO 为了下面的relation增加标签id
            tagMapper.insert(tag);
            Tag newtag = tagMapper.selectByName(string);
            tags.add(newtag);
        } else {
            tags.add(tagMapper.selectByName(string));
        }
    }
    blog.setTags(tags.size() == 0 ? null : tags);
    blog.setContent(StringEscapeUtils.escapeSql(blog.getContent()));
    blogMapper.updateByPrimaryKeySelective(blog);
    relationMapper.deletebyblogid(blog.getBlogid());
    if (tags.size() > 0) {
        for (Tag tag : tags) {
            Relation relation = new Relation();
            relation.setBlogid(blog.getBlogid());
            relation.settId(tag.gettId());
            relationMapper.insert(relation);
        }
    }
    return blog.getBlogid();
}
Also used : Relation(com.myblog.model.Relation) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Tag(com.myblog.model.Tag)

Example 2 with Relation

use of com.myblog.model.Relation in project newblogback by Zephery.

the class BlogServiceImpl method insertblog.

@Override
public synchronized Integer insertblog(Blog blog) {
    blog.setSummary(blog.getContent().length() > 120 ? blog.getContent().substring(0, 120) : blog.getContent());
    blog.setCreateAt(DateTime.now().toString("yyyy-MM-dd"));
    Matcher matcher = Pattern.compile(IMGSRC_REG).matcher(blog.getContent());
    String image_url = null;
    while (matcher.find()) {
        image_url = matcher.group();
    }
    // http://image.wenzhihuai.com/images/20171018051437.png?imageInfo
    // {"size":14736,"format":"png","width":667,"height":387,"colorModel":"rgba"}
    blog.setImageurl(getImageURl(image_url));
    String[] array = blog.getTagforsplit().split(",");
    List<Tag> tags = new ArrayList<>();
    for (String string : array) {
        if (tagMapper.selectByName(string) == null) {
            Tag tag = new Tag();
            tag.settName(string);
            tagMapper.insertSelective(tag);
            Tag newtag = tagMapper.selectByName(string);
            tags.add(newtag);
        // tag.settId(tId);//TODO 返回主键长不起作用
        // tags.add(tag);
        } else {
            tags.add(tagMapper.selectByName(string));
        }
    }
    blog.setTags(tags.size() == 0 ? null : tags);
    blog.setHits(0);
    blog.setContent(StringEscapeUtils.escapeSql(blog.getContent()));
    blogMapper.insert(blog);
    Blog newblog = blogMapper.selectByTitle(blog.getTitle());
    if (tags.size() > 0) {
        for (Tag tag : tags) {
            Relation relation = new Relation();
            relation.setBlogid(newblog.getBlogid());
            relation.settId(tag.gettId());
            relationMapper.insert(relation);
        }
    }
    return blogMapper.selectMaxId();
}
Also used : Relation(com.myblog.model.Relation) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Tag(com.myblog.model.Tag) Blog(com.myblog.model.Blog)

Aggregations

Relation (com.myblog.model.Relation)2 Tag (com.myblog.model.Tag)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Blog (com.myblog.model.Blog)1