Skip to content

devsuperior/aula-jparepository

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JpaRepository: interface boladona do Spring

DevSuperior - Nelio Alves

Image

Siga-nos:

https://instagram.com/devsuperior.ig

https://youtube.com/devsuperior

Trechos de código

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=

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

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
INSERT INTO tb_users(name,email,salary) VALUES ('Maria','[email protected]',1348.74);
INSERT INTO tb_users(name,email,salary) VALUES ('Joao Silva','[email protected]',9276.62);
INSERT INTO tb_users(name,email,salary) VALUES ('Carlos Silva','[email protected]',7318.75);
INSERT INTO tb_users(name,email,salary) VALUES ('Adriana','[email protected]',10688.93);
INSERT INTO tb_users(name,email,salary) VALUES ('Karina','[email protected]',7251.28);
INSERT INTO tb_users(name,email,salary) VALUES ('Carlos Marques','[email protected]',5485.79);
INSERT INTO tb_users(name,email,salary) VALUES ('Carlos Pereira','[email protected]',3519.41);
INSERT INTO tb_users(name,email,salary) VALUES ('Ana Maria','[email protected]',2569.20);
INSERT INTO tb_users(name,email,salary) VALUES ('Beatriz','[email protected]',3853.33);
INSERT INTO tb_users(name,email,salary) VALUES ('Joana','[email protected]',4222.91);
INSERT INTO tb_users(name,email,salary) VALUES ('Tulio Augusto','[email protected]',9453.69);
INSERT INTO tb_users(name,email,salary) VALUES ('Augusto','[email protected]',4119.91);
INSERT INTO tb_users(name,email,salary) VALUES ('Marta','[email protected]',4519.15);
INSERT INTO tb_users(name,email,salary) VALUES ('Silvio','[email protected]',5285.68);
INSERT INTO tb_users(name,email,salary) VALUES ('Washington','[email protected]',6439.99);
INSERT INTO tb_users(name,email,salary) VALUES ('Teresa','[email protected]',8929.78);
INSERT INTO tb_users(name,email,salary) VALUES ('Luciano','[email protected]',3360.72);
INSERT INTO tb_users(name,email,salary) VALUES ('Fabiana','[email protected]',2532.83);
INSERT INTO tb_users(name,email,salary) VALUES ('Fabio','[email protected]',5912.88);
INSERT INTO tb_users(name,email,salary) VALUES ('Gisele','[email protected]',7558.26);
INSERT INTO tb_users(name,email,salary) VALUES ('Larissa','[email protected]',2362.04);
INSERT INTO tb_users(name,email,salary) VALUES ('Natanael','[email protected]',6860.63);
INSERT INTO tb_users(name,email,salary) VALUES ('Meire','[email protected]',3553.40);
INSERT INTO tb_users(name,email,salary) VALUES ('Ana Carolina','[email protected]',1404.28);
INSERT INTO tb_users(name,email,salary) VALUES ('Filipe','[email protected]',3388.73);
@Autowired
private UserRepository repository;

@GetMapping
public ResponseEntity<List<User>> findAll() {
    List<User> result = repository.findAll();
    return ResponseEntity.ok(result);
}

@GetMapping(value = "/page")
public ResponseEntity<Page<User>> findAll(Pageable pageable) {
    Page<User> result = repository.findAll(pageable);
    return ResponseEntity.ok(result);
}

@GetMapping(value = "/search-salary")
public ResponseEntity<Page<User>> searchBySalary(@RequestParam(defaultValue = "0") Double minSalary, @RequestParam(defaultValue = "1000000000000") Double maxSalary, Pageable pageable) {
    Page<User> result = repository.findBySalaryBetween(minSalary, maxSalary, pageable);
    return ResponseEntity.ok(result);
}

@GetMapping(value = "/search-name")
public ResponseEntity<Page<User>> searchByName(@RequestParam(defaultValue = "") String name, Pageable pageable) {
    Page<User> result = repository.findByNameContainingIgnoreCase(name, pageable);
    return ResponseEntity.ok(result);
}
@Query("SELECT obj FROM User obj WHERE obj.salary >= :minSalary AND obj.salary <= :maxSalary")
Page<User> searchBySalary(Double minSalary, Double maxSalary, Pageable pageable);

@Query("SELECT obj FROM User obj WHERE LOWER(obj.name) LIKE LOWER(CONCAT('%',:name,'%'))")
Page<User> searchByName(String name, Pageable pageable);

Page<User> findBySalaryBetween(Double minSalary, Double maxSalary, Pageable pageable);

Page<User> findByNameContainingIgnoreCase(String name, Pageable pageable);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages