增加1对多

master
ck 5 years ago
parent 64bb23797d
commit bdfe500d27

@ -0,0 +1,12 @@
package com.jingcheng.cms.mapper;
import com.jingcheng.cms.model.ArticleCategory;
import com.jingcheng.cms.util.CommonMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ArticleCategoryMapper extends CommonMapper<ArticleCategory> {
int addArticleCategory(List<ArticleCategory> articleCategoryList);
}

@ -14,7 +14,7 @@ public interface ArticleMapper extends CommonMapper<Article> {
int addArticle(Article article);
List<ArticleVo> getArticleListByCondition(@Param("title") String title,
@Param("firstCategory") Long firstCategory,
@Param("firstCategoryName") String firstCategoryName,
@Param("secondCategory") Long secondCategory,
@Param("fileType") Integer fileType);
List<Category> getFirstCategory();
@ -23,6 +23,6 @@ public interface ArticleMapper extends CommonMapper<Article> {
List<ArticleVo> searchArticleListByCondition(@Param("param") String param,
@Param("keyword") String keyword,
@Param("firstCategory") Long firstCategory,
@Param("firstCategoryName") String firstCategoryName,
@Param("secondCategory") Long secondCategory);
}

@ -0,0 +1,15 @@
package com.jingcheng.cms.model;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("article_category")
public class ArticleCategory extends BaseEntity{
private Long articleId;
private Long categoryId;
}

@ -3,9 +3,11 @@ package com.jingcheng.cms.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jingcheng.cms.constants.Constants;
import com.jingcheng.cms.mapper.ArticleCategoryMapper;
import com.jingcheng.cms.mapper.ArticleMapper;
import com.jingcheng.cms.mapper.CategoryMapper;
import com.jingcheng.cms.model.Article;
import com.jingcheng.cms.model.ArticleCategory;
import com.jingcheng.cms.model.Category;
import com.jingcheng.cms.service.ArticleSerive;
import com.jingcheng.cms.util.AjaxResult;
@ -17,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
@ -27,6 +30,8 @@ public class ArticleSerivceImpl implements ArticleSerive {
private ArticleMapper articleMapper;
@Resource
private CategoryMapper categoryMapper;
@Resource
private ArticleCategoryMapper articleCategoryMapper;
@Override
@ -34,7 +39,7 @@ public class ArticleSerivceImpl implements ArticleSerive {
AjaxResult ajaxResult = new AjaxResult();
Long id = jsonObject.getLong("id");
String title = jsonObject.getString("title");
Long firstCategory = jsonObject.getLong("firstCategory");
JSONArray firstCategory = jsonObject.getJSONArray("firstCategory");
Long secondCategory = jsonObject.getLong("secondCategory");
String keyword = jsonObject.getString("keyword");
Integer fileType = jsonObject.getInteger("fileType");
@ -76,16 +81,16 @@ public class ArticleSerivceImpl implements ArticleSerive {
//新增
Article article = new Article();
article.setTitle(title);
article.setFirstCategory(firstCategory);
article.setSecondCategory(secondCategory);
article.setKeyword(keyword);
article.setFileType(fileType);
article.setState(state);
article.setContent(content);
article.setDescribe(describe);
article.setVideoUrl(videoUrl);
article.setSecondCategory(secondCategory);
article.setIsRecommend(isRecommend);
articleMapper.addArticle(article);
id = article.getId();
ajaxResult.setRetmsg("成功新增文章");
}else {
//修改
@ -100,8 +105,6 @@ public class ArticleSerivceImpl implements ArticleSerive {
criteria.andEqualTo("id",id);
article = new Article();
article.setTitle(title);
article.setFirstCategory(firstCategory);
article.setSecondCategory(secondCategory);
article.setKeyword(keyword);
article.setFileType(fileType);
article.setState(state);
@ -110,8 +113,26 @@ public class ArticleSerivceImpl implements ArticleSerive {
article.setVideoUrl(videoUrl);
article.setIsRecommend(isRecommend);
articleMapper.updateByExampleSelective(article,example);
ArticleCategory delArticleCategory = new ArticleCategory();
delArticleCategory.setArticleId(id);
//清除之前的1级分类
if (null !=firstCategory && firstCategory.size()>0){
articleCategoryMapper.delete(delArticleCategory);
}
ajaxResult.setRetmsg("成功修改文章");
}
if (null !=firstCategory && firstCategory.size()>0){
List<Long> firstCategoryIdList = JSONObject.parseArray(firstCategory.toJSONString(), Long.class);
List<ArticleCategory> articleCategoryList = new ArrayList<>();
for (Long categoryId:firstCategoryIdList){
ArticleCategory articleCategory = new ArticleCategory();
articleCategory.setArticleId(id);
articleCategory.setCategoryId(categoryId);
articleCategory.setState(Constants.STATE_VALID);
articleCategoryList.add(articleCategory);
}
articleCategoryMapper.addArticleCategory(articleCategoryList);
}
return ajaxResult;
}
@ -127,7 +148,12 @@ public class ArticleSerivceImpl implements ArticleSerive {
Long firstCategory = jsonObject.getLong("firstCategory");
Long secondCategory = jsonObject.getLong("secondCategory");
Integer fileType = jsonObject.getInteger("fileType");
List<ArticleVo> CategoryList = articleMapper.getArticleListByCondition(title,firstCategory,secondCategory,fileType);
Category category = categoryMapper.selectByPrimaryKey(firstCategory);
String firstCategoryName = null ;
if (category != null ){
firstCategoryName = "%" + category.getName() + "%" ;
}
List<ArticleVo> CategoryList = articleMapper.getArticleListByCondition(title,firstCategoryName,secondCategory,fileType);
JSONObject pageJson = PageUtils.page(CategoryList,pageNum,pageSize);
return pageJson;
}
@ -159,7 +185,12 @@ public class ArticleSerivceImpl implements ArticleSerive {
}
Long firstCategory = jsonObject.getLong("firstCategory");
Long secondCategory = jsonObject.getLong("secondCategory");
List<ArticleVo> CategoryList = articleMapper.searchArticleListByCondition(param,keyword,firstCategory,secondCategory);
Category category = categoryMapper.selectByPrimaryKey(firstCategory);
String firstCategoryName = null ;
if (category != null ){
firstCategoryName = "%" + category.getName() + "%" ;
}
List<ArticleVo> CategoryList = articleMapper.searchArticleListByCondition(param,keyword,firstCategoryName,secondCategory);
JSONObject pageJson = PageUtils.page(CategoryList,pageNum,pageSize);
return pageJson;
}
@ -184,6 +215,11 @@ public class ArticleSerivceImpl implements ArticleSerive {
article = new Article();
article.setState(Constants.DISABLE);
articleMapper.updateByExampleSelective(article,example);
ArticleCategory delArticleCategory = new ArticleCategory();
delArticleCategory.setArticleId(id);
//清除之前的1级分类
articleCategoryMapper.delete(delArticleCategory);
}
ajaxResult.setRetmsg("批量删除成功");
return ajaxResult;
@ -198,13 +234,15 @@ public class ArticleSerivceImpl implements ArticleSerive {
ajaxResult.setRetmsg("未找到该文章");
return ajaxResult;
}else {
Long firstCategoryId = article.getFirstCategory();
Long secondCategoryId = article.getSecondCategory();
Category firstCategory = categoryMapper.selectByPrimaryKey(firstCategoryId);
ArticleCategory findArticleCategory = new ArticleCategory();
findArticleCategory.setArticleId(id);
findArticleCategory.setState(Constants.STATE_VALID);
List<ArticleCategory> firstCategoryList = articleCategoryMapper.select(findArticleCategory);
Category secondCategory = categoryMapper.selectByPrimaryKey(secondCategoryId);
ArticleVo articleVo = new ArticleVo();
articleVo.setTitle(article.getTitle());
articleVo.setFirstCategory(firstCategoryId);
// articleVo.setFirstCategory(firstCategoryId);
articleVo.setSecondCategory(secondCategoryId);
articleVo.setContent(article.getContent());
articleVo.setFileType(article.getFileType());
@ -214,15 +252,26 @@ public class ArticleSerivceImpl implements ArticleSerive {
articleVo.setCreateDateTime(article.getCreateDateTime());
articleVo.setVideoUrl(article.getVideoUrl());
articleVo.setIsRecommend(article.getIsRecommend());
if (null != firstCategory ){
articleVo.setFirstCategoryName(firstCategory.getName());
articleVo.setFirstLevel(firstCategory.getLevel());
articleVo.setFirstSortNum(firstCategory.getSortNum());
if (null != firstCategoryList && firstCategoryList.size()>0 ){
List<String> firstCategoryNameList = new ArrayList<>();
List<String> firstSortNumList = new ArrayList<>();
for (ArticleCategory articleCategory:firstCategoryList){
Category category = categoryMapper.selectByPrimaryKey(articleCategory.getCategoryId());
firstCategoryNameList.add(category.getName());
if (category.getSortNum()!=null){
firstSortNumList.add(String.valueOf(category.getSortNum()));
}else {
firstSortNumList.add("");
}
}
String firstCategoryName = String.join(",", firstCategoryNameList);
String firstSortNum = String.join(",", firstSortNumList);
articleVo.setFirstCategoryName(firstCategoryName);
articleVo.setFirstSortNum(firstSortNum);
}
if (null != secondCategory ){
articleVo.setSecondCategoryName(secondCategory.getName());
articleVo.setSecondLevel(secondCategory.getLevel());
articleVo.setFirstSortNum(secondCategory.getSortNum());
articleVo.setSecondSortNum(secondCategory.getSortNum());
}
ajaxResult.setData(articleVo);
}

@ -11,7 +11,7 @@ public class ArticleVo extends Article {
private String secondCategoryName;
private Integer firstSortNum;
private String firstSortNum;
private Integer secondSortNum;

@ -0,0 +1,14 @@
<?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">
<mapper namespace="com.jingcheng.cms.mapper.ArticleCategoryMapper">
<insert id="addArticleCategory" parameterType="com.jingcheng.cms.model.ArticleCategory">
INSERT INTO article_category
(`article_id`,`category_id`,`state`)
values
<foreach collection ="list" item="item" separator =",">
(#{item.articleId}, #{item.categoryId}, #{item.state})
</foreach >
</insert>
</mapper>

@ -2,20 +2,30 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jingcheng.cms.mapper.ArticleMapper">
<insert id="addArticle" parameterType="com.jingcheng.cms.model.Article">
<insert id="addArticle" useGeneratedKeys="true" keyProperty="id" parameterType="com.jingcheng.cms.model.Article">
INSERT INTO article(`title`,`first_category`,`second_category`,`keyword`,`file_type`,`content`,`state`,`describe`,`video_url`,`is_recommend`)
values (#{title},#{firstCategory},#{secondCategory},#{keyword},#{fileType},#{content},#{state},#{describe},#{videoUrl},#{isRecommend})
</insert>
<select id="getArticleListByCondition" resultType="com.jingcheng.cms.vo.ArticleVo">
select * from (
select ar.`id`,ar.`title`,c1.`name` as `first_category_name`,c2.`name` as `second_category_name`,
ar.`keyword`,ar.`file_type`,ar.`content`,ar.`create_date_time`,ar.`update_date_time`,ar.`state`,
c1.level as `first_level`,c2.level as `second_level`,c1.`sort_num` as `first_sort_num`,c2.`sort_num` as `second_sort_num`,
ar.`keyword`,ar.`file_type`,ar.`describe`,ar.`video_url`,ar.`is_recommend`
FROM article ar
left join category c1
left join
(
select ac.id,ac.article_id,ac.category_id,ac.state,GROUP_CONCAT(ca.name) as `name` ,GROUP_CONCAT(ca.sort_num) as `sort_num`,ca.level
FROM article_category ac
left join category ca
on ac.category_id = ca.id
WHERE ac.state = 1
GROUP BY article_id
)
c1
on
ar.first_category = c1.id and c1.level = 1
ar.id = c1.article_id and c1.level = 1
left join category c2
on
ar.second_category = c2.id and c2.level = 2
@ -24,9 +34,6 @@
<if test="title != null and title != ''">
and ar.title like #{title}
</if>
<if test="firstCategory != null ">
and ar.first_category = #{firstCategory}
</if>
<if test="secondCategory != null ">
and ar.second_category = #{secondCategory}
</if>
@ -34,6 +41,12 @@
and ar.file_type = #{fileType}
</if>
ORDER BY is_recommend Desc
)
big
where 1= 1
<if test="firstCategoryName != null and firstCategoryName != ''">
and big.`first_category_name` like #{firstCategoryName}
</if>
</select>
<select id="getFirstCategory" resultType="com.jingcheng.cms.model.Category">
@ -62,14 +75,24 @@
</select>
<select id="searchArticleListByCondition" resultType="com.jingcheng.cms.vo.ArticleVo">
select * from (
select ar.`id`,ar.`title`,c1.`name` as `first_category_name`,c2.`name` as `second_category_name`,
ar.`keyword`,ar.`file_type`,ar.`content`,ar.`create_date_time`,ar.`update_date_time`,ar.`state`,
c1.level as `first_level`,c2.level as `second_level`,c1.`sort_num` as `first_sort_num`,c2.`sort_num` as `second_sort_num`,
ar.`keyword`,ar.`file_type`,ar.`describe`,ar.`video_url`,ar.`is_recommend`
FROM article ar
left join category c1
left join
(
select ac.id,ac.article_id,ac.category_id,ac.state,GROUP_CONCAT(ca.name) as `name` ,GROUP_CONCAT(ca.sort_num) as `sort_num`,ca.level
FROM article_category ac
left join category ca
on ac.category_id = ca.id
WHERE ac.state = 1
GROUP BY article_id
)
c1
on
ar.first_category = c1.id and c1.level = 1
ar.id = c1.article_id and c1.level = 1
left join category c2
on
ar.second_category = c2.id and c2.level = 2
@ -81,13 +104,15 @@
<if test="keyword != null and keyword != ''">
and ar.keyword like #{keyword}
</if>
<if test="firstCategory != null ">
and ar.first_category = #{firstCategory}
</if>
<if test="secondCategory != null ">
and ar.second_category = #{secondCategory}
</if>
and ar.state = 1
ORDER BY is_recommend Desc
)big
where 1 = 1
<if test="firstCategoryName != null and firstCategoryName != ''">
and big.`first_category_name` like #{firstCategoryName}
</if>
</select>
</mapper>
Loading…
Cancel
Save