




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第Springboot引入hibernate配置自動建表并進行增刪改查操作目錄前言一、引入依賴二、配置yml三、寫代碼四、測試結(jié)果
前言
有些業(yè)務(wù)比較復(fù)雜,比如我們需要新建10張表,每張表有10個字段,如果用手工來操作,肯定非常浪費時間,而且隨著代碼中對實體類的修改,還要同時修改數(shù)據(jù)庫表,有時候?qū)懼鴮懼屯耍a改了,數(shù)據(jù)庫沒改,這種問題使用hibernate的自動建表就好啦。
一、引入依賴
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-data-jpa/artifactId
/dependency
dependency
groupIdmysql/groupId
artifactIdmysql-connector-java/artifactId
scoperuntime/scope
/dependency
二、配置yml
自動建表的配置是ddl-auto,有多個屬性可選
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、實體類
@Id代表這是主鍵,@GeneratedValue和@GenericGenerator設(shè)置主鍵策略是UUID
@Column可以不寫,name是數(shù)據(jù)庫中的字段名,如果數(shù)據(jù)庫中要新建的對應(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)的實體類注解
@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ù)庫中要新建的對應(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尖括號里面填寫的是實體類和實體類的主鍵數(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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 安全與可靠性試題及答案
- 深度分析2025年能源行業(yè)智能電網(wǎng)優(yōu)化與能源互聯(lián)網(wǎng)產(chǎn)業(yè)鏈圖譜報告
- 安全環(huán)保試題及答案大全
- 2025年成人教育線上學(xué)習(xí)模式創(chuàng)新與學(xué)習(xí)評價工具研發(fā)報告001
- 2025年文化與科技融合趨勢下的數(shù)字博物館數(shù)字化技術(shù)應(yīng)用案例研究報告
- 中國醫(yī)保體制培訓(xùn)課件
- 員工培訓(xùn)視頻課件
- 中國制度自信課件
- 再貼現(xiàn)政策課件
- 北京十一學(xué)校2025屆八年級英語第二學(xué)期期中考試試題含答案
- 2024年中鐵(天津)軌道交通投資建設(shè)限公司運營管理人員招聘5人高頻考題難、易錯點模擬試題(共500題)附帶答案詳解
- 2021年中國社會科學(xué)院大學(xué)統(tǒng)計學(xué)原理期末精練試卷
- 放射科疑難病例分析討論記錄
- 2024年《軍事理論》考試題庫附答案(含各題型)
- 廣東省中山市2022-2023學(xué)年高二下學(xué)期期末數(shù)學(xué)試題(學(xué)生版+解析)
- 2024年國家開放大學(xué)《統(tǒng)計學(xué)原理》形成性考核1-4題目及答案
- 《無衣》教學(xué)設(shè)計 統(tǒng)編版高中語文選擇性必修上冊
- 合肥市住宅小區(qū)物業(yè)服務(wù)等級標(biāo)準(zhǔn)
- 創(chuàng)造心智與創(chuàng)新訓(xùn)練智慧樹知到期末考試答案2024年
- 食品廠員工衛(wèi)生培訓(xùn)方案
- 反恐組織機構(gòu)管理制度
評論
0/150
提交評論