第一节 SpringData介绍
1. Spring Data
Spring Data是持久层通用解决方案,支持关系型数据库 Oracle、MySQL、非关系型数据库NoSQL、Map-Reduce 框架、云基础数据服务 、搜索服务等。
2. Spring Data JPA
Spring Data JPA 框架,主要针对的就是 Spring 唯一没有简化到的持久层操作代码,至此,开发者连仅剩的实现持久层工作都省了,唯一要做的,就只是声明持久层的接口,其他都交给 Spring Data JPA 来帮你完成!
第二节 SpringBoot 整合 JPA
1. 引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
2. yml 配置
spring: datasource: type: com.zaxxer.hikari.HikariDataSource url: jdbc:mysql://localhost:3306/jpa?serverTimezone=Asia/Shanghai username: root password: root jpa: hibernate: ddl-auto: update #自动创建表 show-sql: true #显示SQL语句
3. 创建实体类
package com.qf.jpa.entity; import lombok.Data; import javax.persistence.*; import java.io.Serializable; @Entity //标识实体 //为注解标识的实体指定对应的数据表 @Table(name = "student") @Data public class Student implements Serializable { @Id //ID列标识 @GeneratedValue(strategy = GenerationType.IDENTITY)//主键生成策略 @Column(name = "id", length = 20, nullable = false)//列定义 private Long id; @Column(name = "name", length = 50, nullable = false) private String name; @Column(name = "sex", length = 1, nullable = false) private int sex; &