MongoTemplate 操作 mongodb

1. 增加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

2.增加配置

spring.data.mongodb.database=test
spring.data.mongodb.uri=mongodb://user::27017/admin

6.测试

@Document(collection = "login_log")
@Data
public class LoginLog {
    @Id
    private String id;
    private String customerId;
    private String deviceId;
    private String deviceName;
    private long lastLoginTime;
}
@Api(value = "MongodbController", description = "Test接口")
@RestController
@RequestMapping("/loginLog")
@Slf4j
public class MongodbController {
    @Autowired
    private MongoTemplate template;


    @ApiOperation(value = "用户登录日志", notes = "用户登录日志")
    @RequestMapping(value = "/list",
            method = RequestMethod.GET)
    public String list(String key) {
        Query query = new Query();
        query.addCriteria(Criteria.where("customerId").is(Integer.parseInt(key)));
        List<ClientDevice> clientDevices = template.find(query, LoginLog.class);

        return clientDevices.toString();
    }
}

相关推荐