当前位置: 首页 > 后端技术 > Java

mysql8.0版本用JPA初始化数据表的操作流程

时间:2023-04-01 14:08:59 Java

第一步-配置依赖mysqlmysql-connector-javaruntimeorg.springframework.bootspring-boot-starter-web第二步——application.properties设置配置文件spring.datasource.url=jdbc:mysql://${MYSQL_HOST:127.0.0.1}:3306/yunzhi_spring_boot?useSSL=falsespring.datasource.username=rootspring.datasource.password=000000spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver//MySQL8的spring.datasource.driver-class-name配置需要改为"com.mysql.cj.jdbc.Driver"spring.jpa.hibernate。ddl-auto=updatespring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect//在application.properties中配置数据库引擎,因为InnoDBhbm2ddl.auto有四个属性:被删除,然后再根据你的模块类new生成新表,即使两次没有变化(即删除->创建->执行)deletedwhentheprogramterminatesupdate:最常用的属性。第一次加载hibernate时,会根据model类自动建立表结构(前提是先建立数据库)。后面加载hibernate时,表结构会根据model类自动更新,即使表结构发生变化。但是表中的行仍然存在并且之前的行没有被删除。需要注意的是,当部署到服务器上时,表结构并不会立即建立,要等到应用程序第一次运行时才会建立。(notable->create->operation|withtable->updateattributecolumnwithoutit->operation]validate:每次加载hibernate时,都会校验和创建数据库表结构,只会和里面的表进行比较数据库,并不会创建新表,但会插入新值。(开始验证表结构->验证不成功->项目启动失败)第三步-创建实体类@EntitypublicclassTeacher{@Id@GeneratedValue(strategy=GenerationType.IDENTITY)privateLongid;privateStringname;privateBooleansex;privateStringusername;privateStringemail;privateLongcreateTime;privateLongupdateTime;重启后台,数据库完成相应操作。想了解更详细的流程,推荐观看https://segmentfault.com/a/11...