Spring Data若干组件集成搭建与示例代码

官网 https://spring.io/projects/spring-data

来看一张全景图:


在这里插入图片描述


本文涉及框架版本说明:

组件版本
es服务版本7.10.1
spring-boot 版本2.3.12-RELEASE

文章涉及代码已上传到 GITEE https://gitee.com/aqu415/demo/tree/master/spring-data

spring-data-elasticsearch
  • pom(父pom可以到gitee获取)
<?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-data</artifactId>
        <groupId>com.uu</groupId>
        <version>1.0.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-data-es-demo</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- Spring Boot Elasticsearch 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
    </dependencies>
</project>
  • 配置文件
spring:
  elasticsearch:
    rest:
      uris: http://192.168.126.105:9200
  • 实体类:
package com.xx.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Data
@Document(indexName = "info")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Info implements java.io.Serializable {

    @Id
    @Field(type = FieldType.Long, store = true)
    private long id;

    @Field(type = FieldType.Text, store = true, analyzer = "ik_smart")
    private String title;

    @Field(type = FieldType.Text, store = true, analyzer = "ik_smart")
    private String content;
}

  • 仓库操作类
package com.xx.repository;

import com.xx.entity.Info;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface InfoRepository extends ElasticsearchRepository<Info, Long> {
}

  • 启动类
package com.xx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ESApplication {

    public static void main(String[] args) {
        SpringApplication.run(ESApplication.class, args);
    }
}

  • 测试类
package com.xx;


import com.xx.entity.Info;
import com.xx.repository.InfoRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.UUID;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ESApplication.class)
public class ESTest {

    @Resource
    private InfoRepository infoRepository;


    @Test
    public void batchSaveTest() {
        Info info = Info.builder().id(System.currentTimeMillis()).content(UUID.randomUUID().toString()).title(UUID.randomUUID().toString()).build();
        Iterable<Info> infos = infoRepository.saveAll(Arrays.asList(info));
        System.out.println(infos);
    }

    @Test
    public void indexTest() {
        Iterable<Info> all = this.infoRepository.findAll();
        System.out.println(all);
    }
}

  • 测试结果
    写入
    在这里插入图片描述

查询
在这里插入图片描述

spring-data-mongodb
  • pom(父pom可以到gitee获取)
<?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-data</artifactId>
        <groupId>com.uu</groupId>
        <version>1.0.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

</project>
  • 配置文件
spring:
  application:
    name: mongo
  data:
    mongodb:
      uri: mongodb://127.0.0.1:27017/test
  • 实体类:
package com.xx.entity;

import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "test")
@Data
@ToString
@Builder
public class People implements java.io.Serializable {

    @Indexed
    private String name;

    @Indexed
    private int age;
}

  • 仓库操作类
package com.xx.service;

import com.xx.entity.People;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PeopleServie {

    @Resource
    private MongoTemplate mongoTemplate;

    public List<People> getAll() {
        return mongoTemplate.findAll(People.class);
    }

    public void batchSave(People people){
        mongoTemplate.save(people);
    }
}

  • 启动类
package com.xx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MongoApp {

    public static void main(String[] args) {
        SpringApplication.run(MongoApp.class);
    }
}

  • 测试类
package java.com.xx;//package com.xx.test;
//
import com.xx.MongoApp;
import com.xx.entity.People;
import com.xx.service.PeopleServie;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MongoApp.class)
@Slf4j
public class MongoTest {

    @Resource
    private PeopleServie peopleServie;

    @Test
    public void getAllTest() {
        log.info("" + peopleServie.getAll());
    }

    @Test
    public void batchSaveTest() {
        for (int i = 0; i < 10000; i++) {
            this.peopleServie.batchSave(People.builder().name(String.valueOf(i)).age(i).build());
        }
        log.info("save over");
    }
}

文章涉及代码已上传到 GITEE https://gitee.com/aqu415/demo/tree/master/spring-data

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值