Spring|Spring Data JPA实现持久化存储数据到数据库的示例代码
目录
- 1.SpringBoot项目整合JPA
- 1.1 pom.xml依赖
- 1.2 application配置文件
- 2.创建实体类
- 3.启动项目,测试验证
1.SpringBoot项目整合JPA
1.1 pom.xml依赖
1.8 UTF-8UTF-82.3.7.RELEASEorg.springframework.boot spring-boot-starter-data-jpaorg.springframework.boot spring-boot-starter-webmysql mysql-connector-javaruntimeorg.projectlombok lombokorg.springframework.boot spring-boot-starter-testtestorg.junit.vintage junit-vintage-engineorg.springframework.boot spring-boot-dependencies${spring-boot.version} pom import
1.2 application配置文件
application.yml文件如下
# 应用名称spring:application:name: springboot-jpa01# jpa参数配置jpa:database: MySQLdatabase-platform: org.hibernate.dialect.MySQL5InnoDBDialectshow-sql: truehibernate:ddl-auto: update# 运行环境设置profiles:active: dev# 应用服务 WEB 访问端口server:port: 8080
application-dev.yml文件如下
# 应用名称spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/yg-jpa?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8username: rootpassword: root
2.创建实体类 创建与数据库表映射的实体类,绑定字段之间的对应关系,如下
package com.kkarma.web.entity; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import org.hibernate.annotations.GenericGenerator; import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.format.annotation.DateTimeFormat; import javax.persistence.*; import java.time.LocalDateTime; /** * @Author: karma * @Date: 2022/3/31 0031 - 03 - 31 - 15:13 * @Description: com.kkarma.web.entity * @version: 1.0 */@Entity@Table(name = "sys_member")@Datapublic class Member {@Id()@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "member_id")private Long memberId; @Column(name = "member_name", unique = true, nullable = false, length = 64)private String memberName; @Column(name = "password", length = 256)@JsonIgnoreprivate String password; @Column(name = "dept_id")private Integer deptId; @Column(name = "realname", length = 64)private String realName; @Column(name = "avatar", length = 500)private String avatar; @Column(name = "phone", length = 11)private String phone; @Column(name = "email", length = 64)private String email; @Column(name = "gender", length = 1)private Integer gender; @CreatedDate@Column(name = "gmt_create", updatable = false)@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")private LocalDateTime gmtCreate; @CreatedBy@Column(name = "created_by", updatable = false, length = 64)private String createdBy; @LastModifiedDate@Column(name = "gmt_modified")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")private LocalDateTime gmtModified; @LastModifiedBy@Column(name = "updated_by", length = 64)private String updatedBy; @Column(name = "remark", length = 64)private String remark; }
3.启动项目,测试验证 启动项目成功之后,会自动在数据库中创建数据库表,如果创建数据库表成功,说明JPA框架继承和配置都是OK的。
文章图片
文章图片
【Spring|Spring Data JPA实现持久化存储数据到数据库的示例代码】以上就是Spring Data JPA实现持久化存储数据到数据库的示例代码的详细内容,更多关于Spring Data JPA数据存储的资料请关注脚本之家其它相关文章!
推荐阅读
- Spring|Spring Data JPA框架的核心概念与Repository接口详解
- SpringMVC加载WebApplicationContext源码分析
- WinXP电脑C盘里的Application Data是啥?可以删除么?
- Springboot|Springboot jpa使用sum()函数返回结果如何被接收
- Springboot整合Swagger3全注解配置(springdoc-openapi-ui)
- SpringBoot如何访问html和js等静态资源配置
- SpringBoot整合redis中的JSON序列化文件夹操作小结
- What is “passive data structure” in Android/Java?
- springboot的application.properties与.yml的区别
- 我司Spring Boot 项目打包 + Shell 脚本部署详细总结,太有用了!