Какие дополнения должны быть сделаны в этот текстовый файл с полезной картинкой Spring Boot Husky PicJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Какие дополнения должны быть сделаны в этот текстовый файл с полезной картинкой Spring Boot Husky Pic

Сообщение Anonymous »

Этот вопрос - открытый вопрос о новых идеях.
Я хочу поделиться своим кодом, и я надеюсь, что некоторые из вас могут дать мне код для моего проекта Spring Boot Husky Pic. < /p>
пейджинг, сортировка и фильтрация
Студенческий сервис:
public Page getStudentsWithFilters(String firstname, String lastname, Integer age, Pageable pageable) {
Specification spec = Specification.where(null);

if (firstname != null && !firstname.isEmpty()) {
spec = spec.and(StudentSpecification.firstnameContains(firstname));
}

if (lastname != null && !lastname.isEmpty()) {
spec = spec.and(StudentSpecification.lastnameContains(lastname));
}

if (age != null) {
spec = spec.and(StudentSpecification.ageEquals(age));
}

return studentRepository.findAll(spec, pageable);
}

StudentController:
// Beispiel-URL:
// http://localhost:8080/students/search?f ... stname,asc
@GetMapping("/search")
public Page getStudents(
@RequestParam(required = false) String firstname,
@RequestParam(required = false) String lastname,
@RequestParam(required = false) Integer age,
@PageableDefault(size = 10) Pageable pageable) {
return studentService.getStudentsWithFilters(firstname, lastname, age, pageable);
}

Студенческая рециментация
public class StudentSpecification {

public static Specification firstnameContains(String firstname) {
return (root, query, criteriaBuilder) ->
criteriaBuilder.like(root.get("firstname"), "%" + firstname + "%");
}

public static Specification lastnameContains(String lastname) {
return (root, query, criteriaBuilder) ->
criteriaBuilder.like(root.get("lastname"), "%" + lastname + "%");
}

public static Specification ageEquals(Integer age) {
return (root, query, criteriaBuilder) ->
criteriaBuilder.equal(root.get("age"), age);
}
}

jpql и собственные запросы
StudentRepository: < /p>
// JPQL: Find students by age greater than a specific value
@Query("SELECT s FROM Student s WHERE s.age > ?1")
List getStudentsByAgeGreaterThan(int age);

// JPQL: Get first names of students by age greater than a specific value
@Query("SELECT s.firstName FROM Student s WHERE s.age > ?1")
List getFirstNameByAgeGreaterThan(int age);

// Native SQL: Find students with age between two values
@Query(value = "SELECT * FROM student s WHERE s.age BETWEEN ?1 AND ?2", nativeQuery = true)
List getStudentsByAgeBetween(int age1, int age2);

// Native SQL: Find students with age between a minimum and maximum value using named parameters
@Query(value = "SELECT * FROM student s WHERE s.age BETWEEN :minAge AND :maxAge", nativeQuery = true)
List getStudentByAgeBetweenMinAndMaxAge(@Param("minAge") int minAge, @Param("maxAge") int maxAge);

h2 datenbank
Application.properties: < /p>
spring.application.name=dbname
spring.datasource.url=jdbc:h2:file:~/dbname;AUTO_SERVER=TRUE;
spring.datasource.driverClassName=org.h2.Driver
spring.h2.console.enabled=true
spring.datasource.username=username
spring.datasource.password=pwd
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.shell.interactive.enabled=true
spring.jpa.hibernate.ddl-auto=update
spring.sql.init.mode=always

pom.xml зависимости


org.springframework.boot
spring-boot-starter-data-jpa



org.springframework.boot
spring-boot-starter-web



com.mysql
mysql-connector-j
runtime


org.projectlombok
lombok


org.springframework.boot
spring-boot-starter-test
test


org.springframework.shell
spring-shell-starter
3.3.3


com.h2database
h2
runtime




Подробнее здесь: https://stackoverflow.com/questions/794 ... ot-husky-p
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»