您好,登錄后才能下訂單哦!
一、添加依賴
<!-- mongodb --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
二、配置application.properties
###########################mongodb開始#################################### spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=db_test ###########################mongodb結束####################################
三、創建實體類
package com.example.demo.pojo; import java.util.Date; /** * 用戶信息 * * @Author: 我愛大金子 * @Description: 用戶信息 * @Date: Create in 14:09 2017/7/5 */ public class User { private int id; private String name; private Date createTime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", createTime=" + createTime + '}'; } }
四、使用MongoTemplate實現
MongodbComponent.java
package com.example.demo.utils.component; import com.example.demo.pojo.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; /** * Mongodb工具類 * * @Author: 我愛大金子 * @Description: Mongodb工具類 * @Date: Create in 13:48 2017/7/5 */ @Component public class MongodbComponent { @Autowired private MongoTemplate mongoTemplate; public void insert(User user) { mongoTemplate.insert(user); } public void deleteById(int id) { Criteria criteria = Criteria.where("id").in(id); Query query = new Query(criteria); mongoTemplate.remove(query, User.class); } public void updateById(User user) { Criteria criteria = Criteria.where("id").in(user.getId()); Query query = new Query(criteria); Update update = new Update(); update.set("name", user.getName()); update.set("createTime", user.getCreateTime()); mongoTemplate.updateMulti(query, update, User.class); } public User selectById(int id) { Criteria criteria = Criteria.where("id").in(id); Query query = new Query(criteria); return mongoTemplate.findOne(query, User.class); } }
MongoTemplateTest.java(測試)
package com.example.demo; import com.example.demo.pojo.User; import com.example.demo.utils.component.MongodbComponent; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import java.util.Date; /** * MongoTemplate測試 * * @Author: 我愛大金子 * @Description: MongoTemplate測試 * @Date: Create in 13:54 2017/7/5 */ public class MongoTemplateTest extends ApplicationTests { @Autowired private MongodbComponent mongodbComponent; @Test public void insert() { User user = new User(); user.setId(1); user.setName("張三"); user.setCreateTime(new Date()); mongodbComponent.insert(user); } @Test public void select() { System.out.println(mongodbComponent.selectById(1)); } @Test public void update() { User user = new User(); user.setId(1); user.setName("李四"); user.setCreateTime(new Date()); mongodbComponent.updateById(user); System.out.println(mongodbComponent.selectById(1)); } @Test public void delete() { mongodbComponent.deleteById(1); } }
五、使用接口繼承MongoRepository
UserDao.java
package com.example.demo.dao; import com.example.demo.pojo.User; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.mongodb.repository.MongoRepository; import java.util.List; /** * 用戶dao * * @Author: 我愛大金子 * @Description: 用戶dao * @Date: Create in 14:09 2017/7/5 */ public interface UserDao extends MongoRepository<User, Integer> { /** * 根據名稱查找 * @Author: 我愛大金子 * @Description: 根據名稱查找 * @Date: 14:12 2017/7/5 * @param name 名稱 * @return */ List<User> findByName(String name); /** * 根據名稱分頁查詢 * @Author: 我愛大金子 * @Description: 分頁查詢 * @Date: 14:15 2017/7/5 * @param name 名稱 * @param pageable 分頁參數 * @return */ Page<User> findByName(String name, Pageable pageable); }
測試:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。