本文主要是介绍MongoDB实战项目-绘本管理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、绘本项目需求
1.1、绘本管理管理绘本分类和绘本管理;
绘本分类需维护分类编号、分类名称、父级分类编号等字段
绘本主要维护绘本编号、绘本名称、绘本作者、绘本简介、绘本封面、绘本音频、绘本视频、绘本阅读量、绘本每页详情图片、页码、单页说明等信息。
二、模型设计
2、针对以上需求,我们对MongoDB进行简单的模型设计。
绘本分类数据模型:
{"typeNo": "A0002","typeName": "A0002","status": {"$numberLong": "0"},"createTime": {"$date": {"$numberLong": "1650958040321"}},"updateTime": {"$date": {"$numberLong": "1650958040321"}},"isDelete": 0,"_class": "com.library.picture.books.entity.PictureType"
}
绘本数据模型:
{"pictureBookNo": "B-202204270001","pictureBookName": "司马光砸缸2","pictureBookIntroduction": "司马光砸缸2司马光砸缸2222222","pictureCover": "https://library-1258527903.cos.ap-beijing.myqcloud.com/92a9be5607e945b6be9e5c3649ef91aa.JPG","pictureTypeNo": "A0002","author": "司马光2","status": {"$numberLong": "0"},"pages": [{"pageNum": 1,"pageUrl": "","pageInfo": "司马光砸缸1","_class": "com.library.picture.books.model.PageModel"},{"pageNum": 2,"pageUrl": "","pageInfo": "司马光砸缸2","_class": "com.library.picture.books.model.PageModel"},{"pageNum": 3,"pageUrl": "","pageInfo": "司马光砸缸3","_class": "com.library.picture.books.model.PageModel"}],"total": {"$numberLong": "0"},"isDelete": 0,"_class": "com.library.picture.books.entity.PictureBooksInfo","pictureAudio": null,"pictureVideo": null
}
三、代码编写
针对以上数据模型我们创建响应实体信息:
绘本分类实体信息:
/*** 绘本分类对象 picture_type** @author wanghongjie* @date 2022-04-26*/
@Data
public class PictureType extends BaseEntity {private static final long serialVersionUID = 1L;/*** 主键*/@Idprivate String id;/*** 类型编号*/@Excel(name = "类型编号")private String typeNo;/*** 分类名称*/@Excel(name = "分类名称")private String typeName;/*** 父级分类编号*/@Excel(name = "父级分类编号")private String parentTypeNo;/*** 状态*/@Excel(name = "状态")private Long status;
}
绘本信息:
/*** 绘本管理对象 picture_books_info** @author wanghongjie* @date 2022-04-26*/
@Data
public class PictureBooksInfo extends BaseEntity {private static final long serialVersionUID = 1L;/*** 主键*/@Idprivate String id;/*** 绘本编号*/@Excel(name = "绘本编号")private String pictureBookNo;/*** 绘本名称*/@Excel(name = "绘本名称")private String pictureBookName;/*** 绘本简介*/@Excel(name = "绘本简介")private String pictureBookIntroduction;/*** 绘本封面*/private String pictureCover;/*** 绘本分类*/private String pictureTypeNo;/*** 作者*/@Excel(name = "作者")private String author;/*** 阅读数量*/@Excel(name = "阅读数量")private Long readNumber;/*** 绘本音频*/@Excel(name = "绘本音频")private String pictureAudio;/*** 绘本视频*/@Excel(name = "绘本视频")private String pictureVideo;/*** 绘本状态*/@Excel(name = "绘本状态")private Long status;/*** 绘本详情页面*/private List<PageModel> pages;
}/*** 绘本页面信息** @author wanghongjie*/
@Data
public class PageModel implements Serializable {/*** 页码*/private Integer pageNum;/*** 页面图片地址*/private String pageUrl;/*** 页面描述*/private String pageInfo;
}
四、基于mongoldb完成以上模型的增删改查及其分页接口
前端脚手架采用的开源若依项目,这里分页和查询数据格式保持和原有格式一致。因增删改查接口类似,这里主要以绘本为主体进行说明;
4.1、新增接口
新增绘本,新增比较简单,只要将前端将json数据格式传递对,序列化没有问题,通过mongoTemplate.save(pictureBooksInfo, PICTURE);
方法保存即可。
@Overridepublic int insertPictureBooksInfo(PictureBooksInfo pictureBooksInfo) { mongoTemplate.save(pictureBooksInfo, PICTURE);return 1;//简单粗暴处理先
}
4.2、修改接口
修改接口较为繁琐,mongoTemplate
修改需要构建Update
对象进行修改,这里需要将需要修改的对象进行赋值操作。
@Overridepublic int updatePictureBooksInfo(PictureBooksInfo pictureBooksInfo) {Query query = new Query();query.addCriteria(Criteria.where(Wapper.getFieldName(PictureBooksInfo::getPictureBookNo)).is(pictureBooksInfo.getPictureBookNo()));Update update = new Update();update.set(Wapper.getFieldName(PictureBooksInfo::getPictureBookName), pictureBooksInfo.getPictureBookName());update.set(Wapper.getFieldName(PictureBooksInfo::getPictureAudio), pictureBooksInfo.getPictureAudio());update.set(Wapper.getFieldName(PictureBooksInfo::getPictureCover), pictureBooksInfo.getPictureCover());update.set(Wapper.getFieldName(PictureBooksInfo::getPages), pictureBooksInfo.getPages());update.set(Wapper.getFieldName(PictureBooksInfo::getAuthor), pictureBooksInfo.getAuthor());update.set(Wapper.getFieldName(PictureBooksInfo::getPictureVideo), pictureBooksInfo.getPictureVideo());update.set(Wapper.getFieldName(PictureBooksInfo::getPictureBookIntroduction), pictureBooksInfo.getPictureBookIntroduction());UpdateResult updateResult = mongoTemplate.updateFirst(query, update, PICTURE);log.info("updateResult is {}", JSON.toJSONString(updateResult));return (int) updateResult.getMatchedCount();}
这里我没有根据绘本ID进行修改数据,我是通过绘本编号来进行操作。
Wapper.getFieldName(PictureBooksInfo::getPictureVideo)
其实就是获取"pictureVideo"这个字段名称,因为手写字段名称可能出错,这里通过Wapper工具类进行序列化处理。
4.3、分页查询接口
分页查询我们要兼容数据格式,这里做了下简单处理,之前采用mybatis框架,可以使用pagehelper或原生分页插件,这里采用mongo分页方式,我们需要简单封装一下。
@Overridepublic List<PictureBooksInfo> selectPictureBooksInfoList(PictureBooksInfo pictureBooksInfo, Pageable pageable) {Query query = new Query();if (StringUtils.isNotEmpty(pictureBooksInfo.getPictureBookNo())) {query.addCriteria(Criteria.where(Wapper.getFieldName(PictureBooksInfo::getPictureBookNo)).is(pictureBooksInfo.getPictureBookNo()));}if (StringUtils.isNotEmpty(pictureBooksInfo.getPictureTypeNo())) {query.addCriteria(Criteria.where(Wapper.getFieldName(PictureBooksInfo::getPictureTypeNo)).is(pictureBooksInfo.getPictureTypeNo()));}if (StringUtils.isNotEmpty(pictureBooksInfo.getPictureBookName())) {Pattern pattern = Pattern.compile("^.*" + pictureBooksInfo.getPictureBookName() + ".*$", Pattern.CASE_INSENSITIVE);query.addCriteria(Criteria.where(Wapper.getFieldName(PictureBooksInfo::getPictureBookName)).regex(pattern));}pictureBooksInfo.setTotal(mongoTemplate.count(query, PictureType.class, PICTURE));query.with(pageable);return mongoTemplate.find(query, PictureBooksInfo.class, PICTURE);}
这里我们支持根据绘本编号、绘本类型编号等值查询和绘本名称模糊查询。因为前端分页需要获取total信息,所在这里增加了根据当前的查询条件获取总条数的查询。pictureBooksInfo.setTotal(mongoTemplate.count(query, PictureType.class, PICTURE));
。
4.4、删除和Info接口
删除和info都是基于Id进行删除和查询,这个没有什么需要注意的,看代码吧。
@Overridepublic int deletePictureBooksInfoByIds(String[] ids) {Query query = new Query();query.addCriteria(Criteria.where("id").in(ids));DeleteResult remove = mongoTemplate.remove(query, PictureBooksInfo.class, PICTURE);return (int) remove.getDeletedCount();}@Overridepublic PictureBooksInfo selectPictureBooksInfoById(String id) {return mongoTemplate.findById(id, PictureBooksInfo.class, PICTURE);}
OK,增删改查基本上OK了。
获取项目源码请关注公众号:杰子学编程https://julywhj.cn
这篇关于MongoDB实战项目-绘本管理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!