




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第Springboot引入hibernate配置自動(dòng)建表并進(jìn)行增刪改查操作目錄前言一、引入依賴二、配置yml三、寫代碼四、測(cè)試結(jié)果
前言
有些業(yè)務(wù)比較復(fù)雜,比如我們需要新建10張表,每張表有10個(gè)字段,如果用手工來操作,肯定非常浪費(fèi)時(shí)間,而且隨著代碼中對(duì)實(shí)體類的修改,還要同時(shí)修改數(shù)據(jù)庫表,有時(shí)候?qū)懼鴮懼屯?,代碼改了,數(shù)據(jù)庫沒改,這種問題使用hibernate的自動(dòng)建表就好啦。
一、引入依賴
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-data-jpa/artifactId
/dependency
dependency
groupIdmysql/groupId
artifactIdmysql-connector-java/artifactId
scoperuntime/scope
/dependency
二、配置yml
自動(dòng)建表的配置是ddl-auto,有多個(gè)屬性可選
server:
port:8081
spring:
datasource:
driver-class-name:com.mysql.cj.jdbc.Driver
url:jdbc:mysql://127.0.0.1:3306/testuseSSL=falseuseUnicode=truecharacterEncoding=UTF-8serverTimezone=Asia/ShanghairewriteBatchedStatements=true
username:root
password:root
jpa:
hibernate:
ddl-auto:update
#打印SQL語句
show-sql:true
三、寫代碼
1、新建數(shù)據(jù)庫(空數(shù)據(jù)庫即可,不要新建表)
2、實(shí)體類
@Id代表這是主鍵,@GeneratedValue和@GenericGenerator設(shè)置主鍵策略是UUID
@Column可以不寫,name是數(shù)據(jù)庫中的字段名,如果數(shù)據(jù)庫中要新建的對(duì)應(yīng)字段也叫name,可以不寫,columnDefinition指定字段在數(shù)據(jù)庫中的類型、長度、注釋等
packagecom.xuyijie.test.entity;
importjakarta.persistence.*;
importlombok.Data;
importorg.hibernate.annotations.GenericGenerator;
*@author徐一杰
*@date2025/9/1917:25
*@description
//JPA(Hibernate)的實(shí)體類注解
@Entity
@Table(name="people")
//Lombok
@Data
publicclassPeople{
//Id代表這是主鍵,GeneratedValue和GenericGenerator設(shè)置主鍵策略是UUID
@GeneratedValue(generator="id")
@GenericGenerator(name="id",strategy="uuid.hex")
privateStringid;
//Column可以不寫,name是數(shù)據(jù)庫中的字段名,如果數(shù)據(jù)庫中要新建的對(duì)應(yīng)字段也叫name,可以不寫,columnDefinition指定字段在數(shù)據(jù)庫中的類型、長度、注釋等
@Column(name="name",columnDefinition="varchar(255)NOTNULLCOMMENT'名字'")
privateStringname;
@Column(name="sex",columnDefinition="varchar(2)NOTNULLCOMMENT'性別'")
privateStringsex;
@Column(name="age",columnDefinition="int")
privateIntegerage;
3、Dao層
JpaRepositoryPeople,String尖括號(hào)里面填寫的是實(shí)體類和實(shí)體類的主鍵數(shù)據(jù)類型,PeopleMapper繼承JpaRepository以后,可以把PeopleMapper注入到Service里,使用很多內(nèi)置方法
packagecom.xuyijie.test.mapper;
importcom.xuyijie.test.entity.People;
importorg.springframework.data.jpa.repository.JpaRepository;
importorg.springframework.stereotype.Repository;
*@author徐一杰
*@date2025/9/1917:42
*@description
@Repository
publicinterfacePeopleMapperextendsJpaRepositoryPeople,String{
}
4、Controller
packagecom.xuyijie.test.controller;
importcom.xuyijie.test.entity.People;
importcom.xuyijie.test.mapper.PeopleMapper;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.PathVariable;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
*@author徐一杰
*@date2025/9/1917:12
*@description
@RestController
@RequestMapping("/test")
publicclassTest{
@Autowired
privatePeopleMapperpeopleMapper;
@GetMapping("/hello/{str}")
publicStringHello(@PathVariableStringstr){
Peoplepeople=newPeople();
people.setName(str);
people.setSex("男");
//這里的save和findAll都是hibernate自帶的方法,里面還有很多內(nèi)置方法
peopleMapper.save(people);
Syste
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 網(wǎng)絡(luò)安全服務(wù)合同及安全承諾條款說明
- 現(xiàn)代管理學(xué)價(jià)值觀試題及答案
- 行政管理在應(yīng)急管理中的角色試題及答案
- 建筑企業(yè)中標(biāo)合同范本
- 2025年學(xué)校餐飲服務(wù)合同簡易版樣本
- 公文寫作實(shí)戰(zhàn)練習(xí)試題及答案
- 建筑材料的質(zhì)量檢驗(yàn)方法試題及答案
- 2025商業(yè)銀行個(gè)人存款(國債)質(zhì)押借款合同
- 2025大連市建筑材料采購合同書
- 行政管理基礎(chǔ)課程的必修知識(shí)與試題及答案
- 檢驗(yàn)批劃分方案14
- 《公共管理學(xué)》期末考試復(fù)習(xí)題庫(含答案)
- 公司實(shí)際控股人協(xié)議書
- 機(jī)械制圖習(xí)題集第九章《裝配圖》匯編
- 天師大和韓國世翰大學(xué)研究生入學(xué)英語試題
- 土建生態(tài)環(huán)保和綠色施工環(huán)境管理培訓(xùn)ppt
- GB/T 27930-2023非車載傳導(dǎo)式充電機(jī)與電動(dòng)汽車之間的數(shù)字通信協(xié)議
- 電力工程項(xiàng)目管理手冊(cè)
- 2023年高考真題-政治(江蘇卷)含解析
- 醫(yī)院化驗(yàn)單模板 血常規(guī)
- 領(lǐng)導(dǎo)干部個(gè)人事項(xiàng)少報(bào)漏報(bào)說明
評(píng)論
0/150
提交評(píng)論