Springboot引入hibernate配置自動(dòng)建表并進(jìn)行增刪改查操作_第1頁
Springboot引入hibernate配置自動(dòng)建表并進(jìn)行增刪改查操作_第2頁
Springboot引入hibernate配置自動(dòng)建表并進(jìn)行增刪改查操作_第3頁
Springboot引入hibernate配置自動(dòng)建表并進(jìn)行增刪改查操作_第4頁
Springboot引入hibernate配置自動(dòng)建表并進(jìn)行增刪改查操作_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論