• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Springboot如何根据实体类生成数据库表

springboot 搞代码 4年前 (2022-01-05) 25次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了Springboot如何根据实体类生成数据库表的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Springboot 实体类生成数据库表

JPA:springboot -jpa:数据库的一系列的定义数据持久化的标准的体系

学习的目的是:

利用springboot实现对数据库的操作

第一步:添加springboot-data-jpa和数据库的依赖关系

  org.springframework.bootspring-boot-starter-data-jpa mysqlmysql-connector-java

第二步:编写yml文件的配置

 server: port: 8001 spring: application: name: jih-manage datasource: <div style="color:transparent">来源gaodai^.ma#com搞#代!码网</div>name: test url: jdbc:mysql://111.231.231.56/jih username: root password: root type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver jpa: hibernate: ddl-auto: update show-sql: true 

第三步:实体类中使用的注解

  • @Entity 实体类的注解
  • @Id 映射到表格中id的属性
  • @Gernertervalue 添加其自增的属性

第四步:启动项目是否生成表格

补充的知识点:

根据实体类生成数据库的表配置文件有俩种方式分别是yml和properties文件进行配置

yml文件:

 spring: datasource: driver-class-name:  com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/facemap username: root password: root jpa: hibernate: ddl-auto: update show-sql: true 

properties文件的写法:

 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/dbgirl?characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.jpa.show-sql= true spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jackson.serialization.indent_output=false 

有更加详细介绍

参考网址:

//www.gaodaima.com/article/222622.htm

实体类的写法:

 package com.example.demo; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @Entity //实体类的注解 public class Girl { @Id //@id注意选择这个javax.persistence @GeneratedValue private  Integer  id; private  String   cupSize; private  Integer   age; public Girl() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } 

第五步:启动项目即可

完成~

springboot继承JPA根据实体类生成数据库中的表

首先搭建springboot框架。搭建完成之后:

1. pom中添加的依赖

   org.springframework.bootspring-boot-starter-data-jdbc org.springframework.bootspring-boot-starter-web org.mybatis.spring.bootmybatis-spring-boot-starter2.1.1 org.springframework.bootspring-boot-starter-data-jpa<!--mysql-connection--> mysqlmysql-connector-java8.0.15 org.projectlomboklomboktrue org.springframework.bootspring-boot-starter-testtest  org.junit.vintagejunit-vintage-engine

2. application.yml中配置jpa配置

 server: port: 8080 spring: datasource: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/h5mall?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 hikari: minimum-idle: 5 idle-timeout: 180000 maximum-pool-size: 10 auto-commit: true pool-name: MyHikariCP connection-timeout: 30000 jpa: hibernate: ddl-auto: update show-sql: true

其中jpa下的jpa.hibernate.ddl-auto属性值有如下:

  • ddl-auto:create (每次运行该程序,没有表格会新建表格,表内有数据会清空)
  • ddl-auto:create-drop (每次程序结束的时候会清空表)
  • ddl-auto:update (每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新)
  • ddl-auto:validate(运行程序会校验数据与数据库的字段类型是否相同,不同会报错)

一般情况下选择update,其他属性值慎用!

定义用户实体类,通过注解映射成数据库中的表

 import javax.persistence.*; @Entity @Table(name = "user") @Data public class User { @Id @GeneratedValue private Long id; //name属性为表的字段名。length为字段的长度 @Column(length = 30, name = "userId") private String userId; @Column(name = "userName", length = 20, columnDefinition="varchar(100) COMMENT '用户名'") private String userName; @Column(name = "phone", length = 20) private String phone; @Column(name = "password", length = 30) private String password; @Column(name = "userRealName", length = 20) private String userRealName; @Column(name = "address", length = 20) private String address; }

启动springboot项目

可看到控制台上显示了创建表中的

然后查看数据库中是否生成了对应的表:

以上就是Springboot如何根据实体类生成数据库表的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Springboot如何根据实体类生成数据库表

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址