Adding the Required Dependency
Include the Spring Boot Data JPA starter in your POM file to enable JPA repository support:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Configuring Aplication Properties
Set up your database connection and JPA settings in the configuration file:
spring:
datasource:
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: system
password: 123456
jta:
enabled: true
jpa:
show-sql: true
database-platform: org.hibernate.dialect.Oracle12cDialect
hibernate:
ddl-auto: none
The show-sql property enables SQL statement logging, while ddl-auto: none prevents Hibernate from modifying your database schema automatically.
Creating the Entity Class
Define the entity class that maps to your database table. Ensure all column names use consistent casing in the @Column annotations to avoid mapping errors:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
@Entity
@Table(name = "templatetable")
public class ProductInfo implements Serializable {
private static final long serialVersionUID = -7409827055841916106L;
@Id
private String id;
@Column(name = "PRODUCTCODE")
private String productCode;
@Column(name = "PRODUCTNAME")
private String productName;
@Column(name = "CREATETIME")
private Date createdAt;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProductCode() {
return productCode;
}
public void setProductCode(String code) {
this.productCode = code;
}
public String getProductName() {
return productName;
}
public void setProductName(String name) {
this.productName = name;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date timestamp) {
this.createdAt = timestamp;
}
}
Implementing the Repository Layer
Create a repository interface that extends to inherit standard CRUD operations. Define custom query methods following Spring Data JPA's naming conventions:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ProductRepository extends JpaRepository<ProductInfo, String> {
ProductInfo findByProductCode(String code);
List<ProductInfo> findByProductNameContaining(String keyword);
}
The framework automatically generates query logic based on method names. The findByProductCode method produces a query equivalent to SELECT * FROM templatetable WHERE productcode = ?. Spring Data JPA supports various method name patterns including Containing, StartingWith, EndingWith, and logical operators like And and Or.
For advanced query requirements, refer to the official documentation at https://docs.spring.io/spring-data/jpa/docs/2.7.15/reference/html/#jpa.query-methods.
Building the Service Layer
The service layer coordinates business logic by utilizing the repository interface:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class ProductService {
private static final Logger logger = LoggerFactory.getLogger(ProductService.class);
@Autowired
private ProductRepository productRepository;
public List<ProductInfo> getAllProducts() {
return productRepository.findAll();
}
public ProductInfo getProductByCode(String code) {
return productRepository.findByProductCode(code);
}
public ProductInfo saveProduct(ProductInfo product) {
return productRepository.save(product);
}
public void deleteProduct(String id) {
productRepository.deleteById(id);
}
public Optional<ProductInfo> findById(String id) {
return productRepository.findById(id);
}
}
Spring Data JPA provides powerful abstraction over data access patterns, reducing boilerplate code and enabling rapid development of data-driven applications. The repository pattern combined with query method derivasion offers a clean, maintainable approach to database interactions.