一、pom.xml中引入
<!-- https://mvnrepository.com/artifact/org.mongodb.morphia/morphia -->
<dependency>
<groupId>org.mongodb.morphia</groupId>
<artifactId>morphia</artifactId>
<version>1.3.0</version>
</dependency>
二、新增配置类
这个配置类初始化了Datastore对象,它封装了对数据库的操作方法。使用时通过@Autowired注入即可。
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* mongodb配置
*/
@Configuration
public class MorphiaConfig {
private MongoClient mongoClient;
@Value("${spring.data.mongodb.database}")
private String database;
/**
* 设置连接最大空闲时间(到达时间,连接关闭)
* @return mongo client属性
*/
@Bean
public MongoClientOptions mongoClientOptions() {
return MongoClientOptions.builder()
.maxConnectionIdleTime(6000 * 5)
.maxConnectionLifeTime(0)
.build();
}
@Bean
public Datastore datastore(@Autowired MongoClient mongoClient) {
Morphia res = new Morphia();
// 确定Mongo实体类的存放包名
res.mapPackage("com.xoiot.mongo.entity");
Datastore datastore = res.createDatastore(mongoClient,database);
// 为实体类创建索引
datastore.ensureIndexes();
return datastore;
}
}
三、实体类
实体类对应着MongoDB中的文档
package com.xoiot.mongo.entity;
import java.util.Date;
import java.util.Map;
public class DevData {
private String _id;
private String eui;
private String data;
private Date createTime;
private Date updateTime;
private Integer count;
private Map<String,Object> map;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getEui() {
return eui;
}
public void setEui(String eui) {
this.eui = eui;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
@Override
public String toString() {
return "DevData{" +
"_id='" + _id + '\'' +
", eui='" + eui + '\'' +
", data='" + data + '\'' +
", createTime=" + createTime +
", updateTime=" + updateTime +
", count=" + count +
", map=" + map +
'}';
}
}
四、对数据库进行增删改差
1.新增
//创建对象
DevData devData = new DevData();
//封装数据
devData.setData(value);
devData.setEui(UUID.randomUUID().toString().replace("-",""));
devData.setCreateTime(new Date());
devData.setUpdateTime(new Date());
devData.setCount(1);
Map<String, Object> map = new HashMap<>();
map.put("name", "zyf");
map.put("age", "23");
devData.setMap(map);
//执行新增
Key<DevData> save = datastore.save(devData);
2.删除
//先查询对应eui的数据,再将该数据删除
Query<DevData> query = datastore.createQuery(DevData.class).filter("eui = ", eui);
WriteResult delete = datastore.delete(query);
3.更新
//查询记录
Query<DevData> query = datastore.createQuery(DevData.class).filter("eui = ", devData.getEui());
//创建更新实例
UpdateOperations<DevData> updateOperations = datastore.createUpdateOperations(DevData.class)
.set("eui",devData.getEui())
.set("data",devData.getData())
.set("updateTime", new Date())
.inc("count");
//更新
UpdateResults update = datastore.update(query, updateOperations);
4.查询
//普通查询
Query<DevData> query = datastore.createQuery(DevData.class).field("eui").equal(eui);
// Query<DevData> query = datastore.createQuery(DevData.class).filter("eui = ", eui);
DevData devData = query.get();
//分页查询 page页数,size每页条数
int skip = size*(page-1);//要跳过的条数
List<DevData> devDatas = datastore.find(DevData.class)
.asList(new FindOptions().limit(size).skip(skip));
//根据多层文档的条件查询
List<MQTTMessage> mqttMessages = datastore.find(MQTTMessage.class)
.filter("createTime <", DateUtil.parseDate(date)).asList();