MongoDB框架简单使用教程
- 环境 mongodb7 springboot3 java17
依赖
```xml
<dependencies>
<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>morphia-core</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15to18</artifactId>
<version>1.78.1</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bc-fips</artifactId>
<version>1.0.2.5</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bctls-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>auth</artifactId>
<version>2.20.0</version>
</dependency>
</dependencies>
<repositories>
<!-- mogodb 框架快照仓库 -->
<repository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
```
配置: 配合spring使用可以将Store注入容器中使用
- MongoConfig.java
public class MongoConfig {
private static String url = "mongodb://localhost:27017";
private MongoClient mongoClient;
private Datastore datastore;
public void buildMongo(){
createMongoClient();
createMongoDatastore();
}
public void createMongoClient() {
mongoClient = MongoClients.create(builder()
.uuidRepresentation(STANDARD)
.applyConnectionString(new ConnectionString(url))
.build());
}
public void createMongoDatastore(){
Security.addProvider(new BouncyCastleProvider());
Security.addProvider(new BouncyCastleJsseProvider());
MorphiaConfig morphiaConfig = MorphiaConfig.load("morphia-config.properties");
datastore = Morphia.createDatastore(mongoClient,morphiaConfig);
}
public void stop() {
mongoClient.close();
}
}
- morphia-config.properties
######
# default=false
######
morphia.apply-caps=false
######
# default=false
######
morphia.apply-document-validations=false
######
# default=false
######
morphia.apply-indexes=false
######
# Optional
######
morphia.codec-provider=
######
# default=camelCase
# possible values=camelCase, identity, kebabCase, lowerCase, snakeCase, fqcn
######
morphia.collection-naming=camelCase
######
# default=morphia
######
morphia.database=连接数据库名
######
# default=utc
# possible values=utc, system_default
######
morphia.date-storage=utc
######
# default=simpleName
# possible values=className, lowerClassName, lowerSimpleName, simpleName, fqcn
######
morphia.discriminator=simpleName
######
# default=_t
######
morphia.discriminator-key=_t
######
# default=false
######
morphia.enable-polymorphic-queries=false
######
# default=false
######
morphia.ignore-finals=false
######
# default=.*
######
morphia.packages=.*
######
# default=fields
# possible values=fields, methods
######
morphia.property-discovery=fields
######
# default=identity
# possible values=camelCase, identity, kebabCase, lowerCase, snakeCase, fqcn
######
morphia.property-naming=identity
######
# default=dev.morphia.query.DefaultQueryFactory
######
morphia.query-factory=dev.morphia.query.DefaultQueryFactory
######
# default=false
######
morphia.store-empties=false
######
# default=false
######
morphia.store-nulls=false
实体类
```java
@Data
@Entity("login") // 集合名称
@Indexes(
@Index(fields = @Field("salary")) // 设置索引,可设置多个
)
public class Login {
// id
@Id
private ObjectId id;
// 其他集合可作为本集合对象,用Reference修饰
@Reference
private Employee manager;
// 可自定义存储在集合中的字段名
@Property("money")
private Double salary;
// 正常情况可无需注解
private Double day;
private String username;
private String password;
public Login(Message.Entity entity){
Message.User user = entity.getUser();
this.username = user.getUsername();
this.password = user.getPassword();
}
}
```
基本操作
private Message.Entity create(Message.Entity entity) {
Login login = Login.fromEntity(entity);
if (null != login.getPassword() && null != login.getUsername()) {
datastore.insert(login);
return entity;
}else {
return Message.Entity.newBuilder().setCode(500).build();
}
}
private Message.Entity valid(Message.Entity entity) {
System.out.println(entity.getUser().getPassword()+" "+entity.getUser().getUsername());
Query<Login> logins = datastore.find(Login.class);
MorphiaCursor<Login> userIter = logins.filter(Filters
.eq("username", entity.getUser().getUsername()))
.iterator();
if (null == userIter || !userIter.hasNext()) {
return errorMsg("");
}
userIter.hasNext().next();
System.out.println(entity.toByteArray().length);
return entity;
}