探花交友_第2章-完善個人資訊與MongoDB入門
文章目錄
-
- 探花交友_第2章-完善個人資訊與MongoDB入門
- 1、完善個人資訊
-
- 1.1、圖檔上傳
-
- 1.1.1、圖檔存儲解決方案
- 1.1.2、阿裡雲OSS存儲
-
- 1.1.2.1、什麼是OSS服務?
- 1.1.2.2、購買服務
- 1.1.2.3、建立Bucket
- 1.1.2.4、建立使用者
- 1.1.3、導入依賴
- 1.1.4、OSS配置
- 1.1.5、PicUploadService
- 1.1.6、PicUploadController
- 1.1.7、測試
- 1.2、人臉識别
-
- 1.2.1、使用說明
- 1.2.2、安裝jar到本地倉庫
- 1.2.3、開始使用
- 1.2.4、測試
- 1.3、實作完善個人資訊
-
- 1.3.1、UserInfoMapper
- 1.3.2、UserInfoService
- 1.3.3、UserInfoController
- 1.4.4、測試
- 2、校驗token
-
- 2.1、UserController
- 2.2、UserService
- 2.3、測試
- 3、MongoDB快速入門
-
- 3.1、MongoDB簡介
- 3.2、通過docker安裝MongoDB
- 3.3、MongoDB基本操作
-
- 3.3.1、基本概念
- 3.3.2、資料庫以及表的操作
- 3.3.3、新增資料
- 3.3.4、更新資料
- 3.3.5、删除資料
- 3.3.6、查詢資料
- 3.4、索引
- 3.5、執行計劃
- 3.6、UI用戶端工具
- 4、SpringBoot整合MongoDB
-
- 4.1、導入依賴
- 4.2、編寫application.properties配置檔案
- 4.3、編寫實體
- 4.4、編寫dao
- 4.5、編寫啟動類
- 4.6、編寫單元測試
- 完善個人資訊
- 阿裡雲OSS服務應用
- 人臉識别
- MongoDB快速入門
- SpringBoot整合MongoDB
1、完善個人資訊
使用者在首次登入時需要完善個人資訊,包括性别、昵稱、生日、城市、頭像等。
其中,頭像資料需要做圖檔上傳,這裡采用阿裡雲的OSS服務作為我們的圖檔伺服器,并且對頭像要做人臉識别,非人臉照片不得上傳。
1.1、圖檔上傳
1.1.1、圖檔存儲解決方案
實作圖檔上傳服務,需要有存儲的支援,那麼我們的解決方案将以下幾種:
- 直接将圖檔儲存到服務的硬碟
- 優點:開發便捷,成本低
- 缺點:擴容困難
- 使用分布式檔案系統進行存儲
- 優點:容易實作擴容
- 缺點:開發複雜度稍大(有成熟的産品可以使用,比如:FastDFS)
- 使用nfs做存儲
- 優點:開發較為便捷
- 缺點:需要有一定的運維知識進行部署和維護
- 使用第三方的存儲服務
- 優點:開發簡單,擁有強大功能,免維護
- 缺點:付費
在本套課程中選用阿裡雲的OSS服務進行圖檔存儲。
1.1.2、阿裡雲OSS存儲
流程:
1.1.2.1、什麼是OSS服務?
位址:https://www.aliyun.com/product/oss
1.1.2.2、購買服務
使用第三方服務最大的缺點就是需要付費,下面,我們看下如何購買開通服務。
購買下行流量包: (不購買也可以使用,按照流量付費)
說明:OSS的上行流量是免費的,但是下行流量是需要購買的。
1.1.2.3、建立Bucket
使用OSS,首先需要建立Bucket,Bucket翻譯成中文是水桶的意思,把存儲的圖檔資源看做是水,想要盛水必須得有桶,就是這個意思了。
進入控制台,https://oss.console.aliyun.com/overview
選擇Bucket後,即可看到對應的資訊,如:url、消耗流量等 :
檔案管理:
檢視檔案:
1.1.2.4、建立使用者
建立使用者的方式與短信接口中的方式一樣,需要設定oss權限。
1.1.3、導入依賴
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>
1.1.4、OSS配置
aliyun.properties:
aliyun.endpoint = http://oss-cn-zhangjiakou.aliyuncs.com
aliyun.accessKeyId = ***********
aliyun.accessKeySecret = ***************
aliyun.bucketName= tanhua-dev
aliyun.urlPrefix=http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/
AliyunConfig:
package com.tanhua.sso.config;
import com.aliyun.oss.OSSClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:aliyun.properties")
@ConfigurationProperties(prefix = "aliyun")
@Data
public class AliyunConfig {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private String urlPrefix;
@Bean
public OSSClient oSSClient() {
return new OSSClient(endpoint, accessKeyId, accessKeySecret);
}
}
1.1.5、PicUploadService
package com.tanhua.sso.service;
import com.aliyun.oss.OSSClient;
import com.tanhua.sso.config.AliyunConfig;
import com.tanhua.sso.vo.PicUploadResult;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
@Service
public class PicUploadService {
// 允許上傳的格式
private static final String[] IMAGE_TYPE = new String[]{".bmp", ".jpg",
".jpeg", ".gif", ".png"};
@Autowired
private OSSClient ossClient;
@Autowired
private AliyunConfig aliyunConfig;
public PicUploadResult upload(MultipartFile uploadFile) {
PicUploadResult fileUploadResult = new PicUploadResult();
//圖檔做校驗,對字尾名
boolean isLegal = false;
for (String type : IMAGE_TYPE) {
if (StringUtils.endsWithIgnoreCase(uploadFile.getOriginalFilename(),
type)) {
isLegal = true;
break;
}
}
if (!isLegal) {
fileUploadResult.setStatus("error");
return fileUploadResult;
}
// 檔案新路徑
String fileName = uploadFile.getOriginalFilename();
String filePath = getFilePath(fileName);
// 上傳到阿裡雲
try {
// 目錄結構:images/2018/12/29/xxxx.jpg
ossClient.putObject(aliyunConfig.getBucketName(), filePath, new
ByteArrayInputStream(uploadFile.getBytes()));
} catch (Exception e) {
e.printStackTrace();
//上傳失敗
fileUploadResult.setStatus("error");
return fileUploadResult;
}
// 上傳成功
fileUploadResult.setStatus("done");
fileUploadResult.setName(this.aliyunConfig.getUrlPrefix() + filePath);
fileUploadResult.setUid(String.valueOf(System.currentTimeMillis()));
return fileUploadResult;
}
private String getFilePath(String sourceFileName) {
DateTime dateTime = new DateTime();
return "images/" + dateTime.toString("yyyy")
+ "/" + dateTime.toString("MM") + "/"
+ dateTime.toString("dd") + "/" + System.currentTimeMillis() +
RandomUtils.nextInt(100, 9999) + "." +
StringUtils.substringAfterLast(sourceFileName, ".");
}
}
所需其他的代碼:
PicUploadResult:
package com.tanhua.sso.vo;
import lombok.Data;
@Data
public class PicUploadResult {
// 檔案唯一辨別
private String uid;
// 檔案名
private String name;
// 狀态有:uploading done error removed
private String status;
// 服務端響應内容,如:'{"status": "success"}'
private String response;
}
1.1.6、PicUploadController
package com.tanhua.sso.controller;
import com.tanhua.sso.service.PicUploadService;
import com.tanhua.sso.vo.PicUploadResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@RequestMapping("pic/upload")
@Controller
public class PicUploadController {
@Autowired
private PicUploadService picUploadService;
@PostMapping
@ResponseBody
public PicUploadResult upload(@RequestParam("file") MultipartFile multipartFile) {
return this.picUploadService.upload(multipartFile);
}
}
1.1.7、測試
1.2、人臉識别
人臉識别技術采用虹軟開放平台實作(免費使用)。官網:https://www.arcsoft.com.cn/
1.2.1、使用說明
使用虹軟平台需要先注冊開發者賬号:https://ai.arcsoft.com.cn/ucenter/user/userlogin
注冊完成後進行登入,然後進行建立應用:
建立完成後,需要進行實名認證,否則相關的SDK是不能使用的。
實名認證後即可下載下傳對應平台的SDk,我們需要下載下傳windows以及linux平台。
添加SDK(Linux與Windows平台):
下載下傳SDK,打開解壓包,可以看到有提供相應的jar包以及示例代碼:
需要特别說明的是:每個賬号的SDK包不通用,是以自己要下載下傳自己的SDK包。
1.2.2、安裝jar到本地倉庫
進入到libs目錄,需要将arcsoft-sdk-face-3.0.0.0.jar安裝到本地倉庫:
mvn install:install-file -DgroupId=com.arcsoft.face -DartifactId=arcsoft-sdk-face -Dversion=3.0.0.0 -Dpackaging=jar -Dfile=arcsoft-sdk-face-3.0.0.0.jar
安裝成功後,即可通過maven坐标引用了:
<dependency>
<groupId>com.arcsoft.face</groupId>
<artifactId>arcsoft-sdk-face</artifactId>
<version>3.0.0.0</version>
<!--<scope>system</scope>-->
<!--如果沒有安裝到本地倉庫,可以将jar包拷貝到工程的lib下面下,直接引用-->
<!--<systemPath>${project.basedir}/lib/arcsoft-sdk-face-3.0.0.0.jar</systemPath>-->
</dependency>
1.2.3、開始使用
說明:虹軟的SDK是免費使用的,但是首次使用時需要聯網激活,激活後可離線使用。使用周期為1年,1年後需要聯網再次激活。
個人免費激活SDK總數量為100。
配置:application.properties
#虹軟相關配置(在虹軟應用中找到對應的參數)
arcsoft.appid=******************
arcsoft.sdkKey=*****************
arcsoft.libPath=F:\\code\\WIN64
FaceEngineService:
package com.tanhua.sso.service;
import com.arcsoft.face.EngineConfiguration;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.FaceInfo;
import com.arcsoft.face.FunctionConfiguration;
import com.arcsoft.face.enums.DetectMode;
import com.arcsoft.face.enums.DetectOrient;
import com.arcsoft.face.enums.ErrorInfo;
import com.arcsoft.face.enums.ImageFormat;
import com.arcsoft.face.toolkit.ImageFactory;
import com.arcsoft.face.toolkit.ImageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@Service
public class FaceEngineService {
private static final Logger LOGGER = LoggerFactory.getLogger(FaceEngineService.class);
@Value("${arcsoft.appid}")
private String appid;
@Value("${arcsoft.sdkKey}")
private String sdkKey;
@Value("${arcsoft.libPath}")
private String libPath;
private FaceEngine faceEngine;
@PostConstruct
public void init() {
// 激活并且初始化引擎
FaceEngine faceEngine = new FaceEngine(libPath);
int activeCode = faceEngine.activeOnline(appid, sdkKey);
if (activeCode != ErrorInfo.MOK.getValue() && activeCode != ErrorInfo.MERR_ASF_ALREADY_ACTIVATED.getValue()) {
LOGGER.error("引擎激活失敗");
throw new RuntimeException("引擎激活失敗");
}
//引擎配置
EngineConfiguration engineConfiguration = new EngineConfiguration();
//IMAGE檢測模式,用于處理單張的圖像資料
engineConfiguration.setDetectMode(DetectMode.ASF_DETECT_MODE_IMAGE);
//人臉檢測角度,全角度
engineConfiguration.setDetectFaceOrientPriority(DetectOrient.ASF_OP_ALL_OUT);
//功能配置
FunctionConfiguration functionConfiguration = new FunctionConfiguration();
functionConfiguration.setSupportAge(true);
functionConfiguration.setSupportFace3dAngle(true);
functionConfiguration.setSupportFaceDetect(true);
functionConfiguration.setSupportFaceRecognition(true);
functionConfiguration.setSupportGender(true);
functionConfiguration.setSupportLiveness(true);
functionConfiguration.setSupportIRLiveness(true);
engineConfiguration.setFunctionConfiguration(functionConfiguration);
//初始化引擎
int initCode = faceEngine.init(engineConfiguration);
if (initCode != ErrorInfo.MOK.getValue()) {
LOGGER.error("初始化引擎出錯!");
throw new RuntimeException("初始化引擎出錯!");
}
this.faceEngine = faceEngine;
}
/**
* 檢測圖檔是否為人像
*
* @param imageInfo 圖像對象
* @return true:人像,false:非人像
*/
public boolean checkIsPortrait(ImageInfo imageInfo) {
// 定義人臉清單
List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(), ImageFormat.CP_PAF_BGR24, faceInfoList);
return !faceInfoList.isEmpty();
}
public boolean checkIsPortrait(byte[] imageData) {
return this.checkIsPortrait(ImageFactory.getRGBData(imageData));
}
public boolean checkIsPortrait(File file) {
return this.checkIsPortrait(ImageFactory.getRGBData(file));
}
}
#問題:
Caused by: java.lang.UnsatisfiedLinkError: D:\gongju\renlian\haha\libs\WIN64\libarcsoft_face.dll: Can't find dependent libraries
解決:
安裝資料中的:vcredist_x64.exe,即可解決。
1.2.4、測試
package com.tanhua.sso.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.File;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class TestFaceEngineService {
@Autowired
private FaceEngineService faceEngineService;
@Test
public void testCheckIsPortrait(){
File file = new File("F:\\1.jpg");
boolean checkIsPortrait = this.faceEngineService.checkIsPortrait(file);
System.out.println(checkIsPortrait); // true|false
}
}
1.3、實作完善個人資訊
完善個人資訊的功能實作,分為2個接口完成,分别是:完善個人資料資訊、頭像上傳。
mock接口:
- 完善個人資訊
- https://mock.boxuegu.com/project/164/interface/api/28553
- https://mock.boxuegu.com/project/164/interface/api/39725
1.3.1、UserInfoMapper
package com.tanhua.sso.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.sso.pojo.UserInfo;
public interface UserInfoMapper extends BaseMapper<UserInfo> {
}
1.3.2、UserInfoService
package com.tanhua.sso.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.sso.enums.SexEnum;
import com.tanhua.sso.mapper.UserInfoMapper;
import com.tanhua.sso.pojo.User;
import com.tanhua.sso.pojo.UserInfo;
import com.tanhua.sso.vo.PicUploadResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Map;
@Service
public class UserInfoService {
@Autowired
private UserService userService;
@Autowired
private UserInfoMapper userInfoMapper;
@Autowired
private FaceEngineService faceEngineService;
@Autowired
private PicUploadService picUploadService;
public Boolean saveUserInfo(Map<String, String> param, String token) {
//校驗token
User user = this.userService.queryUserByToken(token);
if (null == user) {
return false;
}
UserInfo userInfo = new UserInfo();
userInfo.setUserId(user.getId());
userInfo.setSex(StringUtils.equalsIgnoreCase(param.get("gender"), "man") ? SexEnum.MAN : SexEnum.WOMAN);
userInfo.setNickName(param.get("nickname"));
userInfo.setBirthday(param.get("birthday"));
userInfo.setCity(param.get("city"));
return this.userInfoMapper.insert(userInfo) == 1;
}
public Boolean saveUserLogo(MultipartFile file, String token) {
//校驗token
User user = this.userService.queryUserByToken(token);
if (null == user) {
return false;
}
try {
//校驗圖檔是否是人像,如果不是人像就傳回false
boolean b = this.faceEngineService.checkIsPortrait(file.getBytes());
if (!b) {
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
//圖檔上傳到阿裡雲OSS
PicUploadResult result = this.picUploadService.upload(file);
if (StringUtils.isEmpty(result.getName())) {
//上傳失敗
return false;
}
//把頭像儲存到使用者資訊表中
UserInfo userInfo = new UserInfo();
userInfo.setLogo(result.getName());
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", user.getId());
return this.userInfoMapper.update(userInfo, queryWrapper) == 1;
}
}
1.3.3、UserInfoController
package com.tanhua.sso.controller;
import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.service.UserService;
import com.tanhua.sso.vo.ErrorResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("user")
public class UserInfoController {
@Autowired
private UserInfoService userInfoService;
/**
* 完善個人資訊-基本資訊
*
* @param param
* @return
*/
@PostMapping("loginReginfo")
public ResponseEntity<Object> saveUserInfo(@RequestBody Map<String, String> param,
@RequestHeader("Authorization") String token) {
try {
Boolean bool = this.userInfoService.saveUserInfo(param, token);
if (bool) {
return ResponseEntity.ok(null);
}
} catch (Exception e) {
e.printStackTrace();
}
ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("儲存使用者資訊失敗!").build();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
}
/**
* 完善個人資訊-使用者頭像
*
* @return
*/
@PostMapping("loginReginfo/head")
public ResponseEntity<Object> saveUserLogo(@RequestParam("headPhoto") MultipartFile file,
@RequestHeader("Authorization") String token) {
try {
Boolean bool = this.userInfoService.saveUserLogo(file, token);
if (bool) {
return ResponseEntity.ok(null);
}
} catch (Exception e) {
e.printStackTrace();
}
ErrorResult errorResult = ErrorResult.builder().errCode("000001").errMessage("儲存使用者logo失敗!").build();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
}
}
1.4.4、測試
圖檔上傳超過1MB出錯的解決方案:
#在application.properties檔案中,填入下面的配置
#設定最大的檔案上傳大小
spring.servlet.multipart.max-request-size=30MB
spring.servlet.multipart.max-file-size=30MB
2、校驗token
在整個系統架構中,隻有SSO儲存了JWT中的秘鑰,是以隻能通過SSO系統提供的接口服務進行對token的校驗,是以在SSO系統中,需要對外開放接口,通過token進行查詢使用者資訊,如果傳回null說明使用者狀态已過期或者是非法的token,否則傳回User對象資料。
2.1、UserController
/**
* 校驗token,根據token查詢使用者資料
*
* @param token
* @return
*/
@GetMapping("{token}")
public User queryUserByToken(@PathVariable("token") String token) {
return this.userService.queryUserByToken(token);
}
2.2、UserService
public User queryUserByToken(String token) {
try {
// 通過token解析資料
Map<String, Object> body = Jwts.parser()
.setSigningKey(secret)
.parseClaimsJws(token)
.getBody();
User user = new User();
user.setId(Long.valueOf(body.get("id").toString()));
//需要傳回user對象中的mobile,需要查詢資料庫擷取到mobile資料
//如果每次都查詢資料庫,必然會導緻性能問題,需要對使用者的手機号進行緩存操作
//資料緩存時,需要設定過期時間,過期時間要與token的時間一緻
//如果使用者修改了手機号,需要同步修改redis中的資料
String redisKey = "TANHUA_USER_MOBILE_" + user.getId();
if(this.redisTemplate.hasKey(redisKey)){
String mobile = this.redisTemplate.opsForValue().get(redisKey);
user.setMobile(mobile);
}else {
//查詢資料庫
User u = this.userMapper.selectById(user.getId());
user.setMobile(u.getMobile());
//将手機号寫入到redis中
//在jwt中的過期時間的機關為:秒
long timeout = Long.valueOf(body.get("exp").toString()) * 1000 - System.currentTimeMillis();
this.redisTemplate.opsForValue().set(redisKey, u.getMobile(), timeout, TimeUnit.MILLISECONDS);
}
return user;
} catch (ExpiredJwtException e) {
log.info("token已經過期! token = " + token);
} catch (Exception e) {
log.error("token不合法! token = "+ token, e);
}
return null;
}
2.3、測試
資料已經存儲到redis中:
3、MongoDB快速入門
3.1、MongoDB簡介
MongoDB是一個基于分布式檔案存儲的資料庫。由C++語言編寫。旨在為WEB應用提供可擴充的高性能資料存儲解決方案。
MongoDB是一個介于關系資料庫和非關系資料庫之間的産品,是非關系資料庫當中功能最豐富,最像關系資料庫的,它支援的資料結構非常松散,是類似json的bson格式,是以可以存儲比較複雜的資料類型。
MongoDB最大的特點是它支援的查詢語言非常強大,其文法有點類似于面向對象的查詢語言,幾乎可以實作類似關系資料庫單表查詢的絕大部分功能,而且還支援對資料建立索引。
官網:https://www.mongodb.com
3.2、通過docker安裝MongoDB
#拉取鏡像
docker pull mongo:4.0.3
#建立容器
docker create --name mongodb-server -p 27018:27017 -v mongodb-data:/data/db mongo:4.0.3 --auth
#啟動容器
docker start mongodb-server
#進入容器
docker exec -it mongodb-server /bin/bash
#進入admin資料庫
mongo
use admin
#添加管理者,其擁有管理使用者和角色的權限
db.createUser({ user: 'root', pwd: 'root', roles: [ { role: "root", db: "admin" } ] })
#測試,發現是沒有權限操作的
> show dbs
2020-10-20T09:09:15.543+0000 E QUERY [js] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "command listDatabases requires authentication",
"code" : 13,
"codeName" : "Unauthorized"
} :
#進行認證
mongo -u "root" -p "root" --authenticationDatabase "admin"
#通過admin添加普通使用者
use admin
db.createUser({ user: 'tanhua', pwd: 'l3SCjl0HvmSkTtiSbN0Swv40spYnHhDV', roles: [ { role: "readWrite", db: "tanhua" } ] });
#通過tanhua使用者登入進行測試
mongo -u "tanhua" -p "l3SCjl0HvmSkTtiSbN0Swv40spYnHhDV" --authenticationDatabase "admin"
#測試
[email protected]:/# mongo -u "tanhua" -p "tanhua123" --authenticationDatabase "admin"
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("6c368269-30f0-4b29-a224-05a38b5847e2") }
MongoDB server version: 4.0.3
> use tanhua
switched to db tanhua
> db.user.insert({id:1,username:'zhangsan',age:20})
WriteResult({ "nInserted" : 1 })
> db.user.find()
{ "_id" : ObjectId("5f8eb2726e0de0aa9517afd3"), "id" : 1, "username" : "zhangsan", "age" : 20 }
3.3、MongoDB基本操作
3.3.1、基本概念
為了更好的了解,下面與SQL中的概念進行對比:
SQL術語/概念 | MongoDB術語/概念 | 解釋/說明 |
---|---|---|
database | database | 資料庫 |
table | collection | 資料庫表/集合 |
row | document | 資料記錄行/文檔 |
column | field | 資料字段/域 |
index | index | 索引 |
table joins | 表連接配接,MongoDB不支援 | |
primary key | primary key | 主鍵,MongoDB自動将_id字段設定為主鍵 |
3.3.2、資料庫以及表的操作
#檢視所有的資料庫
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
#通過use關鍵字切換資料庫
> use admin
switched to db admin
#建立資料庫
#說明:在MongoDB中,資料庫是自動建立的,通過use切換到新資料庫中,進行插入資料即可自動建立資料庫
> use testdb
switched to db testdb
> show dbs #并沒有建立資料庫
admin 0.000GB
config 0.000GB
local 0.000GB
> db.user.insert({id:1,name:'zhangsan'}) #插入資料
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
testdb 0.000GB #資料庫自動建立
#檢視表
> show tables
user
> show collections
user
>
#删除集合(表)
> db.user.drop()
true #如果成功删除標明集合,則 drop() 方法傳回 true,否則傳回 false。
#删除資料庫
> use testdb #先切換到要删除的資料庫中
switched to db testdb
> db.dropDatabase() #删除資料庫
{ "dropped" : "testdb", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
3.3.3、新增資料
在MongoDB中,存儲的文檔結構是一種類似于json的結構,稱之為bson(全稱為:Binary JSON)。
#插入資料
#文法:db.COLLECTION_NAME.insert(document)
> db.user.insert({id:1,username:'zhangsan',age:20})
WriteResult({ "nInserted" : 1 })
> db.user.save({id:2,username:'lisi',age:25})
WriteResult({ "nInserted" : 1 })
> db.user.find() #查詢資料
{ "_id" : ObjectId("5c08c0024b318926e0c1f6dc"), "id" : 1, "username" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("5c08c0134b318926e0c1f6dd"), "id" : 2, "username" : "lisi", "age" : 25 }
3.3.4、更新資料
update() 方法用于更新已存在的文檔。文法格式如下:
db.collection.update(
<query>,
<update>,
[
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
]
)
參數說明:
- query : update的查詢條件,類似sql update查詢内where後面的。
- update : update的對象和一些更新的操作符(如 , , ,inc…)等,也可以了解為sql update查詢内set後面的
- upsert : 可選,這個參數的意思是,如果不存在update的記錄,是否插入objNew,true為插入,預設是false,不插入。
- multi : 可選,mongodb 預設是false,隻更新找到的第一條記錄,如果這個參數為true,就把按條件查出來多條記錄全部更新。
- writeConcern :可選,抛出異常的級别。
> db.user.find()
{ "_id" : ObjectId("5c08c0024b318926e0c1f6dc"), "id" : 1, "username" : "zhangsan", "age" : 20 }
{ "_id" : ObjectId("5c08c0134b318926e0c1f6dd"), "id" : 2, "username" : "lisi", "age" : 25 }
> db.user.update({id:1},{$set:{age:22}}) #更新資料
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("5c08c0024b318926e0c1f6dc"), "id" : 1, "username" : "zhangsan", "age" : 22 }
{ "_id" : ObjectId("5c08c0134b318926e0c1f6dd"), "id" : 2, "username" : "lisi", "age" : 25 }
#注意:如果這樣寫,會删除掉其他的字段
> db.user.update({id:1},{age:25})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("5c08c0024b318926e0c1f6dc"), "age" : 25 }
{ "_id" : ObjectId("5c08c0134b318926e0c1f6dd"), "id" : 2, "username" : "lisi", "age" : 25 }
#更新不存在的字段,會新增字段
> db.user.update({id:2},{$set:{sex:1}}) #更新資料
> db.user.find()
{ "_id" : ObjectId("5c08c0024b318926e0c1f6dc"), "age" : 25 }
{ "_id" : ObjectId("5c08c0134b318926e0c1f6dd"), "id" : 2, "username" : "lisi", "age" : 25, "sex" : 1 }
#更新不存在的資料,預設不會新增資料
> db.user.update({id:3},{$set:{sex:1}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.user.find()
{ "_id" : ObjectId("5c08c0024b318926e0c1f6dc"), "age" : 25 }
{ "_id" : ObjectId("5c08c0134b318926e0c1f6dd"), "id" : 2, "username" : "lisi", "age" : 25, "sex" : 1 }
#如果設定第一個參數為true,就是新增資料
> db.user.update({id:3},{$set:{sex:1}},true)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5c08cb281418d073246bc642")
})
> db.user.find()
{ "_id" : ObjectId("5c08c0024b318926e0c1f6dc"), "age" : 25 }
{ "_id" : ObjectId("5c08c0134b318926e0c1f6dd"), "id" : 2, "username" : "lisi", "age" : 25, "sex" : 1 }
{ "_id" : ObjectId("5c08cb281418d073246bc642"), "id" : 3, "sex" : 1 }
3.3.5、删除資料
通過remove()方法進行删除資料,文法如下:
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>
}
)
參數說明:
- query :(可選)删除的文檔的條件。
- justOne : (可選)如果設為 true 或 1,則隻删除一個文檔,如果不設定該參數,或使用預設值 false,則删除所有比對條件的文檔。
- writeConcern :(可選)抛出異常的級别。
執行個體:
> db.user.remove({age:25})
WriteResult({ "nRemoved" : 2 }) #删除了2條資料
#插入4條測試資料
db.user.insert({id:1,username:'zhangsan',age:20})
db.user.insert({id:2,username:'lisi',age:21})
db.user.insert({id:3,username:'wangwu',age:22})
db.user.insert({id:4,username:'zhaoliu',age:22})
> db.user.remove({age:22},true)
WriteResult({ "nRemoved" : 1 }) #删除了1條資料
#删除所有資料
> db.user.remove({})
#說明:為了簡化操作,官方推薦使用deleteOne()與deleteMany()進行删除資料操作。
db.user.deleteOne({id:1})
db.user.deleteMany({}) #删除所有資料
3.3.6、查詢資料
MongoDB 查詢資料的文法格式如下:
db.user.find([query],[fields])
- query :可選,使用查詢操作符指定查詢條件
- fields :可選,使用投影操作符指定傳回的鍵。查詢時傳回文檔中所有鍵值, 隻需省略該參數即可(預設省略)。
如果你需要以易讀的方式來讀取資料,可以使用 pretty() 方法,文法格式如下:
>db.col.find().pretty()
pretty() 方法以格式化的方式來顯示所有文檔。
條件查詢:
操作 | 格式 | 範例 | RDBMS中的類似語句 |
---|---|---|---|
等于 | } | | |
小于 | | | |
小于或等于 | | | |
大于 | | | |
大于或等于 | | | |
不等于 | | | |
執行個體:
#插入測試資料
db.user.insert({id:1,username:'zhangsan',age:20})
db.user.insert({id:2,username:'lisi',age:21})
db.user.insert({id:3,username:'wangwu',age:22})
db.user.insert({id:4,username:'zhaoliu',age:22})
db.user.find() #查詢全部資料
db.user.find({},{id:1,username:1}) #隻查詢id與username字段
db.user.find().count() #查詢資料條數
db.user.find({id:1}) #查詢id為1的資料
db.user.find({age:{$lte:21}}) #查詢小于等于21的資料
db.user.find({age:{$lte:21}, id:{$gte:2}}) #and查詢,age小于等于21并且id大于等于2
db.user.find({$or:[{id:1},{id:2}]}) #查詢id=1 or id=2
#分頁查詢:Skip()跳過幾條,limit()查詢條數
db.user.find().limit(2).skip(1) #跳過1條資料,查詢2條資料
db.user.find().sort({id:-1}) #按照age倒序排序,-1為倒序,1為正序
3.4、索引
索引通常能夠極大的提高查詢的效率,如果沒有索引,MongoDB在讀取資料時必須掃描集合中的每個檔案并選取那些符合查詢條件的記錄。
這種掃描全集合的查詢效率是非常低的,特别在處理大量的資料時,查詢可以要花費幾十秒甚至幾分鐘,這對網站的性能是非常緻命的。
索引是特殊的資料結構,索引存儲在一個易于周遊讀取的資料集合中,索引是對資料庫表中一列或多列的值進行排序的一種結構
#檢視索引
> db.user.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "testdb.user"
}
]
#說明:1表示升序建立索引,-1表示降序建立索引。
#建立索引
> db.user.createIndex({'age':1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
#删除索引
db.user.dropIndex("age_1")
#或者,删除除了_id之外的索引
db.user.dropIndexes()
#建立聯合索引
db.user.createIndex({'age':1, 'id':-1})
#檢視索引大小,機關:位元組
db.user.totalIndexSize()
3.5、執行計劃
MongoDB 查詢分析可以確定我們建議的索引是否有效,是查詢語句性能分析的重要工具。
#插入1000條資料
for(var i=1;i<1000;i++)db.user.insert({id:100+i,username:'name_'+i,age:10+i})
#檢視執行計劃
> db.user.find({age:{$gt:100},id:{$lt:200}}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "testdb.user",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"id" : {
"$lt" : 200
}
},
{
"age" : {
"$gt" : 100
}
}
]
},
"winningPlan" : { #最佳執行計劃
"stage" : "FETCH", #查詢方式,常見的有COLLSCAN/全表掃描、IXSCAN/索引掃描、FETCH/根據索引去檢索文檔、SHARD_MERGE/合并分片結果、IDHACK/針對_id進行查詢
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"age" : 1,
"id" : -1
},
"indexName" : "age_1_id_-1",
"isMultiKey" : false,
"multiKeyPaths" : {
"age" : [ ],
"id" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"age" : [
"(100.0, inf.0]"
],
"id" : [
"(200.0, -inf.0]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "c493d5ff750a",
"port" : 27017,
"version" : "4.0.3",
"gitVersion" : "7ea530946fa7880364d88c8d8b6026bbc9ffa48c"
},
"ok" : 1
}
#測試沒有使用索引
> db.user.find({username:'zhangsan'}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "testdb.user",
"indexFilterSet" : false,
"parsedQuery" : {
"username" : {
"$eq" : "zhangsan"
}
},
"winningPlan" : {
"stage" : "COLLSCAN", #全表掃描
"filter" : {
"username" : {
"$eq" : "zhangsan"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "c493d5ff750a",
"port" : 27017,
"version" : "4.0.3",
"gitVersion" : "7ea530946fa7880364d88c8d8b6026bbc9ffa48c"
},
"ok" : 1
}
3.6、UI用戶端工具
Robo 3T是MongoDB的用戶端工具,我們可以使用它來操作MongoDB。
檢視資料:
或使用Navicat Premium 15:
4、SpringBoot整合MongoDB
spring-data對MongoDB做了支援,使用spring-data-mongodb可以簡化MongoDB的操作。
位址:https://spring.io/projects/spring-data-mongodb
4.1、導入依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<groupId>cn.itcast.mongodb</groupId>
<artifactId>itcast-mongodb</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.2、編寫application.properties配置檔案
# Spring boot application
spring.application.name = itcast-mongodb
#無認證資訊的配置
#spring.data.mongodb.uri=mongodb://192.168.31.81:27017/tanhua
#springboot 配置
spring.data.mongodb.username=tanhua
spring.data.mongodb.password=l3SCjl0HvmSkTtiSbN0Swv40spYnHhDV
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=tanhua
spring.data.mongodb.port=27018
spring.data.mongodb.host=192.168.31.81
4.3、編寫實體
package cn.itcast.mongodb.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private ObjectId id;
private String name;
private int age;
private Address address;
}
package cn.itcast.mongodb.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address {
private String street;
private String city;
private String zip;
}
4.4、編寫dao
package cn.itcast.mongodb.dao;
import cn.itcast.mongodb.pojo.Person;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class PersonDao {
@Autowired
private MongoTemplate mongoTemplate;
public void savePerson(Person person) {
this.mongoTemplate.save(person);
}
public List<Person> queryPersonListByName(String name) {
Query query = Query.query(Criteria.where("name").is(name));
return this.mongoTemplate.find(query, Person.class);
}
public List<Person> queryPersonPageList(Integer page, Integer rows) {
Query query = new Query().limit(rows).skip((page - 1) * rows);
return this.mongoTemplate.find(query, Person.class);
}
public UpdateResult update(Person person) {
Query query = Query.query(Criteria.where("id").is(person.getId()));
Update update = Update.update("age", person.getAge());
return this.mongoTemplate.updateFirst(query, update, Person.class);
}
public DeleteResult deleteById(String id) {
Query query = Query.query(Criteria.where("id").is(id));
return this.mongoTemplate.remove(query, Person.class);
}
}
4.5、編寫啟動類
package cn.itcast.mongodb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MongoApplication {
public static void main(String[] args) {
SpringApplication.run(MongoApplication.class, args);
}
}
4.6、編寫單元測試
package cn.itcast.mongodb;
import cn.itcast.mongodb.dao.PersonDao;
import cn.itcast.mongodb.pojo.Address;
import cn.itcast.mongodb.pojo.Person;
import org.bson.types.ObjectId;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestPersonDao {
@Autowired
private PersonDao personDao;
@Test
public void testSave() {
Person person = new Person(ObjectId.get(), "張三", 20,
new Address("人民路", "上海市", "666666"));
this.personDao.savePerson(person);
}
@Test
public void testQuery() {
List<Person> personList = this.personDao.queryPersonListByName("張三");
for (Person person : personList) {
System.out.println(person);
}
}
@Test
public void testQuery2() {
List<Person> personList = this.personDao.queryPersonPageList(2, 2);
for (Person person : personList) {
System.out.println(person);
}
}
@Test
public void testUpdate() {
Person person = new Person();
person.setId(new ObjectId("5c0956ce235e192520086736"));
person.setAge(30);
this.personDao.update(person);
}
@Test
public void testDelete() {
this.personDao.deleteById("5c09ca05235e192d8887a389");
}
}