spring boot 系列之四:spring boot 整合JPA
上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化,
这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化。
一、代码实现
- 修改pom,引入依赖
<!-- 引入jpa 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> - 修改application.properties,配置相关信息
#修改tomcat默认端口号
server.port=8090
#修改context path
server.context-path=/test #配置数据源信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
#配置jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true - 创建实体类
package com.study.entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="t_user")
public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String userName;
private String password; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} } - 创建repository接口并继承CrudRepository
package com.study.repository; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param; import com.study.entity.User; /**
* 注意:
* 1.这里这里是interface,不是class
*
* 2.CrudRepository里面的泛型,第一个是实体类,第二个是主键的类型
*
* 3.由于crudRepository 里面已经有一些接口了,如deleteAll,findOne等, 我们直接调用即可
*
* 4.当然,我们也可以根据自己的情况来实现自己的接口,如下面的getUser()方法,jpql语句和hql语句差不多
*
* */
public interface UserRepository extends CrudRepository<User, Integer> { /**
* 我们这里只需要写接口,不需要写实现,spring boot会帮忙自动实现
*
* */ @Query("from User where id =:id ")
public User getUser(@Param("id") Integer id);
} - 创建service
- 接口
package com.study.service; import com.study.entity.User; public interface UserService {
public User getUser(Integer id);
} - 实现
package com.study.service.impl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.study.entity.User;
import com.study.repository.UserRepository;
import com.study.service.UserService; @Service
public class UserServiceImpl implements UserService { @Autowired
UserRepository repository; @Override
public User getUser(Integer id) {
//有两种方式:
//1.调用crudRepository的接口
// return repository.findOne(id);
//2.调用我们自己写的接口
return repository.getUser(id);
} }
- 接口
- 创建controller
package com.study.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.study.entity.User;
import com.study.service.UserService; @RestController
public class UserController {
@Autowired
UserService service; @RequestMapping("/getUser/{id}")
public User getUser(@PathVariable("id") Integer id){ return service.getUser(id);
}
} - 测试,页面以json格式显示数据库值

二、知识点引申
关于Repository知识点,可以去看下下面这篇文章
https://segmentfault.com/a/1190000012346333
spring boot 系列之四:spring boot 整合JPA的更多相关文章
- Spring框架系列(2) - Spring简单例子引入Spring要点
上文中我们简单介绍了Spring和Spring Framework的组件,那么这些Spring Framework组件是如何配合工作的呢?本文主要承接上文,向你展示Spring Framework组件 ...
- Spring框架系列(6) - Spring IOC实现原理详解之IOC体系结构设计
在对IoC有了初步的认知后,我们开始对IOC的实现原理进行深入理解.本文将帮助你站在设计者的角度去看IOC最顶层的结构设计.@pdai Spring框架系列(6) - Spring IOC实现原理详解 ...
- Spring框架系列(7) - Spring IOC实现原理详解之IOC初始化流程
上文,我们看了IOC设计要点和设计结构:紧接着这篇,我们可以看下源码的实现了:Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的. ...
- Spring框架系列(8) - Spring IOC实现原理详解之Bean实例化(生命周期,循环依赖等)
上文,我们看了IOC设计要点和设计结构:以及Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的:容器中存放的是Bean的定义即Be ...
- Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现
前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...
- Spring框架系列(10) - Spring AOP实现原理详解之AOP代理的创建
上文我们介绍了Spring AOP原理解析的切面实现过程(将切面类的所有切面方法根据使用的注解生成对应Advice,并将Advice连同切入点匹配器和切面类等信息一并封装到Advisor).本文在此基 ...
- Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现
我们在前文中已经介绍了SpringAOP的切面实现和创建动态代理的过程,那么动态代理是如何工作的呢?本文主要介绍Cglib动态代理的案例和SpringAOP实现的原理.@pdai Spring框架系列 ...
- Spring框架系列(12) - Spring AOP实现原理详解之JDK代理实现
上文我们学习了SpringAOP Cglib动态代理的实现,本文主要是SpringAOP JDK动态代理的案例和实现部分.@pdai Spring框架系列(12) - Spring AOP实现原理详解 ...
- 【spring boot 系列】spring data jpa 全面解析(实践 + 源码分析)
前言 本文将从示例.原理.应用3个方面介绍spring data jpa. 以下分析基于spring boot 2.0 + spring 5.0.4版本源码 概述 JPA是什么? JPA (Java ...
随机推荐
- Object源码解析(JDK1.8)
package java.lang; public class Object { /** * 一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用 */ private static ...
- 笔记:Spring Cloud Hystrix Command属性
主要用来控制 HystrixCommand 命令的行为,主要有下面5种类型的属性配置: execution配置 该配置前缀为 hystrix.command.default execution.iso ...
- ListView动态刷新adapter.notifyDataSetChanged()无反应
前段时间在做一个动态刷新ListView(模拟聊天),遇到一个问题,调用notifyDataSetChanged()方法,数据源已经存在但是并没有动态刷新! 首先我们需要了解notifyDataSet ...
- strcat函数
原型:char *strcat ( char *dest, const char *src) 用法:#include <string.h> 功能:连接两个字符串:strcat( ...
- spring学习笔记二 注解及AOP
本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...
- MariaDB/MySQL中的变量
在MySQL/MariaDB中有好几种变量类型:用户自定义变量.系统变量.一般的临时变量(即本地变量,或称为局部变量). 1.用户变量 用户变量是基于会话的,也是基于用户的,所以我觉得称之为会话变量更 ...
- alpha冲刺总结随笔
前言:前面乱乱糟糟整了一路,到最后终于可以稳定下来了.安安心心做个总结,然后把之后要做的事情都理清楚好了. 新学长似乎是个正经[并不]大腿. 看起来也不用都是一个人或者跟陈华学长两个人对半开了[突然摸 ...
- 20162302 实验一《Java开发环境的熟悉》实验报告
实 验 报 告 课程:程序设计与数据结构 姓名:杨京典 班级:1623 学号:20162302 实验名称:Java开发环境的熟悉 实验器材:装有Ubuntu的联想拯救者80RQ 实验目的与要求:1.使 ...
- 每日冲刺报告-Day4
敏捷冲刺报告--Day4 情况简介 今天完成前端后端任务对接, GUI主体编写 任务进度 赵坤: 完成后端爬虫 李世钰: 前后端对接, GUI编写 黄亦薇:召集小组成员开会,帮助查找资料,寻找BUG ...
- C语言--第14.15周作业
一. 7-3 将数组中的数逆序存放 1.代码 #include 2<stdio.h> int main() { int a[10]; int i, n, s; scanf("%d ...