一、前言
对于频繁读写的功能,例如商品的浏览记录,Mongodb中的添加、删除、查询可以很好的解决。Mongodb是为快速开发互联网Web应用而构建的数据库系统,其数据模型和持久化策略就是为了构建高 读/写 吞吐量和高自动灾备伸缩性的系统。
二、Mongodb的安装和使用(windows系统下)
1、下载Mongodb安装包,下载地址:https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.2.21-signed.msi
2、解压压缩包
3、在安装目录下,创建datadb 和 datalog
4、在安装目录下创建mongod.cfg
systemLog: destination: file path: D:mongodb-ssl-3.2.21datalogmongod.log storage: dbPath: D:mongodb-ssl-3.2.21datadb
5、安装服务(管理员权限下)
在c盘中打开管理员权限命令框
右击,选择以管理员身份运行,进入到命令框,进入Mongodb安装目录,执行命令:mongod.exe --config "D:mongodb-ssl-3.2.21mongod.cfg" --install
安装服务完成后,启动服务,执行命令:net start MongoDB
Mongodb的相关命令
启动服务:net start MongoDB
关闭服务:net stop MongoDB
移除服务:D:mongodb-ssl-3.2.21binmongod.exe --remove
6、下载客户端程序,下载地址:https://download.robomongo.org/1.2.1/windows/robo3t-1.2.1-windows-x86_64-3e50a65.zip
7、解压到指定目录,打开robo3t.exe并连接到localhost:27017
8、Spring Data Mongodb
和Spring Data Elasticsearch类似,Spring Data Mongodb是Spring提供的一种以Spring Data风格来操作数据存储的方式,它可以避免编写大量的样板代码。
9、常用注解
· @Document:表示映射到Mongodb文档上的领域对象
· @Id:表示某个域为ID域
·@Indexed:表示某个字段为Mongodb的索引字段
10、Spring Data方式的数据操作,继承MongodbRepository接口获得常用的数据操作方法
衍生查询
在接口中直接指定查询方法名称,不需要实现,例如:根据会员id按时间倒序获取浏览记录
三、整合Mongodb实现文档操作
1、在pom.xml中添加相关依赖
<!--Mongodb相关依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
2、修改Springboot配置文件application.yml,添加Mongodb相关配置节点
mongodb: host: localhost # Mongodb的连接地址 port: 27017 # Mongodb的连接端口 database: mall-port # Mongodb的连接数据库
3、添加mongodb项目结构文件
4、创建会员浏览记录文档对象MemberReadHistory,注:文档对象的ID域添加@Id注解,需要检索的字段添加@Indexed注解
package com.zzb.test.admin.mongodb.document; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; import java.util.Date; /** * 用户商品的浏览记录 * Created by zzb on 2019/12/10 10:00 */ @Document public class MemberReadHistory { @Id private String id; @Indexed private Long memberId; private String memberNickname; private String memberIcon; @Indexed private Long productId; private String productName; private String productPic; private Date createTime; public String getId() { return id; } public void setId(String id) { this.id = id; } public Long getMemberId() { return memberId; } public void setMemberId(Long memberId) { this.memberId = memberId; } public String getMemberNickname() { return memberNickname; } public void setMemberNickname(String memberNickname) { this.memberNickname = memberNickname; } public String getMemberIcon() { return memberIcon; } public void setMemberIcon(String memberIcon) { this.memberIcon = memberIcon; } public Long getProductId() { return productId; } public void setProductId(Long productId) { this.productId = productId; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductPic() { return productPic; } public void setProductPic(String productPic) { this.productPic = productPic; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
5、创建操作Mongodb的Repository
package com.zzb.test.admin.mongodb.repository; import com.zzb.test.admin.mongodb.document.MemberReadHistory; import org.springframework.data.mongodb.repository.MongoRepository; import java.util.List; /** * 操作Mongodb的Repository * Created by zzb on 2019/12/10 10:01 */ public interface MemberReadHistoryRepository extends MongoRepository<MemberReadHistory,String> { /** * 根据会员id按时间倒序获取浏览记录 * @param memberId * @return */ List<MemberReadHistory> findByMemberIdOrderByCreateTimeDesc(Long memberId); }
6、在service包中创建会员浏览记录管理的Service,MemberReadHistoryService
package com.zzb.test.admin.service; import com.zzb.test.admin.mongodb.document.MemberReadHistory; import java.util.List; /** * 会员浏览记录管理Service * Created by zzb on 2019/12/10 10:42 */ public interface MemberReadHistoryService { /** * 生成浏览记录 * @param memberReadHistory * @return */ int create(MemberReadHistory memberReadHistory); /** * 批量删除浏览记录 * @param ids * @return */ int delete(List<String> ids); /** * 分页获取用户浏览记录 * @param memberId * @return */ List<MemberReadHistory> listPage(Long memberId,Integer pageNum,Integer pageSize); }
7、在impl包下创建其实现类MemberReadHistoryServiceImpl
package com.zzb.test.admin.service.impl; import com.github.pagehelper.PageHelper; import com.zzb.test.admin.mongodb.document.MemberReadHistory; import com.zzb.test.admin.mongodb.repository.MemberReadHistoryRepository; import com.zzb.test.admin.service.MemberReadHistoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * MemberReadHistoryService的实现类 * Created by zzb on 2019/12/10 10:46 */ @Service public class MemberReadHistoryServiceImpl implements MemberReadHistoryService { @Autowired private MemberReadHistoryRepository memberReadHistoryRepository; @Override public int create(MemberReadHistory memberReadHistory) { memberReadHistory.setId(null); memberReadHistory.setCreateTime(new Date()); memberReadHistoryRepository.save(memberReadHistory); return 1; } @Override public int delete(List<String> ids) { List<MemberReadHistory> memberReadHistoryList = new ArrayList<>(ids.size()); for (String id:ids) { MemberReadHistory mrh = new MemberReadHistory(); mrh.setId(id); memberReadHistoryList.add(mrh); } memberReadHistoryRepository.deleteAll(memberReadHistoryList); return ids.size(); } @Override public List<MemberReadHistory> listPage(Long memberId,Integer pageNum,Integer pageSize) { PageHelper.startPage(pageNum,pageSize); return memberReadHistoryRepository.findByMemberIdOrderByCreateTimeDesc(memberId); } }
8、在controller包下新建会员商品浏览记录管理类MemberReadHistoryController
package com.zzb.test.admin.controller; import com.zzb.test.admin.common.CommonPage; import com.zzb.test.admin.common.CommonResult; import com.zzb.test.admin.mongodb.document.MemberReadHistory; import com.zzb.test.admin.service.MemberReadHistoryService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.List; /** * 会员商品浏览记录管理Controller * Created by zzb on 2019/12/10 17:19 */ @Controller @Api(tags = "MemberReadHistoryController", description = "会员商品浏览记录管理") public class MemberReadHistoryController { @Autowired private MemberReadHistoryService memberReadHistoryService; @ApiOperation("创建浏览记录") @RequestMapping(value = "/admin/ums/createReadHistory", method = RequestMethod.POST) @ResponseBody public CommonResult create(@RequestBody MemberReadHistory memberReadHistory){ if (memberReadHistoryService.create(memberReadHistory)>0) { return CommonResult.success("添加记录成功"); } return CommonResult.failed("添加记录失败"); } @ApiOperation("批量删除会员的浏览记录") @RequestMapping(value = "/admin/ums/deleteReadHistory",method = RequestMethod.POST) @ResponseBody public CommonResult delete(@RequestParam("ids") List<String> ids){ if (memberReadHistoryService.delete(ids)>0) { return CommonResult.success("删除成功"); } return CommonResult.failed("删除失败"); } @ApiOperation("分页查询会员的浏览记录") @RequestMapping(value = "/admin/ums/listPageReadHistory",method = RequestMethod.GET) @ResponseBody public CommonResult<CommonPage<MemberReadHistory>> listPage(@RequestParam("memberId") Long memberId, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){ List<MemberReadHistory> memberReadHistoryList = memberReadHistoryService.listPage(memberId,pageNum,pageSize); return CommonResult.success(CommonPage.restPage(memberReadHistoryList)); } }
四、测试
1、登录授权,验证,详见https://www.cnblogs.com/zzb-yp/p/11899880.html
2、访问接口 /admin/ums/createReadHistory添加数据到Mongodb数据库
2、查询刚添加的数据
项目github地址:https://github.com/18372561381/shoptest