您好,登錄后才能下訂單哦!
這篇“Spring Data JPA怎么實現(xiàn)持久化存儲數(shù)據(jù)到數(shù)據(jù)庫”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Spring Data JPA怎么實現(xiàn)持久化存儲數(shù)據(jù)到數(shù)據(jù)庫”文章吧。
<properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
application.yml文件如下
# 應用名稱 spring: application: name: springboot-jpa01 # jpa參數(shù)配置 jpa: database: MySQL database-platform: org.hibernate.dialect.MySQL5InnoDBDialect show-sql: true hibernate: ddl-auto: update # 運行環(huán)境設(shè)置 profiles: active: dev # 應用服務(wù) WEB 訪問端口 server: port: 8080
application-dev.yml文件如下
# 應用名稱 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver 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
創(chuàng)建與數(shù)據(jù)庫表映射的實體類,綁定字段之間的對應關(guān)系,如下
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") @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; }
啟動項目成功之后,會自動在數(shù)據(jù)庫中創(chuàng)建數(shù)據(jù)庫表,如果創(chuàng)建數(shù)據(jù)庫表成功,說明JPA框架繼承和配置都是OK的。
以上就是關(guān)于“Spring Data JPA怎么實現(xiàn)持久化存儲數(shù)據(jù)到數(shù)據(jù)庫”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。