springboot整合mongodb增删改查入门
生活随笔
收集整理的這篇文章主要介紹了
springboot整合mongodb增删改查入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 1.配置與依賴
- 2.實體與repository層
- 3.測試
適合新手。
要求:安裝好mongodb
代碼和運行程序如下:
目錄:
1.配置與依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency> spring.data.mongodb.uri=mongodb://127.0.0.1:27017/test2.實體與repository層
public class User implements Serializable {private static final long serialVersionUID = -3258839839160856613L;private Long id;private String userName;private String passWord;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassWord() {return passWord;}public void setPassWord(String passWord) {this.passWord = passWord;}@Overridepublic String toString() {return "UserEntity{" +"id=" + id +", userName='" + userName + '\'' +", passWord='" + passWord + '\'' +'}';} }Repository
public interface UserRepository {public void saveUser(User user);public User findUserByUserName(String userName);public long updateUser(User user);public void deleteUserById(Long id);}接口實現類
package com.neo.repository.impl;import com.mongodb.client.result.UpdateResult; import com.neo.repository.UserRepository; import com.neo.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Component;/*** Created by summer on 2017/5/5.*/ @Component public class UserRepositoryImpl implements UserRepository {@Autowiredprivate MongoTemplate mongoTemplate;/*** 創(chuàng)建對象* @param user*/@Overridepublic void saveUser(User user) {mongoTemplate.save(user);}/*** 根據用戶名查詢對象* @param userName* @return*/@Overridepublic User findUserByUserName(String userName) {Query query=new Query(Criteria.where("userName").is(userName));User user = mongoTemplate.findOne(query , User.class);return user;}/*** 更新對象* @param user*/@Overridepublic long updateUser(User user) {Query query=new Query(Criteria.where("id").is(user.getId()));Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord());//更新查詢返回結果集的第一條UpdateResult result =mongoTemplate.updateFirst(query,update,User.class);//更新查詢返回結果集的所有// mongoTemplate.updateMulti(query,update,UserEntity.class);if(result!=null)return result.getMatchedCount();elsereturn 0;}/*** 刪除對象* @param id*/@Overridepublic void deleteUserById(Long id) {Query query=new Query(Criteria.where("id").is(id));mongoTemplate.remove(query,User.class);} }3.測試
@RunWith(SpringRunner.class) @SpringBootTest public class UserRepositoryTest {@Autowiredprivate UserRepository userDao;@Testpublic void testSaveUser() throws Exception {User user=new User();user.setId(2l);user.setUserName("小明");user.setPassWord("fffooo123");userDao.saveUser(user);}@Testpublic void findUserByUserName(){User user= userDao.findUserByUserName("小明");System.out.println("user is "+user);}@Testpublic void updateUser(){User user=new User();user.setId(2l);user.setUserName("天空");user.setPassWord("fffxxxx");userDao.updateUser(user);}@Testpublic void deleteUserById(){userDao.deleteUserById(1l);}}運行testSaveUser
更新:
總結
以上是生活随笔為你收集整理的springboot整合mongodb增删改查入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot使用Redis作缓存
- 下一篇: ubuntu18.04(修改环境变量