1.pom文件引入mongodb依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.配置文件(application.properties)配置mongodb的数据库信息
#mogodb配置
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=test
3.创建dto,service,dao,controller类
dto:
@Id
public String id;
public String name;
dao:
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String name);
public List<Customer> findByLastName(String name);
}
controller:
@RequestMapping(value = "/getId")
public String getId(HttpServletRequest req, HttpServletResponse resp) {
Customer customer=new Customer("aaa");
return customerRepository.save(customer).getId();
}