1. dependancy
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
2. config
# mongodbspring.data.mongodb.host=***.mongodb.rds.aliyuncs.comspring.data.mongodb.port=***17spring.data.mongodb.authentication-database=adminspring.data.mongodb.username=usernamespring.data.mongodb.password=pwdspring.data.mongodb.database=yourappname
3. code:
@Slf4j@Servicepublic class LogQueryService { @Autowired private MongoTemplate mongoTemplate; public ListqueryBy(String bizName, String bizKey) { if (StringUtils.isEmpty(bizName) && StringUtils.isEmpty(bizKey)) { return Lists.newArrayList(); } Query query = null; if (StringUtils.isNotEmpty(bizKey)) { query = Query.query(Criteria.where("bizKey").is(bizKey)); } else if (StringUtils.isNotEmpty(bizName)) { query = Query.query(Criteria.where("bizName").is(bizName)); } List list = mongoTemplate.find(query, LogDoc.class); return list; }}
public void logIt(InputDto inputDto, ResponseDto response) { try { LogDoc logDoc = new LogDoc(); logDoc.setHostService("xxxxx"); logDoc.setBizKey(inputDto.getPrimaryKey()); logDoc.setBizName("BizTypeDesc"); logDoc.setCreateDate(new Date()); logDoc.setReceiveObj(inputDto); logDoc.setSendObj(response); mongoTemplate.save(logDoc); } catch (Exception e) { log.error("Log error...continue biz...", e); } }
@NoArgsConstructor@AllArgsConstructor@Getter@Setter@Document(collection="LogDoc")public class LogDoc { private String hostService; private String bizName; // 业务名称 private String bizKey; //业务主键 private Date createDate; /** * 系统发送对象 */ private Object sendObj; /** * 系统接收对象 */ private Object receiveObj;}