1. MongoDB Installation on Mac
- Download mongodb,https://www.mongodb.org/
- Unzip to a specified folder, such as:/users/enniu1/desktop/zjg/mongodb-osx-x86_64-3.2.6 (This is the version of my MongoDB)
- Configure path
- Input command: "VI ~/.bash_profile"
- Add the following two sentence configurations:
1 export mongo_home=/users/enniu1/desktop/zjg/mongodb-osx-x86_64-3.2.62 export path= $PATH: $MONGO _home/bin
- Create Data Catalog
- Input command: "sudo mkdir-p/data/db"
- Give Data directory Permissions
- Input command: "sudo chmod 777/data/db"
- Start
- Exit: Ctrl + C
Note Two errors:
- If you do not create a directory to start directly, will error Http://stackoverflow.com/questions/7948789/mongodb-mongod-complains-that-there-is-no-data-db-folder
- If the data directory permissions are not assigned, an error will be http://stackoverflow.com/questions/15229412/ Unable-to-create-open-lock-file-data-mongod-lock-errno13-permission-denied
Reference: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
2. Code (4 parts)
2.1, Com.xxx.firstboot.domain.Custome
PackageCom.xxx.firstboot.domain;Importorg.springframework.data.annotation.Id;/*** Test MongoDB*/ Public classCustomer {/*** CID: This field is used for MongoDB "_id" Index * 1, need @id annotation * 2, name does not matter, anyway in MongoDB eventually will be converted to "_id" * 3, defined as String type, if defined as Intege R may be an index of only 0, the occurrence of key duplication causes the database to plug in the case; * 4, the type is also the ID of the primary key in the mongorepository generic type*/@IdPrivateString CID; PrivateString FirstName; PrivateString Secondname; PublicString getcid () {returnCID; } Public voidsetcid (String CID) { This. CID =CID; } PublicString Getfirstname () {returnFirstName; } Public voidsetfirstname (String firstname) { This. FirstName =FirstName; } PublicString Getsecondname () {returnSecondname; } Public voidsetsecondname (String secondname) { This. Secondname =Secondname; }}
View Code
Description: the generated colletion (similar to the table in MySQL) is the simple class name of the domain class, Eg.customer.
Attention:
- CID: This field is used for MongoDB "_id" index
- Need @id annotations
- The name does not matter, anyway in MongoDB in the end will be converted to "_id"
- is defined as a string type, if it is defined as an integer, the index will only be 0, and key duplication will cause the database to be plugged in.
- The type is also the ID of the primary key in the mongorepository generic
2.2, Com.xxx.firstboot.mongo.CustomerRepository
PackageCom.xxx.firstboot.mongo;Importjava.util.List;Importorg.springframework.data.mongodb.repository.MongoRepository;ImportCom.xxx.firstboot.domain.Customer;/*** Mongorepository<customer, integer> * First parameter: T operation VO * second parameter: ID T primary KEY type * Function: This interface implements the Crud method * * Note: * 1, because the boot uses the Spring-data-mongodb, so we do not need to write the implementation of the interface, * when we run the program, SPRING-DATA-MONGODB will dynamically create * 2, findbysecondname naming is fastidious, Secondname (the property of the customer) will not find the property LastName error if it is changed to LastName*/ Public InterfaceCustomerrepositoryextendsMongorepository<customer, string> { PublicCustomer findbyfirstname (String firstname); PublicList<customer>findbysecondname (String secondname);}
View Code
Description: This interface is our business interface.
Attention:
- Inherit mongorepository<t, id> interface
- T: Operating domain, such as Com.xxx.firstboot.domain.Customer
- The primary key type of the Id:t (@ID decorated property), which is usually a string
- The implementation class for this interface also implements CRUD operations
- Our interface only needs to define the definition of the method, do not need to implement, SPRING-DATA-MONGODB will be dynamically created when the program is running
- The name of the method is fastidious, related to the attributes of domain (can be measured again)
2.3, Com.xxx.firstboot.web.CustomerController
PackageCom.xxx.firstboot.web;Importjava.util.List;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.RestController;ImportCom.xxx.firstboot.domain.Customer;Importcom.xxx.firstboot.mongo.CustomerRepository;ImportIo.swagger.annotations.Api;Importio.swagger.annotations.ApiOperation, @RestController @requestmapping ("/customer") @Api ("Customer-related APIs for testing MongoDB") Public classCustomercontroller {@AutowiredPrivatecustomerrepository customerrepository; @ApiOperation ("Add a Customer") @RequestMapping (value= "/addcustomer", method =requestmethod.get) PublicCustomer Addcustomer (@RequestParam ("FirstName") String FirstName, @RequestParam ("Secondname") String secondname) {Customer Customer=NewCustomer (); Customer.setfirstname (firstname); Customer.setsecondname (Secondname); returnCustomerrepository.save (customer); } @ApiOperation ("Get all the customer") @RequestMapping (value= "/getallcustomer", method =requestmethod.get) PublicList<customer>Getallcustomer () {returnCustomerrepository.findall (); } @ApiOperation ("Get Customer by FirstName") @RequestMapping (value= "/getcustomerbyfirstname", method =requestmethod.get) PublicCustomer Getcustomerbyfirstname (@RequestParam ("FirstName") String FirstName) {returnCustomerrepository.findbyfirstname (FirstName); } @ApiOperation ("Get multiple customer based on Secondname") @RequestMapping (value= "/getcustomerbysecondname", method =requestmethod.get) PublicList<customer> Getcustomerbysecondname (@RequestParam ("Secondname") String secondname) {returnCustomerrepository.findbysecondname (secondname); } @ApiOperation ("Delete Customer by ID") @RequestMapping (value= "/deletecustomerbyid", method =requestmethod.get) Public BooleanDeletecustomerbyid (@RequestParam ("CID") (String CID) {customerrepository.delete (CID); return true; }}
View Code
description : Inject directly into our own business interface, then do the appropriate operation.
At this point, you are ready to test. Only the default information for MongoDB is used at this time.
- Host:localhost
- port:27017
- Database: Test
- Collection:customer (Simple class name for domain Class)
2.4, Application.properties
1 #mongodb note:mongo3.x would not use the host and port,only use Uri2 spring.data.mongodb.host=192.168.21.543 spring.data.mong odb.port=270174 Spring.data.mongodb.uri=mongodb://192.168.21.54:27017/myfirstmongodb
Note : If you need to specify host, port, database, you need to configure the above information in the Application.properties file.
Attention:
- The configuration must be prefixed with "Spring.data.mongodb"
- If it is mongo3.x, the host and port are useless and require a URI. (not tested)
- URI = mongodb://host:port/Database
- Mongo2.x supports both of the above configuration methods
- mongo3.x only supports URI mode
3. Testing
Launch the app, start the MONGO service process, open swagger, and use Robomongo or Mongobooster client to observe MongoDB storage.
The property is not set in Application.properties.
After setting the properties,
Reference:
Https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-mongodb
https://spring.io/guides/gs/accessing-data-mongodb/example is an explanation of the sample code
http://www.jianshu.com/p/e59cd2dc5274 about the MongoDB primary key
Https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql.html about mongo2.x and 3.x to host, Support for port, URI configuration.
http://blog.didispace.com/springbootmongodb/primary key is a long URI user name, password configuration
11th Chapter Springboot + mongodb (simple query)