Customer.java
package com.bill.electricity.entity;
//import javax.persistence.*; // ✅ Use javax instead of jakarta
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Customer {
A
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // ✅ Fix: use IDENTITY for H2
private Long customerId;
private String name;
private String email;
private String mobile;
private String address;
private double billAmount;
// Getters and Setters
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getBillAmount() {
return billAmount;
}
public void setBillAmount(double billAmount) {
this.billAmount = billAmount;
}
}
ElectricityApplication,java
package com.bill.electricity;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ElectricityApplication {
public static void main(String[] args) {
SpringApplication.run(ElectricityApplication.class, args);
}
}
CustomerController.java
package com.bill.electricity.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bill.electricity.entity.Customer;
import com.bill.electricity.service.CustomerService;
@RestController
@RequestMapping("/api/customer")
@CrossOrigin(origins = "
http://localhost:4200")
public class CustomerController {
@Autowired
private CustomerService customerService;
@PostMapping("/register")
public Customer register(@RequestBody Customer customer) {
return customerService.registerCustomer(customer);
}
@GetMapping("/view/{id}")
public Customer viewBill(@PathVariable Long id) {
return customerService.getCustomerById(id);
}
}
CustomerRepository.java
package com.bill.electricity.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.bill.electricity.entity.Customer;
// This gives you basic CRUD operations automatically
public interface CustomerRepository extends JpaRepository {
// Custom finder method
Customer findByCustomerId(Long customerId);
}
CustomerService.java
package com.bill.electricity.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bill.electricity.entity.Customer;
import com.bill.electricity.repository.CustomerRepository;
@Service
public class CustomerService {
@Autowired
private CustomerRepository customerRepository;
public Customer registerCustomer(Customer customer) {
return customerRepository.save(customer);
}
public Customer getCustomerById(Long id) {
return customerRepository.findByCustomerId(id);
}
}
ApplicationProperties
spring.application.name=electricity
spring.datasource.url=
jdbc:h2:mem:electricitydb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.hibernate.ddl-auto=update
Submit your DMCA takedown request here to
report copyright infringement.