注意 我们想从mongoDB里面拿东西 不能restAPI controller layer直接请求然后从mongoDB里面拿。而是需要REST API发送请求给service service layer再从DAO里面拿, DAO就是spring Data MongoDB, 然后spring data mongo DB 才可以从DB里面直接拿。
还是少不了这么几个:
在maven的pom.xml中田间dependency: Spring Data MongoDB
在application.preperties中添加config
然后我们需要implement table model. (mongoDB有table一说?)
basically is create the table/entity class, use the annotation @document. ----Not @Entity
如果MongoDB里面有对应的表 那么就对应 没有的话我们就创建一个这样schema的table.
然后我们需要一个DAO层(xxxRepository.java) 负责Object和DB之间的转化 所以我们的API也要搞在这一层 API用于做CRUD 既然CRUD 就涉及到各种query
public interface ItemRepository extends MongoRepository{ @Query("{name:'?0'}") GroceryItem findItemByName(String name); //many of the find() related query, is actually uses the **MongoTemplate** to execute @Query(value="{category:'?0'}", fields="{'name' : 1, 'quantity' : 1}") List findAll(String category); public long count(); }
然后create application class用来运行这个模块(是这个微服务的入口):
会用到一个重要的cnnotation:
@SpringBootApplication: 这个annotation 会自动做下面的:@Configuration @EnableAutoConfig @ComponentScan
main function use SpringApplication.run() to launch the application.
if you want more control over the registration process, you can use the @EnableMongoRepositories.



