【第十一章】 springboot + mongodb(简单查询)
2017-07-13 13:43
477 查看
1、mongodb在mac上的安装
- 下载mongodb,https://www.mongodb.org/
- 解压缩到一个指定文件夹,如:/Users/enniu1/Desktop/zjg/mongodb-osx-x86_64-3.2.6(这是我的mongodb的版本)
- 配置PATH 输入命令:"vi ~/.bash_profile"
- 添加如下两句配置:
1 export MONGO_HOME=/Users/enniu1/Desktop/zjg/mongodb-osx-x86_64-3.2.6 2 export PATH=$PATH:$MONGO_HOME/bin
- 创建数据目录 输入命令:"sudo mkdir -p /data/db"
-
输入命令:"sudo chmod 777 /data/db"
-
输入命令:"mongod"
注意两个错:
- 如果没有创建目录直接启动,会报错http://stackoverflow.com/questions/7948789/mongodb-mongod-complains-that-there-is-no-data-db-folder
- 如果没有赋予数据目录权限,会报错http://stackoverflow.com/questions/15229412/unable-to-create-open-lock-file-data-mongod-lock-errno13-permission-denied
参考:https://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
2、代码(4个部分)
2.1、com.xxx.firstboot.domain.Custome
package com.xxx.firstboot.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.xxx.firstboot.domain.Customer; import com.xxx.firstboot.mongo.CustomerRepository; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @RestController @RequestMapping("/customer") @Api("customer相关的API,用于测试mongodb") public class CustomerController { @Autowired private CustomerRepository customerRepository; @ApiOperation("增加一个Customer") @RequestMapping(value = "/addCustomer", method = RequestMethod.GET) public Customer addCustomer(@RequestParam("firstname") String firstname, @RequestParam("secondname") String secondname) { Customer customer = new Customer(); customer.setFirstname(firstname); customer.setSecondname(secondname); return customerRepository.save(customer); } @ApiOperation("获取所有的Customer") @RequestMapping(value = "/getAllCustomer", method = RequestMethod.GET) public List<Customer> getAllCustomer() { return customerRepository.findAll(); } @ApiOperation("根据firstname获取Customer") @RequestMapping(value = "/getCustomerByFirstname", method = RequestMethod.GET) public Customer getCustomerByFirstname(@RequestParam("firstname") String firstname) { return customerRepository.findByFirstname(firstname); } @ApiOperation("根据secondname获取多个Customer") @RequestMapping(value = "/getCustomerBySecondname", method = RequestMethod.GET) public List<Customer> getCustomerBySecondname(@RequestParam("secondname") String secondname) { return customerRepository.findBySecondname(secondname); } @ApiOperation("根据id删除Customer") @RequestMapping(value = "/deleteCustomerById", method = RequestMethod.GET) public boolean deleteCustomerById(@RequestParam("cid") String cid) { customerRepository.delete(cid); return true; } }View Code
说明:直接注入我们自己的业务接口,然后进行相应的操作即可。
此时,就可以进行测试了。只是此时使用的都是mongodb的默认信息。
- host:localhost
- port:27017
- 数据库:test
- collection:customer(domain类的简单类名)
2.4、application.properties
1 #mongodb note:mongo3.x will not use host and port,only use uri 2 spring.data.mongodb.host=192.168.21.54 3 spring.data.mongodb.port=27017 4 spring.data.mongodb.uri=mongodb://192.168.21.54:27017/myfirstMongodb
说明:如果需要指定host、port、数据库,需要在application.properties文件中配置以上信息。
注意:
- 配置必须以"spring.data.mongodb"为前缀
- 如果是mongo3.x的话,host和port没用,需要uri。(未测过)
- uri = mongodb://host:port/数据库
- mongo2.x支持以上两种配置方式
- mongo3.x仅支持uri方式
3、测试
启动应用,启动mongo服务进程,打开swagger,使用robomongo或者mongobooster客户端观察mongodb存储情况。
没有在application.properties中设置属性。
设置属性后,
参考:
https://spring.io/guides/gs/accessing-data-mongodb/ 其中的例子就是对sample代码的解释
http://www.jianshu.com/p/e59cd2dc5274 关于mongodb主键
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql.html 关于mongo2.x与3.x对host、port、uri配置的支持。
http://blog.didispace.com/springbootmongodb/ 主键为Long uri用户名、密码配置
相关文章推荐
- 第十一章 springboot + mongodb(简单查询)
- 第十一章 springboot + mongodb(简单查询)
- springboot + mongodb 查询实例
- SpringBoot简单连接数据库以及查询数据
- SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法
- SpringBoot中简单使用mongodb
- springboot+mongodb 实现按日期分组分页查询功能
- SpringBoot结合MongoDB简单应用
- SpringBoot--配置MongoDB并简单调用
- Springboot+Mybatis简单查询
- 第十二章 springboot + mongodb(复杂查询)
- SpringBoot中简单使用mongodb
- 基于Spring Boot 2.0 及MongoDB 3.6.2 实现的简单文件共享服务器
- springboot干货——(十四【二】)mongodb进行分页显示【含条件查询】
- SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法
- 基于Springboot使用MongoDB进行简单Gis操作
- 【第十二章】 springboot + mongodb(复杂查询)
- 第十二章 springboot + mongodb(复杂查询)
- MongoDB最简单的入门教程之四:使用Spring Boot操作MongoDB
- 1.2 使用IntelliJ IDEA搭建Spring-Boot与MongoDB数据的简单登录项目