Springboot项目连接MongoDB及使用教程

一、连接MongoDB

1、修改application.properties配置文件,增加连接MongoDB的信息

spring.data.mongodb.host=10.238.3.xx
spring.data.mongodb.port=20000
spring.data.mongodb.database=admin
spring.data.mongodb.username=username
spring.data.mongodb.password=password

二、使用

1、创建 ModuleInfo 类

@Document(collection = "module_info")
@Getter
@Setter
@EntityScan
public class ModuleInfo implements java.io.Serializable {
    @Id
    private Long id;

    private Date gmtCreate;

    private Date gmtModified;

    private String appName;

    private String environment;

    private String ip;

    private String port;

    private String version;

    private String status;
}

2、创建service类,ModuleInfoService

public interface ModuleInfoService {
    ModuleInfo findByAppNameAndEnv(String appName, String environment);
}

3、创建service 实现类,ModuleInfoServiceImpl

@Service
public class ModuleInfoServiceImpl implements ModuleInfoService {
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public ModuleInfo findByAppNameAndEnv(String appName, String environment) {
        Query query = new Query(Criteria.where("appName").is(appName).and("environment").is(environment)).limit(1);
        return mongoTemplate.findOne(query, ModuleInfo.class);
    }
}

4、controller类

@RestController
@RequestMapping(value = "/api/interfacecase")
public class InterfaceCaseController {
    @Autowired
    SingleCaseDebugAPI singleCaseDebugAPI;

    @Autowired
    GatewayCaseService gatewayCaseService;

    @Autowired
    WebAPICaseService webAPICaseService;

    @Autowired
    MicroServiceCaseService microServiceCaseService;

    @Autowired
    RecordService recordService;

    @Autowired
    ModuleInfoService moduleInfoService;

    @Autowired
    BaseRedisDao baseRedisDao;

    @RequestMapping(value = "/debugInterfaceCase", method = RequestMethod.GET)
    public boolean debugInterfaceCase(@RequestParam(value = "id") Integer id, @RequestParam(value = "interfaceType") String interfaceType, @RequestParam(value = "username") String username){
        System.out.println("前端传递过来的用例ID:" + id + "  用例接口类型:" + interfaceType);
        ResMsg resMsg = new ResMsg();
        JSONObject jsonObject = new JSONObject();
        boolean result = true;

        if(interfaceType.equals("Gateway")){
            GatewayCase gatewayCase = gatewayCaseService.findByPrimaryKey(id);
            jsonObject = JSONObject.parseObject(JSONObject.toJSONString(gatewayCase), JSONObject.class);
        }else if(interfaceType.equals("WebAPI")){
           WebAPICase webAPICase = webAPICaseService.findByPrimaryKey(id);
            jsonObject = JSONObject.parseObject(JSONObject.toJSONString(webAPICase), JSONObject.class);
        }else {
            // 微服务
            MicroServiceCase microServiceCase = microServiceCaseService.findByPrimaryKey(id);
            String entranceDesc = "dubbo://" + microServiceCase.getClassName() + "/" + microServiceCase.getMethodName();
            String appName = "umehome-manager";

            //动态获取接口请求参数
            Record record = recordService.findByAppNameAndEntranceDesc(appName, entranceDesc);
//            System.out.println("record: " + JSONObject.toJSONString(record));

            //获取回放的ip
            ModuleInfo moduleInfo = moduleInfoService.findByAppNameAndEnv(appName, "dockerGray");
            System.out.println("moduleInfo: " + JSONObject.toJSONString(moduleInfo));
        }
        return result;
    }
 }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Spring Boot项目整合MongoDB的示例: 1. 首先,需要在 `pom.xml` 文件中添加 MongoDB 的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> ``` 2. 然后,需要在 `application.properties` 文件中配置 MongoDB连接信息,例如: ```properties spring.data.mongodb.uri=mongodb://localhost:27017/mydb ``` 3. 接着,创建一个实体,例如: ```java @Document(collection = "users") public class User { @Id private String id; private String name; private int age; // getter 和 setter 略 } ``` 这里使用了 `@Document` 注解来指定该实体对应的 MongoDB 的集合名称。 4. 创建一个 `UserRepository` 接口,继承自 `MongoRepository` 接口,例如: ```java public interface UserRepository extends MongoRepository<User, String> { } ``` 这里使用了 `MongoRepository` 接口,它提供了一些常用的 CRUD 操作方法。可以根据需要自定义方法。 5. 编写业务逻辑,例如: ```java @Service public class UserService { @Autowired private UserRepository userRepository; public User save(User user) { return userRepository.save(user); } public List<User> findAll() { return userRepository.findAll(); } public Optional<User> findById(String id) { return userRepository.findById(id); } public void deleteById(String id) { userRepository.deleteById(id); } } ``` 这里使用了 `@Service` 注解来声明该为一个服务使用 `@Autowired` 注解来注入 `UserRepository` 实例。 6. 最后,编写一个控制器,例如: ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @PostMapping public User save(@RequestBody User user) { return userService.save(user); } @GetMapping public List<User> findAll() { return userService.findAll(); } @GetMapping("/{id}") public Optional<User> findById(@PathVariable String id) { return userService.findById(id); } @DeleteMapping("/{id}") public void deleteById(@PathVariable String id) { userService.deleteById(id); } } ``` 这里使用了 `@RestController` 注解来声明该为一个 RESTful 风格的控制器使用 `@Autowired` 注解来注入 `UserService` 实例,使用 `@PostMapping`、`@GetMapping`、`@DeleteMapping` 等注解来声明不同的 HTTP 请求方法,分别对应新增、查询、删除操作。 以上就是一个简单的 Spring Boot 项目整合 MongoDB 的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值