您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
一、添加依赖
<!-- 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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。