千家信息网

springBoot(22):集成mongodb

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,一、添加依赖 org.springframework.boot spring-boot-starter-data-mongodb二、配置application.
千家信息网最后更新 2025年01月23日springBoot(22):集成mongodb

一、添加依赖

   org.springframework.boot   spring-boot-starter-data-mongodb

二、配置application.properties

###########################mongodb开始####################################spring.data.mongodb.host=localhostspring.data.mongodb.port=27017spring.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 */@Componentpublic 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);}

测试:


0