MongoDB 入门教程
一.下载
1.MongoDB
2.MongoDB Compass(MongoDB的客户端)
二.安装
1.MongoDB
2.MongoDB Compass
三.Compass连接MongoDB
1.创建MongoDB数据库连接
2.创建数据库
四.Spring整合MongoDB
1.添加依赖
2.修改application.yml配置
3.添加文档对象MProduct
4.添加MProductRepository接口用于操作Mongodb
5.添加MProductService接口和MProductServiceImpl实现类
6.添加MProductController控制器
7.Mongo注解
8.接口测试
一.下载
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.接口测试
创建产品
查询产品
删除产品
还没有评论,来说两句吧...