国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Springboot --- 整合spring-data-jpa和spring-data-elasticsearch

這篇具有很好參考價值的文章主要介紹了Springboot --- 整合spring-data-jpa和spring-data-elasticsearch。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Springboot --- 整合spring-data-jpa和spring-data-elasticsearch

Springboot --- 整合spring-data-jpa和spring-data-elasticsearch
Springboot --- 整合spring-data-jpa和spring-data-elasticsearch

Springboot --- 整合spring-data-jpa和spring-data-elasticsearch

Springboot --- 整合spring-data-jpa和spring-data-elasticsearch

SpringBoot: 整合Ldap.
SpringBoot: 整合Spring Data JPA.
SpringBoot: 整合Elasticsearch.
SpringBoot: 整合spring-data-jpa和spring-data-elasticsearch.
SpringBoot: 整合thymeleaf.
SpringBoot: 注入第三方j(luò)ar包.
SpringBoot: 整合Redis.
SpringBoot: 整合slf4j打印日志.
SpringBoot: 整合定時任務(wù),自動執(zhí)行方法.
SpringBoot: 配置多數(shù)據(jù)源,使用JdbcTemplate以及NamedParameterJdbcTemplate.
SpringBoot: 詳解pom.xml中build和profile.
SpringBoot: 監(jiān)控.
SpringBoot: 緩存Cache/Redis.
SpringBoot: 整合Zookeeper.
Git: 使用詳解.


???????本節(jié)重點就是jpa和es共用一個實體類

1. 依賴

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.nolan</groupId>
    <artifactId>spring-data-es</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-data-es</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. 配置文件

server.port=8085

spring.data.elasticsearch.client.reactive.endpoints=http://localhost:9200
spring.data.elasticsearch.repositories.enabled=true


spring.datasource.one.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.one.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.one.jdbc-url=jdbc:oracle:thin:@//xxxxxx:1521/xxxx
spring.datasource.one.username=xxxx
spring.datasource.one.password=xxxx

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

3. 代碼部分

3.1 Entity

各注解詳細介紹:

  • @Data 就是set/get方法
  • @Entity jpa封裝的實體類
  • @Table jpa對應(yīng)數(shù)據(jù)庫的表
  • @Document es封裝的實體類
  • @Id 主鍵
  • @GeneratedValue 主鍵的類型
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.*;

@Data
@Entity
@Table(name = "USERS")
@Document(indexName = "spring.test",indexStoreType = "user")
public class User {

    @javax.persistence.Id
    @Id
    @GeneratedValue(xxxxx)
    @Column(name = "id")
    private int id;
    private String username;
    private String password;
    private int age;
}

3.2 Repository

  • 像下面這樣寫,肯定是不行的

Springboot --- 整合spring-data-jpa和spring-data-elasticsearch文章來源地址http://www.zghlxwxcb.cn/news/detail-424522.html

  • 正確寫法如下
    注意:啟動類上面還有兩個注解
import com.nolan.es.entity.User;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.Optional;

public interface UserEsRepository extends ElasticsearchRepository<User, Integer> {


    @Override
    @Highlight(fields = {
            @HighlightField(name = "username"),
            @HighlightField(name = "age")
    })
    Optional<User> findById(Integer integer);
}
import com.nolan.es.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserJpaRepository extends JpaRepository<User,Integer> {
    
}

3.3 Config

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DBConfig {
    @Bean(name = "dsOne1")
    @Qualifier("dsOne1")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.one")
    public DataSource dsOne1(){
        return DataSourceBuilder.create().build();
    }
}

3.4 Service

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    public boolean checkIndexExists(Class<?> cls ){
        boolean isExist = elasticsearchRestTemplate.indexOps(cls).exists();
        //獲取索引名
        String indexName = cls.getAnnotation(Document.class).indexName();
        System.out.printf("index %s is %s\n", indexName, isExist ? "exist" : "not exist");
        return isExist;
    }

}

3.5 啟動類

  • 這兩個注解非常重要
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories("com.nolan.es.dao.jpa")
@EnableElasticsearchRepositories("com.nolan.es.dao.es")
public class SpringDataEsApplication {

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

}

3.6 Test

import com.nolan.es.dao.es.UserEsRepository;
import com.nolan.es.dao.jpa.UserJpaRepository;
import com.nolan.es.entity.User;
import com.nolan.es.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import java.util.Iterator;
import java.util.Optional;

@SpringBootTest
class SpringDataEsApplicationTests {

    @Autowired
    private UserEsRepository esRepository;
    @Autowired
    private UserJpaRepository jpaRepository;
    @Autowired
    private UserService userService;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Test
    void contextLoads() {

        boolean result = userService.checkIndexExists(User.class);

        Iterable<User> all = jpaRepository.findAll();
        Iterator<User> userIterators = all.iterator();

        while (userIterators.hasNext()){
            User next = userIterators.next();
            esRepository.save(next);
            System.out.println(1);
        }
    }
}

3.7 項目結(jié)構(gòu)

  • 項目結(jié)構(gòu)
    Springboot --- 整合spring-data-jpa和spring-data-elasticsearch

到了這里,關(guān)于Springboot --- 整合spring-data-jpa和spring-data-elasticsearch的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • SpringBoot原理分析 | Spring Data整合:JDBC、Druid、Mybatis

    SpringBoot原理分析 | Spring Data整合:JDBC、Druid、Mybatis

    ??wei_shuo的個人主頁 ??wei_shuo的學(xué)習(xí)社區(qū) ??Hello World ! Spring Data是一個用于簡化數(shù)據(jù)庫訪問和操作的開源框架,為開發(fā)人員提供了一種通用的方式來處理不同類型的數(shù)據(jù)存儲,例如關(guān)系型數(shù)據(jù)庫(如MySQL、PostgreSQL、Oracle)和非關(guān)系型數(shù)據(jù)庫(如MongoDB、Cassandra、Redis)等。

    2024年02月12日
    瀏覽(47)
  • Spring Data JPA 快速上手

    Spring Data JPA 快速上手

    JPA的全稱是Java Persisitence API,即JAVA持久化API,是sum公司退出的一套基于ORM的規(guī)范,內(nèi)部是由一些列的接口和抽象類構(gòu)成。JPA通過JDK5.0注解描述對象-關(guān)系表的映射關(guān)系,并將運行期的實體對象持久化到數(shù)據(jù)庫中。 Spring Data的優(yōu)勢:可以操作多種數(shù)據(jù)庫,關(guān)系型數(shù)據(jù)庫,非關(guān)系

    2024年04月23日
    瀏覽(26)
  • Spring Data JPA 學(xué)習(xí)筆記

    Spring Data JPA: Spring Data JPA 的技術(shù)特點: @Entity 標(biāo)注是一個實體類,實體類中的每一個屬性都對應(yīng)表中的一列。 @Table(name = “User”) 這個注解用于指定實體類對應(yīng)的數(shù)據(jù)庫表名。(但首字母會小寫) @Data:這個注解是Lombok庫提供的,用于自動生成實體類的getter和setter方法、構(gòu)造函

    2024年04月09日
    瀏覽(27)
  • Spring data JPA常用命令

    Spring Data JPA是Spring框架的一部分,它提供了一個簡化的方式來與關(guān)系型數(shù)據(jù)庫進行交互。JPA代表Java持久化API,它是Java EE規(guī)范中定義的一種對象關(guān)系映射(ORM)標(biāo)準(zhǔn)。Spring Data JPA在JPA的基礎(chǔ)上提供了更高級的抽象,使得開發(fā)人員能夠更輕松地進行數(shù)據(jù)庫操作。 使用Spring Data

    2024年02月15日
    瀏覽(31)
  • SpringBoot整合Spring Data Elasticsearch,寫給互聯(lián)網(wǎng)大廠員工的真心話

    SpringBoot整合Spring Data Elasticsearch,寫給互聯(lián)網(wǎng)大廠員工的真心話

    @RunWith(SpringRunner.class) @SpringBootTest(classes = ItcastElasticsearchApplication.class) public class IndexTest { @Autowired private ElasticsearchTemplate elasticsearchTemplate; //注入ElasticsearchTemplate類 @Test public void testCreate(){ // 創(chuàng)建索引,會根據(jù)Item類的@Document注解信息來創(chuàng)建 elasticsearchTemplate.createIndex(Item.class)

    2024年04月14日
    瀏覽(23)
  • Spring Data JPA的@Entity注解

    ?rulesCouponTypeConverter.java ?entity/CouponTemplate.java Spring JPA 包的標(biāo)準(zhǔn)注解,對數(shù)據(jù)庫字段進行了映射,我挑幾個關(guān)鍵注解說道一下。 1、Entity:聲明了“數(shù)據(jù)庫實體”對象,它是數(shù)據(jù)庫 Table 在程序中的映射對象; 2、Table:指定了 CouponTemplate 對應(yīng)的數(shù)據(jù)庫表的名稱; 3、ID/Generat

    2024年02月11日
    瀏覽(26)
  • Spring Boot 篇四: Spring Data JPA使用SQL Server

    Spring Boot 篇四: Spring Data JPA使用SQL Server

    本篇介紹篇一至篇三中用到的JPA鏈接SQL Server的具體情況以及實戰(zhàn)過程中可能遇到的問題。 具體的下載和安裝教程,請參閱微軟SQL Server官網(wǎng); SQL Server Express 是免費的,并且配套的SQL Server Management Studio也是可以用的。 呃,當(dāng)然,使用Docker來運行SQL Server是另外一條路徑。具體

    2024年02月05日
    瀏覽(17)
  • 如何使用Spring Data JPA簡化MySQL數(shù)據(jù)訪問

    本篇文章是 “一起學(xué)習(xí)mysql” 系列的第五篇文章,本篇文章我們學(xué)習(xí)一下Spring Data JPA的使用,在開始學(xué)習(xí)器,我們先來了解一下什么是JPA。 JPA的全稱是Java Persistence API,是J2EE中的一條規(guī)范,它標(biāo)準(zhǔn)化了數(shù)據(jù)持久化API。在上一篇文章中,我們了解了如何使用MyBatis進行MySQL數(shù)據(jù)

    2024年02月15日
    瀏覽(27)
  • Spring Data JPA之自動創(chuàng)建數(shù)據(jù)庫表

    Spring Data JPA之自動創(chuàng)建數(shù)據(jù)庫表

    由于在項目中使用到了Spring Data JPA(Java Persistent API)進行項目開發(fā),并且自己對JPA比較感興趣想進行學(xué)習(xí)和了解。首先學(xué)習(xí)和了解的是JPA自動創(chuàng)建數(shù)據(jù)庫表,通過JPA能夠讓軟件工程師們不用再去手動創(chuàng)建數(shù)據(jù)表,能夠減輕軟件工程師們的工作量。 通過本篇博客可以實現(xiàn)使用

    2024年02月05日
    瀏覽(27)
  • 【Springboot系列】SpringBoot整合Jpa

    【Springboot系列】SpringBoot整合Jpa

    前言: Spring Boot是一種快速開發(fā)框架,它簡化了Java應(yīng)用程序的開發(fā)過程。而Jpa(Java Persistence API)是Java持久化規(guī)范的一種實現(xiàn),它提供了一種方便的方式來訪問和操作數(shù)據(jù)庫。將Spring Boot與Jpa整合可以更加方便地進行數(shù)據(jù)庫操作,提高開發(fā)效率。本文將介紹如何使用Spring Bo

    2024年02月05日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包