直接上代码:
application配置:
#数据库主机地址,默认localhost
spring.data.mongodb.host=localhost
#数据库连接端口
spring.data.mongodb.port=27017
#connection URL
spring.data.mongodb.uri=mongodb://localhost/newdata
spring.data.mongodb.database=newdata
spring.data.mongodb.username=admin
spring.data.mongodb.password=123456
domain层代码,表的映射:
package com.example.demo.domain;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@Document(collection = "data")
public class Mongodata {
private String id;
private String sbj;
private String prd;
private String obj;
private String create_time;
private String update_time;
private Integer is_delete;
private String file_id;
private String pos;
public Mongodata() {
super();
}
public Mongodata(String sbj, String prd, String obj) {
super();
this.sbj = sbj;
this.prd = prd;
this.obj = obj;
}
}
Dao层接口和实现:
package com.example.demo.dao;
public interface DataDAO {
void add(String sbj, String prd, String obj);
void remove(String sbj, String prd, String obj);
void delete(String sbj, String prd, String obj);
void updateIs_deleteByKey(String uri);
void update_relation(String sbj, String prd, String obj, String Sbj, String Prd, String Obj);
}
package com.example.demo.dao;
import com.example.demo.domain.Mongodata;
import org.springframework.beans.factory.annotation.Autowired;
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 java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Component
public class MongoDataDAO implements DataDAO {
@Autowired
private MongoTemplate mongoTemplate;
public static String TABLENAME = "data";
@Override
public void delete(String sbj, String prd, String obj) {
Query query = Query.query(Criteria.where("sbj").is(sbj).and("prd").is(prd).and("obj").is(obj));
Update update = new Update();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //设置日期格式
update.set("is_delete", 1).set("update_time", formatter.format(LocalDateTime.now()));
mongoTemplate.updateFirst(query, update, Mongodata.class);
}
@Override
public void add(String sbj, String prd, String obj) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //设置日期格式
Mongodata mongodata = new Mongodata(sbj, prd, obj);
mongodata.setCreate_time(formatter.format(LocalDateTime.now())); // new Date()为获取当前系统时间
mongodata.setUpdate_time(formatter.format(LocalDateTime.now()));
mongoTemplate.insert(mongodata, TABLENAME);
}
@Override
public void remove(String sbj, String prd, String obj) {
Query query = Query.query(Criteria.where("sbj").is(sbj).and("prd").is(prd).and("obj").is(obj));
mongoTemplate.remove(query, TABLENAME);
}
@Override
public void updateIs_deleteByKey(String uri) {
Query query = Query.query(Criteria.where("sbj").is(uri).and("obj").is(uri));
Update update = new Update();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //设置日期格式
update.set("is_delete", 1).set("update_time", formatter.format(LocalDateTime.now()));
mongoTemplate.updateMulti(query, update, Mongodata.class);
}
@Override
public void update_relation(String sbj, String prd, String obj, String Sbj, String Prd, String Obj) {
Query query = Query.query(Criteria.where("sbj").is(sbj).and("prd").is(prd).and("obj").is(obj));
Update update = new Update();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //设置日期格式
update.set("is_delete", 1).set("update_time", formatter.format(LocalDateTime.now()));
mongoTemplate.updateFirst(query, update, Mongodata.class);
Mongodata mongodata = new Mongodata(sbj, prd, obj);
mongodata.setCreate_time(formatter.format(LocalDateTime.now())); // new Date()为获取当前系统时间
mongodata.setUpdate_time(formatter.format(LocalDateTime.now()));
mongoTemplate.insert(mongodata, TABLENAME);
}
}
service层接口和实现:
package com.example.demo.service;
import com.example.demo.POJO.Jsondata;
import com.example.demo.POJO.UriJson;
public interface DataService {
//增加一条数据
void add(Jsondata jsondata);
//删除一条数据,is_delete设置为1
void delete(Jsondata jsondata);
//修改关系,删除old,增加novel
void update_relation(Jsondata oldData, Jsondata newData);
//删除节点,将sbj和obj等于uri的删除
void remove_node(UriJson uriJson);
//删除一条数据,从库里删除
void remove(Jsondata jsondata);
}
package com.example.demo.service.impl;
import com.example.demo.POJO.UriJson;
import com.example.demo.dao.DataDAO;
import com.example.demo.service.DataService;
import com.example.demo.POJO.Jsondata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DataServiceImpl implements DataService {
@Autowired
private DataDAO dataDAO;
@Override
public void update_relation(Jsondata oldData, Jsondata newData) {
dataDAO.update_relation(oldData.getSbj(), oldData.getPrd(), oldData.getObj(), newData.getSbj(), newData.getPrd(), newData.getObj());
}
@Override
public void remove_node(UriJson uriJson) {
dataDAO.updateIs_deleteByKey(uriJson.getUri());
}
@Override
public void add(Jsondata jsondata) {
dataDAO.add(jsondata.getSbj(), jsondata.getPrd(), jsondata.getObj());
}
@Override
public void delete(Jsondata jsondata) {
dataDAO.delete(jsondata.getSbj(), jsondata.getPrd(), jsondata.getObj());
}
@Override
public void remove(Jsondata jsondata) {
dataDAO.remove(jsondata.getSbj(), jsondata.getPrd(), jsondata.getObj());
}
}