一个Java小白的学习之路 个人博客 youngljx.top
Spring Data JPA
Spring Data JPA是Spring基于ORM框架、JPA规范的基础上封装的一套JPA应用框架,是基于Hibernate之上构建的JPA使用解决方案,用极简的代码实现了对数据库的访问和操作,包括了增、删、改、查等在内的常用功能.
SpringBoot整合Spring Data JPA
pom文件添加依赖
<!-- 配置mysql数据库连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 配置jpa依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
properties配置文件
#数据库连接池配置 时区时间是本地时间
spring.datasource.url=jdbc:mysql://localhost:3306/mysql1
?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=GMT%2B8
#注意新版本driver使用:com.mysql.cj.jdbc.Driver 否则会异常
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=qwert
spring.datasource.tomcat.initial-size=10
spring.datasource.tomcat.max-active=20
spring.datasource.tomcat.max-idle=8
spring.datasource.tomcat.min-idle=8
#jpa相关配置
spring.jpa.database = mysql
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect
spring.jpa.open-in-view=false
编写实体类Bean
- 编写公共实体类BaseBean
/**
* @description: 描述实体类的共同配置
* @date: 2020-03-11 12:58
*/
@Data
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseBean {
/**
* @Description: 自增长id
* @Date: 2020/3/11-14:11
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* @Description:创建时间
* @Date: 2020/3/10-22:17
*/
@CreatedDate
@Column(nullable = false ,updatable = false)
protected Date createTime;
/**
* @Description: 修改时间
* @Date: 2020/3/10-22:17
*/