moved OSIV examples from spring-data-jpa-3 to spring-data-jpa-4
This commit is contained in:
@@ -2,8 +2,10 @@ package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableJpaRepositories
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.boot.daos;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.boot.domain.Customer;
|
||||
|
||||
/**
|
||||
* JPA CrudRepository interface
|
||||
*
|
||||
* @author ysharma2512
|
||||
*
|
||||
*/
|
||||
@Repository
|
||||
@Transactional
|
||||
public interface CustomerRepository extends CrudRepository<Customer, Long>{
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.boot.web.controllers;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.boot.daos.CustomerRepository;
|
||||
import com.baeldung.boot.domain.Customer;
|
||||
|
||||
/**
|
||||
* A simple controller to test the JPA CrudRepository operations
|
||||
* controllers
|
||||
*
|
||||
* @author ysharma2512
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
public class CustomerController {
|
||||
|
||||
CustomerRepository customerRepository;
|
||||
|
||||
public CustomerController(CustomerRepository customerRepository2) {
|
||||
this.customerRepository = customerRepository2;
|
||||
}
|
||||
|
||||
@PostMapping("/customers")
|
||||
public ResponseEntity<List<Customer>> insertCustomers() throws URISyntaxException {
|
||||
Customer c1 = new Customer("James", "Gosling");
|
||||
Customer c2 = new Customer("Doug", "Lea");
|
||||
Customer c3 = new Customer("Martin", "Fowler");
|
||||
Customer c4 = new Customer("Brian", "Goetz");
|
||||
List<Customer> customers = Arrays.asList(c1, c2, c3, c4);
|
||||
customerRepository.saveAll(customers);
|
||||
return ResponseEntity.ok(customers);
|
||||
}
|
||||
|
||||
}
|
||||
+4
-7
@@ -1,15 +1,12 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.model.BasicUser;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepository extends JpaRepository<BasicUser, Long> {
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
@EntityGraph(attributePaths = "permissions")
|
||||
Optional<BasicUser> findDetailedByUsername(String username);
|
||||
import com.baeldung.model.BasicUser;
|
||||
|
||||
public interface UserRepository extends JpaRepository<BasicUser, Long> {
|
||||
|
||||
Optional<BasicUser> findSummaryByUsername(String username);
|
||||
|
||||
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.service;
|
||||
|
||||
import com.baeldung.model.BasicUser;
|
||||
import com.baeldung.repository.UserRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class SimpleUserService implements UserService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public SimpleUserService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<BasicUser> findOne(String username) {
|
||||
return userRepository.findDetailedByUsername(username);
|
||||
}
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.service;
|
||||
|
||||
import com.baeldung.model.BasicUser;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserService {
|
||||
Optional<BasicUser> findOne(String username);
|
||||
}
|
||||
-45
@@ -1,45 +0,0 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.model.BasicUser;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class DetailedUserDto {
|
||||
|
||||
private Long id;
|
||||
private String username;
|
||||
private Set<String> permissions;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public Set<String> getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public void setPermissions(Set<String> permissions) {
|
||||
this.permissions = permissions;
|
||||
}
|
||||
|
||||
public static DetailedUserDto fromEntity(BasicUser user) {
|
||||
DetailedUserDto detailed = new DetailedUserDto();
|
||||
detailed.setId(user.getId());
|
||||
detailed.setUsername(user.getUsername());
|
||||
detailed.setPermissions(user.getPermissions());
|
||||
|
||||
return detailed;
|
||||
}
|
||||
}
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import com.baeldung.service.UserService;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public UserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping("/{username}")
|
||||
public ResponseEntity<?> findOne(@PathVariable String username) {
|
||||
return userService.findOne(username)
|
||||
.map(DetailedUserDto::fromEntity)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user