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] Spring Data JPA的更多相关文章

  1. Spring boot data JPA数据库映射关系 : @OneToOne,@OneToMany,@ManyToMany

    问题描述 在利用Spring boot data JPA进行表设计的时候,表对象之间经常存在各种映射关系,如何正确将理解的映射关系转化为代码中的映射关系是关键之处. 解决办法 概念理解 举例:在公司的 ...

  2. Spring boot data jpa 示例

    一.maven pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...

  3. Spring Data JPA简单使用

    用Spring Data JPA操作数据库 这份教程教你用Spring Data JPA从关系数据库mysql中存储和提取数据.总结来自https://spring.io/guides/gs/acce ...

  4. Spring------Spring boot data jpa的使用方法

    1.DoMain.java import org.springframework.boot.SpringApplication; import org.springframework.boot.aut ...

  5. 快速搭建springmvc+spring data jpa工程

    一.前言 这里简单讲述一下如何快速使用springmvc和spring data jpa搭建后台开发工程,并提供了一个简单的demo作为参考. 二.创建maven工程 http://www.cnblo ...

  6. spring boot(五):spring data jpa的使用

    在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...

  7. 转:使用 Spring Data JPA 简化 JPA 开发

    从一个简单的 JPA 示例开始 本文主要讲述 Spring Data JPA,但是为了不至于给 JPA 和 Spring 的初学者造成较大的学习曲线,我们首先从 JPA 开始,简单介绍一个 JPA 示 ...

  8. 深入浅出学Spring Data JPA

    第一章:Spring Data JPA入门 Spring Data是什么 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得对数据的访问变得方便快捷,并支持map ...

  9. spring data jpa 调用存储过程

    网上这方面的例子不是很多,研究了一下,列出几个调用的方法. 假如我们有一个mysql的存储过程 CREATE DEFINER=`root`@`localhost` PROCEDURE `plus1in ...

  10. Spring Data JPA 学习记录1 -- 单向1:N关联的一些问题

    开新坑 开新坑了(笑)....公司项目使用的是Spring Data JPA做持久化框架....学习了一段时间以后发现了一点值得注意的小问题.....与大家分享 主要是针对1:N单向关联产生的一系列问 ...

随机推荐

  1. 小菜鸟之crond

    前一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个 ...

  2. 常见三种加密(MD5、非对称加密,对称加密)

    转载. https://blog.csdn.net/SSY_1992/article/details/79094556 任何应用的开发中安全都是重中之重,在信息交互异常活跃的现在,信息加密技术显得尤为 ...

  3. Netty源码剖析-接受数据

    参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! ----主线:worker thread ①多路复用器(Select ...

  4. C++ STL Vector学习 (待续)

    头文件:<vector> 初始化 vector<Elementtype> vec(); /*Elementtype是数据类型,10代表长单为10*/ vector<Ele ...

  5. hyper-v虚拟机centos7网络配置

    原文地址:https://jingyan.baidu.com/article/91f5db1b0279bd1c7e05e377.html hyper-v安装了centos7之后并不能上网,这里简单介绍 ...

  6. python基础知识0-1

    绝对值:abs age = -19 age.__abs__() 19 相加: add age.__add__() 与运算:and age.__add__() 比较两个数大小:cmp age._cmp_ ...

  7. Spring与Web框架(例如Spring MVC)漫谈——关于Spring对于多个Web框架的支持

    在看Spring MVC的官方文档时,最后一章是关于Spring对于其它Web框架的支持(如JSF,Apache Struts 2.x,Tapestry 5.x),当然Spring自己的MVC框架Sp ...

  8. hdu 3371 有毒的卡时间题目

    同样的代码 每次交的结果都不一样 #include<stdio.h> #include<string.h> #include<stdlib.h> #include& ...

  9. spring——aop详细总结1

    AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补 ...

  10. JDK + Tomcat 安装 + 制作自定义镜像【第 3 篇 系统镜像】

    [第 1 篇 JDK]:https://www.cnblogs.com/del88/p/11842387.html[第 2 篇 Tomcat]:https://www.cnblogs.com/del8 ...