Using the @repository interface provided by Springboot, you can accomplish the custom work that once required a lot of code writing and configuration files. These configurations, which previously gave novice programmers a headache and were proud of an experienced programmer, have become less important due to the continuous improvement of the framework and have also improved programmer productivity.
This article describes how to manipulate MongoDB through Springboot.
I. Configure Pom.xml First
<parent> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-parent</artifactid> <version>1.4.1.RELEASE</version> < RelativePath/> <!--lookup parent from repository--></parent> <dependencies> < dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-web</artifactid> </dependency> <dependency> <groupId> Org.springframework.boot</groupid> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> < /dependencies>
Two. Configuring the MongoDB configuration in Application.properties
Spring.data.mongodb.uri=mongodb://root:[email Protected]:27017/test
Three. Declaration of entity and repository
@Document (collection = "T_app") @Datapublic class app{ @Id private ObjectId Id; @Field ("Api_key") private String ApiKey; Private String appname; private list<object> activities;}
@Repositorypublic interface Apprepository extends Mongorepository<app, objectid>{ App Findonebyapikey ( String apiKey);}
Four. Supported repository
Ps:keyword can be linked with and methods.
Such as:
list<discountcode> FindFirst5byActivityidinandendtimeafterandStatus( List<objectid> Activityids, Date endtime,string status);
Five. Repository Declaration and use
@Autowiredprivate apprepository apprepository;
Spring Boot MongoDB JPA simplifies development