Этот вопрос - открытый вопрос о новых идеях.
Я хочу поделиться своим кодом, и я надеюсь, что некоторые из вас могут дать мне код для моего проекта 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
Какие дополнения должны быть сделаны в этот текстовый файл с полезной картинкой Spring Boot Husky Pic ⇐ JAVA
Программисты JAVA общаются здесь
1738713305
Anonymous
Этот вопрос - открытый вопрос о новых идеях.
Я хочу поделиться своим кодом, и я надеюсь, что некоторые из вас могут дать мне код для моего проекта Spring Boot Husky Pic. < /p>
[b] пейджинг, сортировка и фильтрация [/b]
[b] Студенческий сервис: [/b]
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);
}
[b] StudentController: [/b]
// Beispiel-URL:
// http://localhost:8080/students/search?firstname=Armin&lastname=Dilic&age=18&page=0&size=10&sort=lastname,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);
}
[b] Студенческая рециментация [/b]
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);
}
}
[b] jpql и собственные запросы [/b]
[b] StudentRepository: [/b] < /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);
[b] h2 datenbank [/b]
[b] Application.properties: [/b] < /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
[b] pom.xml зависимости [/b]
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
Подробнее здесь: [url]https://stackoverflow.com/questions/79412863/what-additions-should-be-made-to-this-text-file-with-helpful-spring-boot-husky-p[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия