In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "SpringBoot+jpa configuration how to automatically create tables according to entity classes". In daily operation, I believe many people have doubts about how to automatically create tables according to entity classes in SpringBoot+jpa configuration. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "SpringBoot+jpa configuration how to automatically create tables according to entity classes". Next, please follow the editor to study!
The jpa configuration automatically creates Table 1. 1 based on entity classes. Configuration file application.propertiesspring.datasource.url=jdbc:mysql://localhost:3306/bootTable?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.jpa.database=mysqlspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=updatespring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect2.pom.xml import package org.springframework.boot spring-boot-starter-data-jpa Mysql mysql-connector-java 3. Writing the entity class import javax.persistence.*;import java.io.Serializable;import java.util.Objects;@Entity// declares that the entity class public class User implements Serializable {@ Id / / declares the attribute @ GeneratedValue / / self-increment private Integer id corresponding to the unique identity of the entity @ Column (nullable = false, unique = true, length = 32) / / length 32, unique index, nullable indicates that true can be empty, false cannot / / the definition of table fields used to declare entity attributes private String userName; private String passWord; private String email; private String nickName; private String regTime; @ Transient / / fields that do not map to columns private String desc; / / omit get and set methods} 4. Run the project
It can be generated immediately after startup.
5. Aiming at the problem that the database did not generate database tables after the start of the project.
Package guide is not correct: import javax.persistence.*
Incorrect configuration file: spring.jpa.hibernate.ddl-auto=update
The note is incorrect: don't forget @ Entity
...
There may also be another reason:
The entry file for Sprint is in a subdirectory, which should be one level higher than others such as service, dao, controller, and entity.
For example, if the service file is located in com.demo.metaService, then the entry file xxxApplication.java should be under com.demo
Jpa automatically generates Table 1 according to Entry. Join dependency org.springframework.boot spring-boot-starter-data-jpa
Delete it if you rely on spring-data-jpa, otherwise there will be errors such as not finding bootstrap
two。 Configure application.yml
Add the configuration of Jpa automatic generation table
Spring: jpa: # # configure automatic table creation: updata: no table creation, table update operation. The console displays the table creation statement hibernate: ddl-auto: update show-sql: true3. Create Entity
It is recommended to create a basic Entity, which can be used to create common fields in the table with mybatisplus,jackson,SnowFlake,lombok and other libraries. Import relevant notes by yourself. Please understand them yourself.
BaseEntry.java
@ Data// omits the setget method @ MappedSuperclass / / tag parent @ EntityListeners (AuditingEntityListener.class) / / jpa data monitor @ JsonIgnoreProperties (value= {"hibernateLazyInitializer", "handler", "fieldHandler"}) / / ignores the parsed field public abstract class BaseEntry implements Serializable {private static final long serialVersionUID = 1L; @ Id @ TableId @ ApiModelProperty (value= "unique identity") private String id = String.valueOf (SnowFlakeUtil.getFlowIdInstance (). NextId ()) @ ApiModelProperty (value = "creator") @ CreatedBy private String createBy; @ CreatedDate @ JsonFormat (timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @ DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss") @ ApiModelProperty (value = "creation time") private Date createTime; @ ApiModelProperty (value = "updater") @ LastModifiedBy private String updateBy @ LastModifiedDate @ JsonFormat (timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @ DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss") @ ApiModelProperty (value = "update time") private Date updateTime; @ ApiModelProperty (value = "delete flag default 0") @ TableLogic private Integer delFlag = CommonConstant.STATUS_NORMAL;}
Business Entry, for reference only
/ * tb_bussiness_up_record entity class * * @ author * * / @ Data@Entity@Table (name = "tb_bussiness_up_record") @ TableName ("tb_bussiness_up_record") public class TbBussinessUpRecord extends BaseEntry {private static final long serialVersionUID = 1L; @ ApiModelProperty (value = "dealer") private String bussinessId @ ApiModelProperty (value = "audit time") @ JsonFormat (timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @ DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss") private String auditTime;} this is the end of the study on "how to configure SpringBoot+jpa to automatically create tables based on entity classes". I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.