SpringBoot中MongoDB注解概念及使用

maven依赖

//响应式依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb-reactive -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
    <version>2.4.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
    <version>2.4.0</version>
</dependency>

@Id

主键,不可重复,自带索引,可以在定义的列名上标注,需要自己生成并维护不重复的约束。如果自己不设置@Id主键,mongodb会自动生成一个唯一主键。

@Document

标注在实体类上,标明由mongodb来维护该表。

import org.springframework.data.mongodb.core.mapping.Document;
把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档,不指定collection参数即默认类名作为 mongodb 对应 collection 名
@Document(collection="mongodb 对应 collection 名")      

// 若未加 @Document ,该 bean save 到 mongodb 的 user collection
// 若添加 @Document ,则 save 到 reUser collection
@Document(collection="reUser") 
public class User{
}

@Indexed

声明该字段需要加索引,加索引后以该字段为条件检索将大大提高速度。
唯一索引的话是@Indexed(unique = true)。
也可以对数组进行索引,如果被索引的列是数组时,MongoDB会索引这个数组中的每一个元素。
也可以对整个Document进行索引,排序是预定义的按插入BSON数据的先后升序排列。

@CompoundIndex
复合索引,加复合索引后通过复合索引字段查询将大大提高速度。

@Document
@CompoundIndexes({
    @CompoundIndex(name = "age_idx", def = "{'lastName': 1, 'age': -1}")
})
public class Person<T extends Address> {
}


//2dsphere索引支持在地球状球体计算几何的查询。
@Document
@CompoundIndexes({
    @CompoundIndex(name = "loc_index", def = "{'loc': '2dsphere'}"),
})
public class LastLoc {
	@Id
	private String entityId;
	private String entityType;
	private GeoJsonPoint loc; 

}

写法如上,lastName和age将作为复合索引,数字参数指定索引的方向,1为正序,-1为倒序。方向对单键索引和随机存不要紧,但如果你要执行分组和排序操作的时候,它就非常重要了。

@Field

代表一个字段,可以不加,不加的话默认以参数名为列名。

// 给映射存储到 mongodb 的字段取别名
// 在 java bean 中字段名为 entityType,存储到 mongodb 中 key 为 entity
@Field("entity")
private String entityType;

@Transient

被该注解标注的,将不会被录入到数据库中。只作为普通的javaBean属性。

@DBRef
设置对象的关联
两个document结构如下:

@Data
@Document(collection = "main")
public class Main {
 
    @Id
    private String id;
 
    private String name;
 
    private String age;
 
    private String gender;
 
    @DBRef
    private Ref ref;
}
@Data
@Document(collection = "ref")
public class Ref {
 
    @Id
    private String id;
 
    private String address;
 
    private String father;
 
    private String mother;
}

测试方法

@Test
public void testInsert() {
 
    Ref ref = new Ref();
    ref.setAddress("滨江火炬大道");
    ref.setFather("zhang");
    ref.setMother("wang");
    refRepository.save(ref).subscribe();
 
    Main main = new Main();
    main.setName("main");
    main.setAge("18");
    main.setGender("男");
    main.setRef(ref);
    mainRepository.save(main).subscribe();
}

插入报错

Caused by: org.springframework.data.mapping.MappingException: Cannot create a reference to an object with a NULL id.

要保证被ref的实体先于引用ref的实体生成,那么修改成如下形式
在这里插入图片描述

数据格式
在这里插入图片描述
测试查询主实体

@Test
    public void getMain() {
        Mono<Main> byId = mainRepository.findById("5bed07c946ce5e02f8cd0ee9");
        Main main = byId.block();
        System.out.println("main = " + main);
    }
main = Main(id=5bed07c946ce5e02f8cd0ee9, name=main, age=18, gender=, ref=Ref(id=5bed07c946ce5e02f8cd0ee8, address=滨江火炬大道, father=zhang, mother=wang))
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值