Springboot Configuring MongoDB

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.