intelliJIDEAgradlespringboot对接mysql数据库⽰例前期准备:
安装好mysql并建⽴⼀个数据库以供连接
打开intelliJ IDEA新建⼀个项⽬:
该项⽬的⼀些设置:
Web项下的Web依赖,SQL项下的JPA和MySQL依赖⼀定要勾选上,这样IntelliJ IDEA会在⽣成项⽬时在⾃动⽣成的adle⽂件⾥⾃动写好依赖命令,这样就⽆需后⾯⾃⼰⼿动添加命令语句了。
刚刚初创的项⽬就长这个样⼦,其中的adle已经⾃动⽣成好,内容如下:adle
buildscript {
ext {
springBootVersion = '2.1.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'ample'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
springboot结构}
要连接MySQL数据库,那么先要在项⽬的application.properties⽂件中设置好连接参数:
#ddl-auto:create----每次运⾏该程序,没有表格会新建表格,表内有数据会清空
#ddl-auto:create-drop----每次程序结束的时候会清空表
#ddl-auto:update----每次运⾏程序,没有表格会新建表格,表内有数据不会清空,只会更新
#ddl-auto:validate----运⾏程序会校验数据与数据库的字段类型是否相同,不同会报错
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
项⽬⼤体框架建构好后,我们开始添加关联数据⽤的java bean类,⾸先新建⼀个User类,该类的结构其实就是映射数据库中数据表的结构:
数据库中的表共有id,name,email三个字段,对应User类中的id,name,email:
User.class:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。