@Overridepublic boolean batchAddTag(String tags) throws Exception {//将字符串转成数组String[] split = tags.split(",");List tagList = new ArrayList<>();//循环数据,放入List集合中for (String str : split) {//过滤:判断数据库中是否有存在的标签,没有就添加,有就不添加if (findByTagName(str) == null) {Tag tag = new Tag();tag.setTagName(str);tagList.add(tag);}}//我们限制下添加的数量,一次最多添加10个标签if (tagList == null || tagList.size() == 0 || tagList.size() > 10){throw new Exception ("标签已存在或超过添加标签的限制!");}boolean isStatus = tagMapper.batchAddTag(tagList);return isStatus;}
批量删除: 所谓的批量删除就是前端传来多个数据的id,我们根据id去表中把数据删除,和普通的删除的区别就是多个id删除数据罢了 。首先接收到id,和批量添加一样,对id进行解析,然后存到新new的对象中,把所有的对象都存到List集合中进行统一删除 。
@Overridepublic boolean batchDelTag(String ids){//将id字符串转成数组String[] split = ids.split(",");List idList = new ArrayList<>();for (String id : split) {Tag tag = new Tag();tag.setId(Integer.valueOf(id));idList.add(tag);}boolean isStatus = tagMapper.deleteBatch(idList);return isStatus;}
完整代码如下:
package com.blog.personalblog.service.Impl;import com.blog.personalblog.config.page.PageRequest;import com.blog.personalblog.entity.Tag;import com.blog.personalblog.mapper.TagMapper;import com.blog.personalblog.service.TagService;import com.github.pagehelper.PageHelper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.ArrayList;import java.util.List;/*** @author: SuperMan* @create: 2021-11-28*/@Servicepublic class TagServiceImpl implements TagService {@AutowiredTagMapper tagMapper;@Overridepublic List getTagPage(PageRequest pageRequest) {int pageNum = pageRequest.getPageNum();int pageSize = pageRequest.getPageSize();PageHelper.startPage(pageNum,pageSize);List tagList = tagMapper.getTagPage();return tagList;}@Overridepublic int saveTag(Tag tag) {return tagMapper.createTag(tag);}@Overridepublic int updateTag(Tag tag) {return tagMapper.updateTag(tag);}@Overridepublic void deleteTag(Integer tagId) {tagMapper.deleteTag(tagId);}@Overridepublic boolean batchAddTag(String tags) throws Exception {//将字符串转成数组String[] split = tags.split(",");List tagList = new ArrayList<>();//循环数据,放入List集合中for (String str : split) {//过滤:判断数据库中是否有存在的标签,没有就添加,有就不添加if (findByTagName(str) == null) {Tag tag = new Tag();tag.setTagName(str);tagList.add(tag);}}//我们限制下添加的数量,一次最多添加10个标签if (tagList == null || tagList.size() == 0 || tagList.size() > 10){throw new Exception ("标签已存在或超过添加标签的限制!");}boolean isStatus = tagMapper.batchAddTag(tagList);return isStatus;}@Overridepublic boolean batchDelTag(String ids){//将id字符串转成数组String[] split = ids.split(",");List idList = new ArrayList<>();for (String id : split) {Tag tag = new Tag();tag.setId(Integer.valueOf(id));idList.add(tag);}boolean isStatus = tagMapper.deleteBatch(idList);return isStatus;}@Overridepublic Tag findByTagName(String tagName) {Tag tag = new Tag();tag.setTagName(tagName);Tag byTagName = tagMapper.getByTagName(tag);return byTagName;}}
4、数据库查询接口实现
新建一个接口:.java
package com.blog.personalblog.mapper;import com.blog.personalblog.entity.Tag;import org.springframework.stereotype.Repository;import java.util.List;/*** @author: SuperMan* @create: 2021-11-28*/@Repositorypublic interface TagMapper {/*** 创建* @param tag* @return*/int createTag(Tag tag);/*** 修改* @param tag* @return*/int updateTag(Tag tag);/*** 分类列表(分页)* @return*/List getTagPage();/*** 删除* @param id*/void deleteTag(Integer id);/*** 批量添加标签* @param strings* @return*/boolean batchAddTag(List strings);/*** 批量删除标签* @param ids*/boolean deleteBatch(List ids);/*** 根据标签查找该对象* @param tag* @return*/Tag getByTagName(Tag tag);}
- 盘点Spring/Boot的那些常用扩展点
- 三万字盘点Spring/Boot的那些常用扩展点
- 【独立版】智慧城市同城V4_2.2.7全开源全插件VUE版
- 【独立版】智慧城市同城V4_2.1.4全开源全插件VUE版
- vue body的背景图片属性设置
- springboot集成redis模拟手机发送验证码进行验证
- Spring Cloud应用接入华为云微服务CSE
- 基于Spring Boot 2.0的IoT应用集成和使用CSE实践
- Spring Boot 2.0集成和使用CSE
- Vue 不睡觉教程1-从最土开始