spring data mongodb 使用 之 spring boot 2.x

spring data 是一个家族在使用中大同小异
本人写的spring data是通过maven子父工程管理
parent项目的 : pom.xml

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-boot-data</artifactId>
        <groupId>com.ronnie</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-data-mongodb</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

propertise

spring:
  data:
    mongodb:
      uri: mongodb://root:password@iip:27017/yourdb

document对象

package com.ronnie.data.document;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * @Description:
 * @Author: rongyu
 * @CreateDate: 2018/9/1$ 14:25$
 * @Remark:
 */
@Data
@Document
public class Member {
    private Long memberId;

    private String username;

    private String password;
}

关于@Id 注解在没有时会自动生成objectId 为_id 如果@Id 有则使用注解字段为_id,再调用保存时没有@id 数据库自动维护,@id存在会直接报保存或者替换

dao

package com.ronnie.data.repository;

import com.ronnie.data.document.Member;
import org.springframework.data.mongodb.repository.MongoRepository;

/**
 * @Description:
 * @Author: rongyu
 * @CreateDate: 2018/9/1$ 14:28$
 * @Remark:
 */
public interface MemberRepository extends MongoRepository<Member,Long> {
}

继承后直接注入使用

package com.ronnie.data;

import com.ronnie.data.document.Member;
import com.ronnie.data.repository.MemberRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.LinkedList;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
    @Autowired
    MemberRepository memberRepository;
    @Autowired
    MongoTemplate mongoTemplate;

    @Test
    public void queryDemo() {
        // add Sort
        Sort.Order order1 = new Sort.Order(Sort.Direction.DESC,"id");
        Sort.Order order2 = new Sort.Order(Sort.Direction.ASC,"username");
        Sort sort = Sort.by(order1,order2);
        // add condition
        Member member = new Member();
        member.setUsername("admin");
        Example<Member> memberExample = Example.of(member);
        // add pageRequest
        PageRequest pageRequest = PageRequest.of(0, 10, sort);
        // query result
        Page<Member> page = memberRepository.findAll(memberExample,pageRequest);
        log.info("page={}", page);
    }
}

MongoRepository接口之中所有方法,接受的都是分页查询过程中的对象,直接调用即可
也可以直接注入mongoTemplate 进行操作

github https://github.com/ronnierry/spring-boot-data/tree/master/spring-data-jpa
QQ群 : 869658872

上一篇:SQL Server 性能调优(方法论)


下一篇:一键修改配置文件