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);
}
}

最新文章

  1. 如何一步一步用DDD设计一个电商网站(二)—— 项目架构
  2. SparkConf加载与SparkContext创建(源码阅读一)
  3. js markdown chart flow
  4. Gmail邮箱添加域名解析
  5. Java中对List集合的排序
  6. hadoop2.2编程:从default mapreduce program 来理解mapreduce
  7. Storyboard 经常用法总结-精华版
  8. 使用Mockito进行单元测试【2】—— stub 和 高级特性[转]
  9. codeforces div2.C
  10. error:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
  11. Doskey命令详解
  12. JSP页面使用include指令出现 Duplicate local variable basePath
  13. MQTT 嵌入式端通讯协议解析(转)
  14. golang - interface的作用
  15. ASPxCallback组件(珍藏版)
  16. indexOf实现引申出来的各种字符串匹配算法
  17. Mockito单测,mock service层的mapper
  18. Oracle性能诊断艺术-读书笔记(脚本execution_plans截图)
  19. 一些兼容性的meta标签
  20. 在WPF中显示动态GIF

热门文章

  1. CSS3伪类与伪元素的区别及注意事项
  2. 【AtCoder】ARC065
  3. shell实践--简单抓取网页内容
  4. thinkphp5日志文件权限的问题
  5. varnish应用
  6. MySQL 聚合函数(二)Group By的修饰符——ROLLUP
  7. 怎样监听HTTP请求的成功、失败与进行时
  8. css鼠标悬浮控制元素隐藏与显示
  9. asp.net core In Docker(Image)
  10. JS写斐波那契数列的几种方法