Skip to main content

Posts

Showing posts from January, 2024

Service

  @Service public class CustomerService { @Autowired private CustomerDao customerDao ; public ResponseEntity<ResponseStructure<Customer>> saveCustomer(Customer customer ) { ResponseStructure<Customer> structure = new ResponseStructure<Customer>(); structure .setBody( customerDao .saveCustomer( customer )); structure .setMessage( "Saved Successfully" ); structure .setCode(HttpStatus. ACCEPTED .value()); return new ResponseEntity<ResponseStructure<Customer>> ( structure ,HttpStatus. ACCEPTED ); } public ResponseEntity<ResponseStructure<Customer>> updateCustomer(Customer customer ) { ResponseStructure<Customer> structure = new ResponseStructure<Customer>(); structure .setBody( customerDao .updateCustomer( customer )); structure .setMessage( "Saved Successfully" ); structure .setCode(HttpStatus. ACCEPTED .value()); return new ResponseEntity<Respo...

Repository

  public interface CustomerRepository extends JpaRepository<Customer, Integer> { @Query ( "select c from Customer c where first_name=?1 and phone=?2" ) Option verifyCustomer(String name , long phone ); }

Dao

  @Repository public class CustomerDao { @Autowired private CustomerRepository repository ; public Customer saveCustomer(Customer customer ) { return repository .save( customer ); } public Customer updateCustomer(Customer customer ) { return repository .save( customer ); } public void deleteCustomer( int id ) { repository .deleteById( id ); } public Optional<Customer> getCustomer( int id ) { return repository .findById( id ); } public List<Customer> getAll() { return repository .findAll(); } }

controller

  @RestController @CrossOrigin public class CustomerController { @Autowired private CustomerService customerService ; @PostMapping ( "/customer" ) public ResponseEntity<ResponseStructure<Customer>> saveCustomer( @RequestBody Customer customer ) { return customerService .saveCustomer( customer ); } @PutMapping ( "/customer" ) public ResponseEntity<ResponseStructure<Customer>> updateCustomer( @RequestBody Customer customer ) { return customerService .saveCustomer( customer ); } @DeleteMapping ( "/customer/{id}" ) public ResponseEntity<ResponseStructure<String>> deleteCustomer( @PathVariable int id ) { return customerService .deleteCustomer( id ); } // @GetMapping("/customer/{id}") // public ResponseEntity<ResponseStructure<Customer>> getCustomer(@PathVariable int id) // { // return customerService.getCustomer(id); // } @GetMapping ( "/hosp...

application.properties

Spring boot  application.properties Code #spring.datasource.username=root #spring.datasource.password= admin #spring.datasource.url= jdbc : mysql :// localhost :3306/customer?createDatabaseIfNotExist=true #spring.jpa.hibernate.ddl-auto=update #spring.jpa.properties.format- sql =true #spring.jpa.show- sql =true #spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect spring.datasource.url= jdbc : mysql :// localhost :3306/customer?createDatabaseIfNotExist=true spring.datasource.username= root spring.datasource.password= admin spring.jpa.hibernate.ddl-auto= update spring.jpa.properties.hibernate.format_sql= true spring.jpa.show-sql= true spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect