返回顶部
首页 > 资讯 > 后端开发 > Python >Spring Data JPA实现持久化存储数据到数据库的示例代码
  • 184
分享到

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

2024-04-02 19:04:59 184人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

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

1.springBoot项目整合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文件如下

# 应用名称
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.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.创建实体类

创建与数据库表映射的实体类,绑定字段之间的对应关系,如下

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;


@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.启动项目,测试验证

启动项目成功之后,会自动在数据库中创建数据库表,如果创建数据库表成功,说明JPA框架继承和配置都是OK的。

以上就是Spring Data JPA实现持久化存储数据到数据库的示例代码的详细内容,更多关于Spring Data JPA数据存储的资料请关注编程网其它相关文章!

--结束END--

本文标题: Spring Data JPA实现持久化存储数据到数据库的示例代码

本文链接: https://lsjlt.com/news/147550.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作