Spring Data JPA实现持久化存储数据到数据库的示例代码

网友投稿 286 2022-08-01


目录1.SpringBoot项目整合JPA1.1 pom.xml依赖1.2 application配置文件2.创建实体类3.启动项目,测试验证

1.SpringBoot项目整合JPA

1.1 pom.xml依赖

1.8

UTF-8

UTF-8

2.3.7.RELEASE

org.springframework.boot

spring-boot-starter-datahttp://-jpa

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java&lthttp://;/artifactId>

runtime

org.http://projectlombok

lombok

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-dependencies

${spring-boot.version}

pom

import

1.2 application配置文件

application.yml文件如下

# 应用名称

spring:

application:

name: springboot-jpa01

# jpa参数配置

jpa:

database: MySQL

database-platform: org.hibernate.dialect.MySQL5InnoDBDialect

show-sql: true

hibernate:

ddl-auto: update

# 运行环境设置

profiles:

active: dev

# 应用服务 WEB 访问端口

server:

port: 8080

application-dev.yml文件如下

# 应用名称

spring:

datasource:

driver-class-name: com.mysql.cj.jdbc.Driverhttp://

url: jdbc:mysql://localhost:3306/yg-jpa?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8

username: root

password: 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")

@Data

public 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)

@JsonIgnore

private 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")

@Dahttp://teTimeFormat(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 Data JPA实现持久化存储数据到数据库的示例代码的详细内容,更多关于Spring Data JPA数据存储的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Data JPA框架快速入门之自定义Repository接口
下一篇:Spring Data JPA框架的核心概念与Repository接口详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~