1 using MAVEN to introduce MongoDB dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2 configuring MongoDB, default DB is Test
spring.data.mongo.host=127.0.0.1
spring.data.mongo.port=27017
Spring.data.mongo.database=test
3 Developing PO entity classes
public class Item {
@Id
Private String ID;
private String name;
Private String Price;
/**getters and setters*/
}
4 implements the Mongorepository interface, which needs to specify a specific generic
Public interface Itemrepository extends Mongorepository<item, string> {
Item findbyname (String name);
}
5 Invocation Mode
public class Itemcontroller {
@Autowired
Private Itemrepository itemrepository;
@RequestMapping ("/additem")
@ResponseBody
public string AddItem (string paramstr) {
Jsonobject data = new Jsonobject ();
Item item = new Item ();
Item.setname ("Item 1");
Item.setprice ("5.95");
try {
Itemrepository.save (item);
} catch (Exception e) {
Data.put ("Error", "ADD item Error");
}
return data.tostring ();
}
Springboot Configuring MongoDB