In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article "Spring Data JPA how to achieve persistent storage of data to the database" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "Spring Data JPA how to achieve persistent storage of data to the database" article bar.
1.SpringBoot Project Integration JPA1.1 pom.xml relies on 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web mysql Mysql-connector-java runtime org.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 profile
The application.yml file is as follows
# Application name spring: application: name: springboot-jpa01 # jpa Parameter configuration jpa: database: MySQL database-platform: org.hibernate.dialect.MySQL5InnoDBDialect show-sql: true hibernate: ddl-auto: update # Runtime Environment Settings profiles: active: dev# Application Service WEB access Port server: port: 8080
The application-dev.yml file is as follows
# Application name 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: root2. Create an entity class
Create an entity class that maps to the database table, and bind the corresponding relationship between the fields, as follows
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: 0031 * / @ 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) @ 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. Start the project, test and verify
After starting the project successfully, the database table is automatically created in the database. If the database table is created successfully, it means that the inheritance and configuration of the JPA framework are OK.
The above is about the content of this article on "how to achieve persistent storage of data to the database by Spring Data JPA". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.