标签:
1、mongodb在mac上的安装
1 export MONGO_HOME=/Users/enniu1/Desktop/zjg/mongodb-osx-x86_64-3.2.6 2 export PATH=$PATH:$MONGO_HOME/bin
注意两个错:
参考:https://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
2、代码(4个部分)
2.1、com.xxx.firstboot.domain.Customer
1 package com.xxx.firstboot.domain; 2 3 import org.springframework.data.annotation.Id; 4 5 /** 6 * 测试mongodb 7 */ 8 public class Customer { 9 /** 10 * cid:该字段用于mongodb的"_id"索引 11 * 1、需要@Id注解 12 * 2、取名无所谓,反正在mongodb中最后都会转化为"_id" 13 * 3、定义为String类型,如果定义为Integer可能索引只会是0,会出现key重复导致数据库插不进去的情况; 14 * 4、该类型也是MongoRepository泛型中主键的ID 15 */ 16 @Id 17 private String cid; 18 private String firstname; 19 private String secondname; 20 21 public String getCid() { 22 return cid; 23 } 24 25 public void setCid(String cid) { 26 this.cid = cid; 27 } 28 29 public String getFirstname() { 30 return firstname; 31 } 32 33 public void setFirstname(String firstname) { 34 this.firstname = firstname; 35 } 36 37 public String getSecondname() { 38 return secondname; 39 } 40 41 public void setSecondname(String secondname) { 42 this.secondname = secondname; 43 } 44 45 }
说明:生成的colletion(类似于MySQL中的表)就是domain类的简单类名,eg.customer。
注意:
2.2、com.xxx.firstboot.mongo.CustomerRepository
1 package com.xxx.firstboot.mongo; 2 3 import java.util.List; 4 5 import org.springframework.data.mongodb.repository.MongoRepository; 6 7 import com.xxx.firstboot.domain.Customer; 8 9 /** 10 * MongoRepository<Customer, Integer> 11 * 第一个参数:T 操作的vo 12 * 第二个参数:ID T的主键类型 13 * 作用:该接口实现了CRUD方法 14 * 15 * 注意: 16 * 1、由于boot使用了spring-data-mongodb,所以我们不需要写该接口的实现, 17 * 当我们运行程序的时候,spring-data-mongodb会动态创建 18 * 2、findBySecondname命名是有讲究的,Secondname(是Customer的属性)若改为lastname就会报找不到属性lastname的错误 19 */ 20 public interface CustomerRepository extends MongoRepository<Customer, String> { 21 public Customer findByFirstname(String firstname); 22 public List<Customer> findBySecondname(String secondname); 23 }
说明:该接口就是我们的业务接口。
注意:
2.3、com.xxx.firstboot.web.CustomerController
1 package com.xxx.firstboot.web; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8 import org.springframework.web.bind.annotation.RequestParam; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import com.xxx.firstboot.domain.Customer; 12 import com.xxx.firstboot.mongo.CustomerRepository; 13 14 import io.swagger.annotations.Api; 15 import io.swagger.annotations.ApiOperation; 16 17 @RestController 18 @RequestMapping("/customer") 19 @Api("customer相关的API,用于测试mongodb") 20 public class CustomerController { 21 22 @Autowired 23 private CustomerRepository customerRepository; 24 25 @ApiOperation("增加一个Customer") 26 @RequestMapping(value = "/addCustomer", method = RequestMethod.GET) 27 public Customer addCustomer(@RequestParam("firstname") String firstname, 28 @RequestParam("secondname") String secondname) { 29 Customer customer = new Customer(); 30 customer.setFirstname(firstname); 31 customer.setSecondname(secondname); 32 return customerRepository.save(customer); 33 } 34 35 @ApiOperation("获取所有的Customer") 36 @RequestMapping(value = "/getAllCustomer", method = RequestMethod.GET) 37 public List<Customer> getAllCustomer() { 38 return customerRepository.findAll(); 39 } 40 41 @ApiOperation("根据firstname获取Customer") 42 @RequestMapping(value = "/getCustomerByFirstname", method = RequestMethod.GET) 43 public Customer getCustomerByFirstname(@RequestParam("firstname") String firstname) { 44 return customerRepository.findByFirstname(firstname); 45 } 46 47 @ApiOperation("根据secondname获取多个Customer") 48 @RequestMapping(value = "/getCustomerBySecondname", method = RequestMethod.GET) 49 public List<Customer> getCustomerBySecondname(@RequestParam("secondname") String secondname) { 50 return customerRepository.findBySecondname(secondname); 51 } 52 53 @ApiOperation("根据id删除Customer") 54 @RequestMapping(value = "/deleteCustomerById", method = RequestMethod.GET) 55 public boolean deleteCustomerById(@RequestParam("cid") String cid) { 56 customerRepository.delete(cid); 57 return true; 58 } 59 }
说明:直接注入我们自己的业务接口,然后进行相应的操作即可。
此时,就可以进行测试了。只是此时使用的都是mongodb的默认信息。
2.4、application.properties
说明:如果需要指定host、port、数据库,需要在application.properties文件中配置以上信息。
注意:
3、测试
启动应用,启动mongo服务进程,打开swagger,使用robomongo客户端观察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://www.cnblogs.com/java-zhao/p/5449333.html