[Spring] Spring Data JPA
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的更多相关文章
- Spring boot data JPA数据库映射关系 : @OneToOne,@OneToMany,@ManyToMany
问题描述 在利用Spring boot data JPA进行表设计的时候,表对象之间经常存在各种映射关系,如何正确将理解的映射关系转化为代码中的映射关系是关键之处. 解决办法 概念理解 举例:在公司的 ...
- Spring boot data jpa 示例
一.maven pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns ...
- Spring Data JPA简单使用
用Spring Data JPA操作数据库 这份教程教你用Spring Data JPA从关系数据库mysql中存储和提取数据.总结来自https://spring.io/guides/gs/acce ...
- Spring------Spring boot data jpa的使用方法
1.DoMain.java import org.springframework.boot.SpringApplication; import org.springframework.boot.aut ...
- 快速搭建springmvc+spring data jpa工程
一.前言 这里简单讲述一下如何快速使用springmvc和spring data jpa搭建后台开发工程,并提供了一个简单的demo作为参考. 二.创建maven工程 http://www.cnblo ...
- spring boot(五):spring data jpa的使用
在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...
- 转:使用 Spring Data JPA 简化 JPA 开发
从一个简单的 JPA 示例开始 本文主要讲述 Spring Data JPA,但是为了不至于给 JPA 和 Spring 的初学者造成较大的学习曲线,我们首先从 JPA 开始,简单介绍一个 JPA 示 ...
- 深入浅出学Spring Data JPA
第一章:Spring Data JPA入门 Spring Data是什么 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得对数据的访问变得方便快捷,并支持map ...
- spring data jpa 调用存储过程
网上这方面的例子不是很多,研究了一下,列出几个调用的方法. 假如我们有一个mysql的存储过程 CREATE DEFINER=`root`@`localhost` PROCEDURE `plus1in ...
- Spring Data JPA 学习记录1 -- 单向1:N关联的一些问题
开新坑 开新坑了(笑)....公司项目使用的是Spring Data JPA做持久化框架....学习了一段时间以后发现了一点值得注意的小问题.....与大家分享 主要是针对1:N单向关联产生的一系列问 ...
随机推荐
- redis通用命令
1.keys pattern 含义:查找所有符合给定模式(pattern)的key keys * 遍历所有key keys he[h-l]* 遍历以he开头,第三个字符为h-l之间的所有key key ...
- 安装matplotlib,报错ERROR: Command errored out with exit status 1:
使用pip install matplotlib 出现报错信息: 发现这行报错 : 我是在pycharm上安装的,可是提示我去安装 Microsoft Visual C++ ,然后去百度查了下,发现只 ...
- 创建安全的 Netty 程序
1.使用 SSL/TLS 创建安全的 Netty 程序 SSL 和 TLS 是众所周知的标准和分层的协议,它们可以确保数据时私有的 Netty提供了SSLHandler对网络数据进行加密 使用Http ...
- 8.perf top系统性能分析工具
perf 是一个调查 Linux 中各种性能问题的有力工具. # perf --help usage: perf [--version] [--help] COMMAND [ARGS] The m ...
- Python 常用内置模块详解
Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...
- k8s认证及serviceAccount、userAccount
1.概述 用kubectl向apiserver发起的命令,采用的是http方式,K8s支持多版本并存. kubectl的认证信息存储在~/.kube/config,所以用curl无法直接获取apis中 ...
- Java 关于String Pool
下面的文章讲得挺清楚: https://www.baeldung.com/java-string-pool 再加一个关于虚拟机的,因为上面的文章提到了JVM: https://abhirockzz.w ...
- git、git bash、git shell
git 一个快速的分布式版本控制系统(工具),支持该工具的网站有Github等. shell 是linux.unix系统的外壳(区别于核),用于输入并执行命令(命令解析器). 它类似于DOS下的com ...
- StoneTab标签页CAD插件 3.2.5
//////////////////////////////////////////////////////////////////////////////////////////////////// ...
- Tomat服务器学习
Tomat服务器学习 使用的是Redhat版本的Tomcat 目录结构 bin:可执行文件 conf:配置文件 lib:tomcat运行时依赖的jar包 logs:日志文件 temp:临时文件 web ...