springBoot(22):集成mongodb
一、添加依赖
成都创新互联公司联系电话:18982081108,为您提供成都网站建设网页设计及定制高端网站建设服务,成都创新互联公司网页制作领域十年,包括成都雨棚定制等多个方面拥有丰富的网站营销经验,选择成都创新互联公司,为网站保驾护航!
org.springframework.boot spring-boot-starter-data-mongodb
二、配置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{ /** * 根据名称查找 * @Author: 我爱大金子 * @Description: 根据名称查找 * @Date: 14:12 2017/7/5 * @param name 名称 * @return */ List findByName(String name); /** * 根据名称分页查询 * @Author: 我爱大金子 * @Description: 分页查询 * @Date: 14:15 2017/7/5 * @param name 名称 * @param pageable 分页参数 * @return */ Page findByName(String name, Pageable pageable); }
测试:
网站名称:springBoot(22):集成mongodb
文章源于:http://scyanting.com/article/giegpd.html