欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Data?JPA實(shí)現(xiàn)持久化存儲(chǔ)數(shù)據(jù)到數(shù)據(jù)庫的示例代碼

 更新時(shí)間:2022年04月27日 16:08:19   作者:嫣夜來  
Spring Data JPA是Spring基于JPA規(guī)范的基礎(chǔ)上封裝的?套 JPA 應(yīng)?框架,可使開發(fā)者?極簡的代碼即可實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的訪問和操作。本文我們來了解如何用Spring?Data?JPA框架實(shí)現(xiàn)數(shù)據(jù)持久化存儲(chǔ)到數(shù)據(jù)庫,感興趣的可以了解一下

1.SpringBoot項(xiàng)目整合JPA

1.1 pom.xml依賴

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

1.2 application配置文件

application.yml文件如下

# 應(yīng)用名稱
spring:
  application:
    name: springboot-jpa01

  # jpa參數(shù)配置
  jpa:
    database: MySQL
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    show-sql: true
    hibernate:
      ddl-auto: update
  # 運(yùn)行環(huán)境設(shè)置
  profiles:
    active: dev

# 應(yīng)用服務(wù) WEB 訪問端口
server:
  port: 8080

application-dev.yml文件如下

# 應(yīng)用名稱
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

2.創(chuàng)建實(shí)體類

創(chuàng)建與數(shù)據(jù)庫表映射的實(shí)體類,綁定字段之間的對(duì)應(yīng)關(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;
}

3.啟動(dòng)項(xiàng)目,測(cè)試驗(yàn)證

啟動(dòng)項(xiàng)目成功之后,會(huì)自動(dòng)在數(shù)據(jù)庫中創(chuàng)建數(shù)據(jù)庫表,如果創(chuàng)建數(shù)據(jù)庫表成功,說明JPA框架繼承和配置都是OK的。

以上就是Spring Data JPA實(shí)現(xiàn)持久化存儲(chǔ)數(shù)據(jù)到數(shù)據(jù)庫的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Spring Data JPA數(shù)據(jù)存儲(chǔ)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java實(shí)現(xiàn)斗地主簡化版

    Java實(shí)現(xiàn)斗地主簡化版

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)斗地主簡化版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Springboot整個(gè)Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的示例代碼

    Springboot整個(gè)Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的示例代碼

    這篇文章主要介紹了Springboot整個(gè)Quartz實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • SpringBoot redis分布式緩存實(shí)現(xiàn)過程解析

    SpringBoot redis分布式緩存實(shí)現(xiàn)過程解析

    這篇文章主要介紹了SpringBoot redis分布式緩存實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • idea導(dǎo)入module全流程

    idea導(dǎo)入module全流程

    這篇文章主要介紹了idea導(dǎo)入module全流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 淺析Spring基于注解的AOP

    淺析Spring基于注解的AOP

    Spring是一個(gè)廣泛應(yīng)用的框架,SpringAOP則是Spring提供的一個(gè)標(biāo)準(zhǔn)易用的aop框架,依托Spring的IOC容器,提供了極強(qiáng)的AOP擴(kuò)展增強(qiáng)能力,對(duì)項(xiàng)目開發(fā)提供了極大地便利
    2022-11-11
  • java實(shí)現(xiàn)圖書管理系統(tǒng)

    java實(shí)現(xiàn)圖書管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • JAVA中4種解析XML文件的方法

    JAVA中4種解析XML文件的方法

    這篇文章主要介紹了JAVA中4種解析XML文件的方法,文中示例代碼非常詳細(xì),幫助大家更好的了解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • mybatis-plus 關(guān)于savebatch,saveorupdatebatch遇到的坑及解決辦法

    mybatis-plus 關(guān)于savebatch,saveorupdatebatch遇到的坑及解決辦法

    本文主要介紹了mybatis-plus 關(guān)于savebatch,saveorupdatebatch遇到的坑及解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • spring boot的maven配置依賴詳解

    spring boot的maven配置依賴詳解

    本篇文章主要介紹了spring boot的maven配置依賴詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • 使用Java實(shí)現(xiàn)類似Comet風(fēng)格的web app

    使用Java實(shí)現(xiàn)類似Comet風(fēng)格的web app

    這篇文章主要介紹了使用Java實(shí)現(xiàn)類似Comet風(fēng)格的web app的方法,包括客戶端的響應(yīng)和XML解析等功能,需要的朋友可以參考下
    2015-11-11

最新評(píng)論