Tell us about the origin of springboot and uncle lind.ddd
Mongodb is secondary encapsulated in Lind.DDD (Uncle's .net and .net core) and uses it as a kind of repository. For developers, only cure a few standard interfaces. In the springboot framework, it is somewhat similar to Uncle's lind, and it is also secondary encapsulated. Developers only need to pay attention to their own business. The standard curd operation is completed by springboot to help us implement. Generally, we will design an interface repository with entity objects, so that it can inherit the standard interface of mongo, and then inject the standard implementation into the springboot dependency injection framework. All of this is what the framework helps us implement!
Implement mongodb in the project
If the project needs to use mongodb to persist data, it can generally be achieved through the following steps:
1 Add package dependency build.gradle
compile('org.springframework.boot:spring-boot-starter-data-mongodb')If there is a unit test project, you can use embedded mongodb so that it does not need to communicate with external resources. How it works: download the mongodb package from remotely, start it, delete the generated collection after the test is completed
testCompile('de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.0.3')2 Add the default configuration item application.yml
spring: data: mongodb: uri: mongodb://192.168.99.100:27017/Test password: guest username: guest
3Add the entity class corresponding to the mongodb collection
/** * Address. */@Data@NoArgsConstructor@AllArgsConstructorpublic class Address { /** * Number. */ @Id private String id; /** * Province. */ private String province; /** * City. */ private String city; /** * District. */ private String district; /** * Status. */ private Status status;}4 Add the repository class corresponding to the entity. It needs to inherit the extendeds standard mongodb repository. At the same time, Mongodb repository supports automatic convention methods. Developers can use @Query annotation to determine the returned field list, which is very necessary for big data documents. If the default interface cannot meet our requirements, we need to define a personalized interface and implement it. In the following example, our AddressExtRepository is a personalized interface. Our external interface AddressRepository needs to inherit it. Note that the uncle believes that this has cracked the object-oriented opening and closing principle. Yuan Fang, what do you think!
/** * The address warehousing interface provided to the outside, inheriting all address-related interfaces. */public interface AddressRepository extends AddressExtRepository, MongoRepository<Address, String> { /** * Get the address list based on the province. * * @param province * @return */ @Query(fields = "{'province': 0}") List<Address> findAddressesByProvince(String province); /** * Get the address list based on the province and province. * * @param province * @param city * @return */ @Query()//fields indicates the field contained in List<Address> findAddressesByProvinceAndCityAndDistict(String province, String city, String district);}Personalized warehousing implementation, using MongoTemplate object to interact with the mongodb database!
/** * Repository implementation of special rules. */public class AddressExtRepositoryImpl implements AddressExtRepository { @Autowired MongoTemplate mongoTemplate; @Override public Address findByProvinceAndCity(String province, String city) { Query query = new Query(Criteria.where("province").is(province).and("city").is(city)); return mongoTemplate.findOne(query, Address.class, "address"); }}5. In the controller, you can access the storage and business objects directly through the @Autowired annotation.
@RestControllerpublic class MongoController { // Repository. @Autowired private AddressRepository repository; // User business. @Autowired private UserService userService; /** * Get the address list. * * @return */ @RequestMapping("/address/{province}") public Address getAddress(@PathVariable("province") String province) { System.out.println("1,province=" + province); return userService.getAddress(province); }}Summarize
The above is a detailed explanation of the integration and usage examples of springboot Mongodb introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!