楼主手残,开发工具一般更喜欢用IDEA。
IDEA新建项目,NEXT选好JDK,包的路径啥的。到这一步的时候注意了呀老哥们,web选择spring web ,NoSQL选择符合标题的spring Data MongoDB,然后就NEXT到最后,这样IDEA就自动帮你把你需要的依赖自己导进来了,舒服。
主要是这两个依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
好了,接下来开始搭建你的代码结构,控制层,实体层啥的整起来,放一个我自己的。比如这样
接下来整一下配置文件里的
接下来去到springboot的启动类 添加 @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) 注解,将配置文件的数据加载到spring,否则会读取不到配置的MongoDB信息。
…
…
…
好像完事了。网上大把MongoDB的增删改查demo,拿来丢进去就完事。
丢一份弟弟的吧,弟弟我的也是网上参考过来的。
实体层
(注意一点就是注解@Document里边是collection 而不是 collation,弟弟我在这个坑里边待挺久)
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "historical_data") //mongodb对应表
public class IotHistoricalData {
@Id
private String id;
private String generateTime;
private String tagId;
private String tagName;
private Double tagData;
private String baseId;
private String baseName;
private String areaId;
private String areaName;
private String deviceId;
private String deviceName;
private String channelId;
private String channelName;
private String landCode;
private String landName;
省略set get
Dao层
(都是这些增删改查的处理接口)
import com.suipianny.iotmongo.entity.IotHistoricalData;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface IotHistoricalDataDao {
void saveData(IotHistoricalData iotHistoricalData);
void removeData(String id);
void updateData(IotHistoricalData iotHistoricalData);
IotHistoricalData findDataById(String id);
List<IotHistoricalData> findAllData();
List<IotHistoricalData> findPageData(int pageNo,int pageSize);
}
service层,接口对应的逻辑实现代码
(这里注意的是MongoTemplate 这玩意,springboot对mongoDB数据库的操作主要通过MongoTemplate来进行)
import com.suipianny.iotmongo.dao.IotHistoricalDataDao;
import com.suipianny.iotmongo.entity.IotHistoricalData;
import org.springframework.data.domain.Sort;
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;
import javax.annotation.Resource;
import java.util.List;
@Component
public class IotHistoricalDataDaoImpl implements IotHistoricalDataDao {
@Resource
private MongoTemplate mongoTemplate;
/**
* 新增数据
* */
@Override
public void saveData(IotHistoricalData iotHistoricalData) {
mongoTemplate.save(iotHistoricalData);
}
/**
* 删除数据
* */
@Override
public void removeData(String id){
IotHistoricalData iotHistoricalData = new IotHistoricalData();
iotHistoricalData.setId(id);
mongoTemplate.remove(iotHistoricalData);
}
/**
* 更新数据
* */
@Override
public void updateData(IotHistoricalData iotHistoricalData) {
Query query = new Query(Criteria.where("id").is(iotHistoricalData.getId()));
Update update = new Update();
update.set("generateTime", iotHistoricalData.getGenerateTime());
update.set("tagId", iotHistoricalData.getTagId());
update.set("tagName", iotHistoricalData.getTagName());
update.set("tagData", iotHistoricalData.getTagData());
update.set("baseId", iotHistoricalData.getBaseId());
update.set("baseName", iotHistoricalData.getBaseName());
update.set("areaId", iotHistoricalData.getAreaId());
update.set("areaName", iotHistoricalData.getAreaName());
update.set("deviceId", iotHistoricalData.getDeviceId());
update.set("deviceName", iotHistoricalData.getDeviceName());
update.set("channelId", iotHistoricalData.getChannelId());
update.set("channelName", iotHistoricalData.getChannelName());
update.set("landCode", iotHistoricalData.getLandCode());
update.set("landName", iotHistoricalData.getLandName());
mongoTemplate.updateFirst(query, update, IotHistoricalData.class);
}
/**
* 查询单条数据
* */
@Override
public IotHistoricalData findDataById(String id) {
Query query = new Query(Criteria.where("id").is(id));
IotHistoricalData iotHistoricalData = mongoTemplate.findOne(query, IotHistoricalData.class);
return iotHistoricalData;
}
/**
* 查询全部数据
* */
@Override
public List<IotHistoricalData> findAllData(){
Query query = new Query();
List<IotHistoricalData> iotHistoricalData = mongoTemplate.find(query,IotHistoricalData.class,"historical_data");
return iotHistoricalData;
}
/**
* 分页查询数据
* */
@Override
public List<IotHistoricalData> findPageData(int pageNo,int pageSize){
Query query = new Query().skip((pageNo-1)*pageSize).limit(pageSize); //初始化分页
query.with(Sort.by(Sort.Direction.DESC,"generateTime")); //按照时间降序排序
List<IotHistoricalData> iotHistoricalData = mongoTemplate.find(query,IotHistoricalData.class,"historical_data");
return iotHistoricalData;
}
}
最后就是自己的业务逻辑controller控制层了
(简单的实现一个新增的功能)
@Controller
@ResponseBody
@RequestMapping("/iotData")
public class IotHistoricalDataController {
@Autowired
private IotHistoricalDataDao dataDao;
/**
* 测试保存数据
* */
@CrossOrigin
@RequestMapping("/saveTest")
public Object saveData(){
IotHistoricalData data = new IotHistoricalData();
data.setId("2");
data.setTagName("空气温度");
data.setTagData(66.5);
dataDao.saveData(data);
Map<String,String> map = new HashMap<>();
map.put("code","0");
map.put("message","保存数据成功");
return map;
}
}
但是注意的是,ObjectId 是MongoDB的id 的默认类型,ObjectId 采用12字节的存储空间,每个字节两位16进制数字,是一个24位的字符串。
可以使用hutool工具的唯一ID工具-IdUtil来生成,具体看文档,这里也贴出来一下。
em…再提一点吧,很多老哥们保存之后遇到一个问题就是,保存的数据,表里总是多出一个 _class 的字段,感觉很难受,那么你可以去参考这老哥写的https://blog.csdn.net/u011663149/article/details/85299961
溜了溜了,自我学习记录,如有不妥之处,您骂一句就走好吧。