admin 管理员组

文章数量: 1086019

This is the infra module from a hexagonal architecture that contains all from database related code.

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("be.inbo.watervogels.repository")
@EntityScan("be.inbo.watervogels.repository.entity")
@EnableJpaAuditing
public class PersistenceConfig {
}

Repository implementation, interface imported from other module

package be.inbo.watervogels.repository.impl;

import be.inbo.watervogels.domain.SurveySummary;
import be.inbo.watervogels.repository.SurveyRepository;
import be.inbo.watervogels.repository.entity.SurveySummaryViewEntity;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import .springframework.beans.factory.annotation.Autowired;
import .springframework.data.domain.PageImpl;
import .springframework.data.domain.PageRequest;
import .springframework.data.domain.Pageable;
import .springframework.data.domain.Sort;
import .springframework.stereotype.Repository;

import java.time.Clock;
import java.time.LocalDate;
import java.util.List;

@Repository
public class SurveyRepositoryImpl implements SurveyRepository {

    @Autowired
    private Clock clock;

    @PersistenceContext
    private EntityManager entityManager;

    private final String SELECT_CURRENT_SEASON_QUERY = """
            SELECT s.id, s.surveyname, ss.name, es.label, se.startDate, se.endDate
            FROM SurveyEntity s 
            JOIN s.surveySeasons ss  
            JOIN ss.surveyEvents se 
            JOIN se.eventSequence es
            WHERE se.endDate > ?1 and s.code not like 'ZSCH' 
            ORDER BY s.sequence, s.surveyname, es.sortOrder
""";

    private final String COUNT_CURRENT_SEASON_QUERY = """
            SELECT count(*)
            FROM SurveyEntity s 
            JOIN s.surveySeasons ss  
            JOIN ss.surveyEvents se 
            JOIN se.eventSequence es
            WHERE se.endDate > ?1 and s.code not like 'ZSCH' 
            ORDER BY s.sequence, s.surveyname, es.sortOrder
""";

    public PageImpl<SurveySummary> getCurrentSeason(int page, int size, String sortBy, boolean ascending) {
        Pageable pageable = PageRequest.of(page, size, ascending ? Sort.by(sortBy).ascending() : Sort.by(sortBy).descending());

        List<SurveySummaryViewEntity> entities = this.entityManager.createQuery(SELECT_CURRENT_SEASON_QUERY, SurveySummaryViewEntity.class)
                .setParameter(1, LocalDate.now(clock))
                .setFirstResult(page * size)
                .setMaxResults(size)
                .getResultList();

        long total = (long) this.entityManager.createQuery(COUNT_CURRENT_SEASON_QUERY)
                .setParameter(1, LocalDate.now(clock))
                .getSingleResult();

        List<SurveySummary> surveySummaries = entities.stream()
                .map(entity -> new SurveySummary(entity.getSurveyName(), entity.getSeasonName(), entity.getSeasonStartDate(), entity.getSeasonEndDate(), entity.getEventSequenceLabel()))
                .toList();

        return new PageImpl<>(surveySummaries, pageable, total);
    }
}

build.gradle

plugins {
    `java-library`
}


dependencies {
    implementation(".springframework.boot:spring-boot-starter-data-jpa")
    compileOnly(".projectlombok:lombok")
    implementation("com.h2database:h2")
    testImplementation("com.h2database:h2")
    runtimeOnly("com.microsoft.sqlserver:mssql-jdbc")
    annotationProcessor(".projectlombok:lombok")
    testImplementation(".springframework.boot:spring-boot-starter-test")
    testRuntimeOnly(".junit.platform:junit-platform-launcher")


    testImplementation(".projectlombok:lombok")

    implementation(project(":watervogels-backend-domain"))
    testImplementation(project(":watervogels-backend-domain"))
}

tasks.withType<Test> {
    useJUnitPlatform()
}

For simplicity I use h2 for now. My assumption was that as long as you import spring-boot-data-jpa, you set your properties correclty and import some kind of jdbc driver (h2 in my case), spring can figure out what needs to be set up.

spring.application.name=watervogels-backend-persistence

spring.datasource.driver-class-name=.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

本文标签:

Error[2]: Invalid argument supplied for foreach(), File: /www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm, Line: 58
File: /www/wwwroot/roclinux.cn/tmp/route_read.php, Line: 205, include(/www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm)
File: /www/wwwroot/roclinux.cn/tmp/index.inc.php, Line: 129, include(/www/wwwroot/roclinux.cn/tmp/route_read.php)
File: /www/wwwroot/roclinux.cn/index.php, Line: 29, include(/www/wwwroot/roclinux.cn/tmp/index.inc.php)