2.MongoDB Compass(MongoDB的客戶端)
4.添加MProductRepository接口用於操作Mongodb
5.添加MProductService接口和MProductServiceImpl實現類
一.下載
1.MongoDB
https://www.mongodb.com/download-center/community
2.MongoDB Compass(MongoDB的客戶端)
https://www.mongodb.com/download-center/compass
二.安裝
1.MongoDB
"install mongoDB compass" 不勾選,後面自己安裝
安裝完後啓動查看版本
創建配置文件
systemLog:
destination: file
path: D:\soft\MongoDB\log\mongod.log
storage:
dbPath: D:\soft\MongoDB\data\db
確保上面配置的文件夾對應的路徑是有的,沒有則創建(創建data\db和log兩個文件夾)
安裝服務
用管理員權限 打開命令行執行
D:\soft\MongoDB\bin\mongod.exe --config "D:\soft\MongoDB\mongod.cfg" --install
另外在無需管理員權限的命令窗口啓動/關閉服務
相關命令
刪除服務:sc delete MongoDB
啓動服務:net start MongoDB
關閉服務:net stop MongoDB
移除服務:D:\soft\MongoDB\bin\mongod.exe --remove
2.MongoDB Compass
安裝完後啓動
三.Compass連接MongoDB
1.創建MongoDB數據庫連接
2.創建數據庫
四.Spring整合MongoDB
1.添加依賴
<!---mongodb相關依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.修改application.yml配置
spring:data節點下添加Mongodb相關配置
#MongoDB配置
mongodb:
host: localhost
port: 27017
database: ak-mall
3.添加文檔對象MProduct
文檔對象的ID域添加@Id註解,需要檢索的字段添加@Indexed註解。
@Data
@Document
public class MProduct implements Serializable {
private static final long serialVersionUID = -1296261179864925924L;
@Id
private String id;
@Indexed
private String brandId;
private String name;
private Date createTime;
}
4.添加MProductRepository接口用於操作Mongodb
/**
* 繼承MongoRepository接口,這樣就擁有了一些基本的Mongodb數據操作方法,同時定義了一個衍生查詢方法
*/
public interface MProductRepository extends MongoRepository<MProduct, Long> {
/**
* 根據品牌Id查詢產品並倒序
*
* @param brandId 品牌Id
*/
List<MProduct> findByBrandIdOrderByCreateTimeDesc(String brandId);
}
5.添加MProductService接口和MProductServiceImpl實現類
public interface MProductService {
/**
* 生成產品
*/
int create(MProduct mProduct);
/**
* 批量刪除
*/
int delete(List<String> ids);
/**
* 根據brandId獲取產品
*/
List<MProduct> list(String brandId);
}
@Service
public class MProductServiceImpl implements MProductService {
@Autowired
private MProductRepository mProductRepository;
@Override
public int create(MProduct mProduct) {
mProduct.setId(null);
mProduct.setCreateTime(new Date());
mProductRepository.save(mProduct);
return 1;
}
@Override
public int delete(List<String> ids) {
List<MProduct> deleteList = new ArrayList<>();
for (String id : ids) {
MProduct mProduct = new MProduct();
mProduct.setId(id);
deleteList.add(mProduct);
}
mProductRepository.deleteAll(deleteList);
return ids.size();
}
@Override
public List<MProduct> list(String brandId) {
return mProductRepository.findByBrandIdOrderByCreateTimeDesc(brandId);
}
}
6.添加MProductController控制器
@Api(tags = "MProductController", description = "Mongo-產品控制器")
@RestController
@RequestMapping("/m/product")
public class MProductController {
@Autowired
private MProductService mProductService;
@ApiOperation("創建產品")
@RequestMapping(value = "/create", method = RequestMethod.POST)
public CommonResult create(@RequestBody MProduct mProduct) {
int count = mProductService.create(mProduct);
if (count > 0) {
return CommonResult.success(count);
} else {
return CommonResult.failed();
}
}
@ApiOperation("刪除產品")
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public CommonResult delete(@RequestParam("ids") List<String> ids) {
int count = mProductService.delete(ids);
if (count > 0) {
return CommonResult.success(count);
} else {
return CommonResult.failed();
}
}
@ApiOperation("根據brandId獲取產品")
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public CommonResult<List<MProduct>> list(String brandId) {
List<MProduct> mProductList = mProductService.list(brandId);
return CommonResult.success(mProductList);
}
}
7.Mongo註解
@Document:標示映射到Mongodb文檔上的領域對象
@Id:標示某個域爲ID域
@Indexed:標示某個字段爲Mongodb的索引字段
8.接口測試
創建產品
查詢產品
刪除產品