Mongodb MongoRepository操作mongo

1、实体类

public classRuleModel implements Serializable{

       @Id

       Stringid;//序号

      StringruleName;//规则名称

      StringuserGroupName;//用户群组name

      StringuserGroupCode;//用户群组code

     get,set方法略

}


2、继承MongoRepository

@Repository

public interface RuleModelRepository extends MongoRepository{

}


3、实现服务接口

@Service

public class RuleManagerServiceImpl implements RuleManagerService {

private staticLoggerlogger= LoggerFactory.getLogger(RuleManagerServiceImpl.class);

@Autowired

private RuleModelRepository ruleModelRepository;

@Override

publicOpResult add(AddRulesVO addRulesVO) {

      RuleModel ruleModel = ObjectConvertor.toAnotherObj(addRulesVO,RuleModel.class);

      try{

           ruleModelRepository.insert(ruleModel);

           logger.info("插入规则成功"+ruleModel);

           returnOpResult.createSucResult("success");

       }catch(Exception e) {

          logger.error("[EXCEPTION]"+e.getMessage());

     }

    return OpResult.createFailResult(ErrorCodeEnum.ADD_RULE_EXCEPTION);

}

}

你可能感兴趣的:(Mongodb MongoRepository操作mongo)