相关推荐recommended
MongDB之整合SpringBoot
作者:mmseoamin日期:2024-01-23

MongoTemplate 方式

依赖

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

创建账户

# 切换到数据库
use study
# 创建账户
db.createUser({user:"root",pwd:"123456",roles:[{role:"dbOwner",db:"study"}]})

配置文件

spring.data.mongodb.host=192.168.204.156
spring.data.mongodb.port=27017
spring.data.mongodb.database=study
spring.data.mongodb.password=123456
spring.data.mongodb.username=root

测试

@SpringBootTest(classes = {SpringbootMongodbDemoApplication.class})
public class Main {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Test
    public void testAddData() {
        // 添加
        Goods goods = Goods.builder().qty(100).item("hello").status("B").size(new Size(165.0F, 53.4F, "cm")).build();
        mongoTemplate.save(goods, "goods");
        // 查询
        List list = mongoTemplate.findAll(Goods.class);
        list.forEach(item -> System.out.println(item.toString()));
    }
}
@Data
@Builder
public class Goods {
    private String id;
    private String item;
    private Integer qty;
    private Size size;
    private String status;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Size {
    private Float h;
    private Float w;
    private String uom;
}

增删改查

import com.example.springbootmongodbdemo.SpringbootMongodbDemoApplication;
import com.example.springbootmongodbdemo.model.Goods;
import com.example.springbootmongodbdemo.model.Size;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
@SpringBootTest(classes = {SpringbootMongodbDemoApplication.class})
public class Main {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Test
    public void addData() {
        Goods build = Goods.builder().qty(100).item("hello").status("B").size(new Size(165.0F, 53.4F, "cm")).build();
        mongoTemplate.save(build, "goods");
    }
    @Test
    public void findAll() {
        List all = mongoTemplate.findAll(Goods.class);
        all.forEach(item -> System.out.println(item.toString()));
    }
    @Test
    public void findById() {
        Goods build = Goods.builder().id("63bbdac2fa9405a15160f476").build();
        Query query = new Query(where("id").is(build.getId()));
        List goods = mongoTemplate.find(query, Goods.class);
        System.out.println(goods.toString());
    }
    @Test
    public void findBy() {
        // 匹配r结尾的数据
        Query query = new Query(where("item").regex("^.*r$"));
        List goods = mongoTemplate.find(query, Goods.class);
        System.out.println(goods.toString());
    }
    @Test
    public void update() {
        Goods build = Goods.builder().id("63bbdac2fa9405a15160f476").build();
        Query query = new Query(where("id").is(build.getId()));
        Update update = Update.update("item", "canvas02");
        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, Goods.class);
        System.out.println(updateResult);
    }
    @Test
    public void del() {
        Goods build = Goods.builder().id("63d0eff1d20d7f153665244d").build();
        Query query = new Query(where("id").is(build.getId()));
        DeleteResult remove = mongoTemplate.remove(query, Goods.class);
        System.out.println(remove);
    }
}

MongoRepository 方式

依赖&配置

同上。

实体类

// 关键注解:@Document,@Id
@Data
@Builder
@Document("goods")
public class Goods {
    @Id
    private String id;
    private String item;
    private Integer qty;
    private Size size;
    private String status;
}

接口类

public interface GoodsRepository extends MongoRepository {
}

测试

@SpringBootTest(classes = {SpringbootMongodbDemoApplication.class})
public class Main {
    @Resource
    private GoodsRepository goodsRepository;
    @Test
    public void testAddData() {
        Goods build = Goods.builder().qty(100).item("hello").status("B").size(new Size(165.0F, 53.4F, "cm")).build();
        Goods save = goodsRepository.save(build);
        System.out.println(save);
    }
}