本文是springboot結合SpringBoot starter-data-mongodb 進行增刪改查 快速入門教程java
1、準備linux
請參考如下幾篇文章git
手把手 linux 下 MongoDB 的安裝spring
手把手 linux 下 MongoDB 的使用(一)mongodb
2、添加依賴springboot
在POM 中添加以下依賴app
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
3、配置文件函數
在配置文件 application.yml 添加以下配置spring-boot
spring: data: mongodb: #單機配置 host: 192.168.1.1:27017 #指定操做的數據庫 #authenticationDatabase: admin username: admin password: 123456 # 多個IP集羣的配置: # uri://admin:123456@192.168.252.121:20000,192.168.252.122:20000,192.168.252.12:20000/demo
4、實體類配置
實體映射是經過MongoMappingConverter這個類實現的。它能夠經過註釋把java類轉換爲mongodb的文檔。
有如下幾種註釋:
@Id - 文檔的惟一標識,在mongodb中爲ObjectId,它是惟一的,經過時間戳+機器標識+進程ID+自增計數器(確保同一秒內產生的Id不會衝突)構成。
@Document - 聲明此類爲mongodb的文檔實體類,經過collection參數指定這個類對應的文檔名稱。@Document(collection=」mongodb」) mongodb對應表
@Indexed - 聲明該字段須要索引,建索引能夠大大的提升查詢效率。
@CompoundIndex - 複合索引的聲明,建複合索引能夠有效地提升多字段的查詢效率。
@Transient - 映射忽略的字段,該字段不會保存到mongodb。
@PersistenceConstructor - 聲明構造函數,做用是把從數據庫取出的數據實例化爲對象。該構造函數傳入的值爲從DBObject中取出的數據
@Data @Document(collection = "demo_entity_collection") public class DemoEntity implements Serializable { @Id private Long id; @Field("title") private String title; @Field("description") private String description; @Field("by") private String by; @Field("url") private String url; }
5、使用用例
@RunWith(SpringRunner.class) @SpringBootTest(classes = App.class) public class AppTest { @Autowired private MongoTemplate mongoTemplate; /** * 添加數據 */ @Test public void saveDemoTest() { DemoEntity demoEntity = new DemoEntity(); demoEntity.setId(1L); demoEntity.setTitle("Spring Boot 中使用 MongoDB"); demoEntity.setDescription("關注公衆號,小羅技術筆記,專一於開發技術的研究與知識分享"); demoEntity.setBy("xiaoluo"); demoEntity.setUrl("http://www.yuwowugua.com"); mongoTemplate.save(demoEntity); // mongoTemplate.insert(demoEntity); } /** * 刪除數據 */ @Test public void removeDemoTest() { //經過id 刪除 mongoTemplate.remove(2L); //經過其它惟一標識值刪除 Query queryDelete = new Query().addCriteria(Criteria.where("caseCode").is("1314235246")); mongoTemplate.remove(queryDelete,DemoMapEntity.class); } /** * 更新數據 */ @Test public void updateDemoTest() { DemoEntity demoEntity = new DemoEntity(); demoEntity.setId(1L); demoEntity.setTitle("Spring Boot 中使用 MongoDB 更新數據"); demoEntity.setDescription("關注公衆號,小羅技術筆記,專一於開發技術的研究與知識分享"); demoEntity.setBy("xiaoluo"); demoEntity.setUrl("http://www.yuwowugua.com"); Query query = new Query(Criteria.where("id").is(demoEntity.getId())); Update update = new Update(); update.set("title", demoEntity.getTitle()); update.set("description", demoEntity.getDescription()); update.set("by", demoEntity.getBy()); update.set("url", demoEntity.getUrl()); mongoTemplate.updateFirst(query, update, DemoEntity.class); } /** * 查詢數據 */ @Test public void findDemoByIdTest() { Query query = new Query(Criteria.where("id").is(1L)); DemoEntity demoEntity = mongoTemplate.findOne(query, DemoEntity.class); System.out.println(JSONObject.toJSONString(demoEntity)); } /** * map集合追加數據 */ @Test public void pushMapDemo() { DemoMapEntity demoMapEntity = new DemoMapEntity(); demoMapEntity.setCaseCode("1314235246"); Map<String, List<String>> map = new HashMap<>(); List<String> list = new ArrayList<>(); list.add("1"); list.add("2"); map.put("data.valuation", list); demoMapEntity.setData(map); mongoTemplate.save(demoMapEntity); Update update = new Update(); List<String> list2 = new ArrayList<>(); list2.add("3"); list2.add("4"); update.push("data.valuation", list2); Query queryUpdate = new Query().addCriteria(Criteria.where("caseCode").is(demoMapEntity.getCaseCode())); mongoTemplate.updateFirst(queryUpdate, update, DemoMapEntity.class); } }
6、源碼下載
碼雲:https://gitee.com/luoluo1995/...