[Spring] Spring Data JPA
Spring Data jpa
2023-09-14 08:59:14 时间
Previouly we need to define a DAO interface and a DAO impl for 'Employee', it is not so reuseable, since all the DAO has the same structure:
package com.luv2code.springboot.cruddemo.dao; import com.luv2code.springboot.cruddemo.entity.Employee; import java.util.List; public interface EmployeeDAO { public List<Employee> findAll(); public Employee findById (int theId); public void save(Employee theEmployee); public void deleteById(int theId); }
Spring data jpa provids a much simple and reuseable way to do the stuff, we only need to info JPA with 'Employee' entity class and its primary id type. Then JPA will provides us all the methods we need, such ass 'findAll' & 'findById' & 'save' & 'deleteById':
package com.luv2code.springboot.cruddemo.dao; import com.luv2code.springboot.cruddemo.entity.Employee; import org.springframework.data.jpa.repository.JpaRepository; public interface EmployeeRepository extends JpaRepository<Employee, Integer> { // NO need to write any code here }
We just need to update the service to use new Data JPA:
package com.luv2code.springboot.cruddemo.service; import com.luv2code.springboot.cruddemo.dao.EmployeeRepository; import com.luv2code.springboot.cruddemo.entity.Employee; import org.springframework.beans.factory.annotation.Autowired; import java.util.Optional; public class EmployeeServiceImpl implements EmployeeService{ private EmployeeRepository employeeRepository; @Autowired public EmployeeServiceImpl (EmployeeRepository theEmployeeRepository) { employeeRepository = theEmployeeRepository; } @Override public List<Employee> findAll() { return employeeRepository.findAll(); } @Override public Employee findById(int theId) { Optional<Employee> result= employeeRepository.findById(theId); Employee theEmployee = null; if (result.isPresent()) { theEmployee = result.get(); } else { throw new RuntimeException("Did not find employee id - " + theId); } return theEmployee; } @Override public void save(Employee theEmployee) { employeeRepository.save(theEmployee); } @Override public void deleteById(int theId) { employeeRepository.deleteById(theId); } }
相关文章
- spring boot: spring Aware的目的是为了让Bean获得Spring容器的服务
- Spring异常解决 java.lang.NullPointerException,配置spring管理hibernate时出错
- [Spring学习笔记 3 ] spring 注解详解,完全注解,常用注解
- [Java Spring Data] Optional<> query response, orElse, orElseThrow, ifPresent
- [Java Spring data] @Query @Param
- [Java Spring Data] JPA Example with unit test
- spring boot容器启动详解--转
- [Spring Data JPA] Derived Query Methods
- [Spring Boot] Adding JPA and Spring Data JPA
- spring cloud 学习笔记 服务注册与发现(二)
- Spring Data ElasticSearch入门案例
- spring data jpa实现分页查询功能
- spring data jpa实现数据作废(删除)功能
- Fenix — 比 MyBatis 更加强大的 Spring Data JPA 扩展库
- Spring Data Jpa、Jpa、Hibernate、JDBC四者之间的关系
- Atititt hi dev eff db op Spring JDBC 目录 1. Spring JDBC21 1.1. Atitit 数据库db insert 插入数据data 最佳实践
- Atitit spring单元测试 注解 获取服务名 Spring文件单独放在一个文件夹,去掉dubbo配置,方便测试 里面包含的mybatis 找不到,只好设置成相对于class绝对路径可以了
- 【异常】spring-boot-starter-data-elasticsearch 解决远程主机强迫关闭了一个现有的连接。
- Spring Boot 玩转 MinIO:轻松搭建分布式对象存储系统,提升数据管理效率
- 学习Spring Boot:(十四)spring-shiro的密码加密
- Spring整合Hibernate、Hibernate JPA、Spring Data JPA、Spring Data Redis
- spring+redis的集成,使用spring-data-redis来集成
- Spring data jpa批量插入和更新
- 009-Spring Boot 事件监听、监听器配置与方式、spring、Spring boot内置事件
- 使用Java Spring消费MySQL中的数据库存储过程
- SpringBoot入门:SpringBoot的数据库操作:spring-data-jpa的使用
- Spring Cloud Gateway整合Eureka路由转发