Springboot引入hibernate配置自動(dòng)建表并進(jìn)行增刪改查操作
前言
有些業(yè)務(wù)比較復(fù)雜,比如我們需要新建10張表,每張表有10個(gè)字段,如果用手工來(lái)操作,肯定非常浪費(fèi)時(shí)間,而且隨著代碼中對(duì)實(shí)體類(lèi)的修改,還要同時(shí)修改數(shù)據(jù)庫(kù)表,有時(shí)候?qū)懼鴮?xiě)著就忘了,代碼改了,數(shù)據(jù)庫(kù)沒(méi)改,這種問(wèn)題使用 hibernate 的自動(dòng)建表就好啦。
一、引入依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
二、配置yml
自動(dòng)建表的配置是ddl-auto,有多個(gè)屬性可選
# 1. none 永遠(yuǎn)以數(shù)據(jù)表字段為準(zhǔn),不做任何修改
# 2. validate 加載hibernate時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu),會(huì)和數(shù)據(jù)庫(kù)中的表進(jìn)行比較,不會(huì)創(chuàng)建新表,但是會(huì)插入新值
# 3. create 每次加載hibernate,重新創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu),這就是導(dǎo)致數(shù)據(jù)庫(kù)表數(shù)據(jù)丟失的原因
# 4. create-drop 加載hibernate時(shí)創(chuàng)建,退出是刪除表結(jié)構(gòu)
# 5. update 加載hibernate自動(dòng)更新數(shù)據(jù)庫(kù)結(jié)構(gòu)(最常用的一個(gè))
server: port: 8081 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true username: root password: root jpa: hibernate: ddl-auto: update # 打印SQL語(yǔ)句 show-sql: true
三、寫(xiě)代碼
1、新建數(shù)據(jù)庫(kù)(空數(shù)據(jù)庫(kù)即可,不要新建表)
2、實(shí)體類(lèi)
@Id代表這是主鍵,@GeneratedValue和@GenericGenerator設(shè)置主鍵策略是UUID
@Column可以不寫(xiě),name是數(shù)據(jù)庫(kù)中的字段名,如果數(shù)據(jù)庫(kù)中要新建的對(duì)應(yīng)字段也叫name,可以不寫(xiě),columnDefinition指定字段在數(shù)據(jù)庫(kù)中的類(lèi)型、長(zhǎng)度、注釋等
package com.xuyijie.test.entity; import jakarta.persistence.*; import lombok.Data; import org.hibernate.annotations.GenericGenerator; /** * @author 徐一杰 * @date 2022/9/19 17:25 * @description */ //JPA(Hibernate)的實(shí)體類(lèi)注解 @Entity //表名 @Table(name = "people") //Lombok @Data public class People { //Id代表這是主鍵,GeneratedValue和GenericGenerator設(shè)置主鍵策略是UUID @Id @GeneratedValue(generator = "id") @GenericGenerator(name = "id", strategy = "uuid.hex") private String id; //Column可以不寫(xiě),name是數(shù)據(jù)庫(kù)中的字段名,如果數(shù)據(jù)庫(kù)中要新建的對(duì)應(yīng)字段也叫name,可以不寫(xiě),columnDefinition指定字段在數(shù)據(jù)庫(kù)中的類(lèi)型、長(zhǎng)度、注釋等 @Column(name = "name", columnDefinition="varchar(255) NOT NULL COMMENT '名字'") private String name; @Column(name = "sex", columnDefinition="varchar(2) NOT NULL COMMENT '性別'") private String sex; @Column(name = "age", columnDefinition="int") private Integer age; }
3、Dao層
JpaRepository<People, String>尖括號(hào)里面填寫(xiě)的是實(shí)體類(lèi)和實(shí)體類(lèi)的主鍵數(shù)據(jù)類(lèi)型,PeopleMapper繼承JpaRepository以后,可以把PeopleMapper 注入到Service里,使用很多內(nèi)置方法
package com.xuyijie.test.mapper; import com.xuyijie.test.entity.People; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; /** * @author 徐一杰 * @date 2022/9/19 17:42 * @description */ @Repository public interface PeopleMapper extends JpaRepository<People, String> { }
4、Controller
package com.xuyijie.test.controller; import com.xuyijie.test.entity.People; import com.xuyijie.test.mapper.PeopleMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author 徐一杰 * @date 2022/9/19 17:12 * @description */ @RestController @RequestMapping("/test") public class Test { @Autowired private PeopleMapper peopleMapper; @GetMapping("/hello/{str}") public String Hello(@PathVariable String str){ People people = new People(); people.setName(str); people.setSex("男"); // 這里的save和findAll都是hibernate自帶的方法,里面還有很多內(nèi)置方法 peopleMapper.save(people); System.out.println(peopleMapper.findAll()); return str; } }
四、測(cè)試結(jié)果
1、表已建好
2、請(qǐng)求接口,打印SQL,插入和查詢(xún)數(shù)據(jù)
使用瀏覽器調(diào)用接口
控制臺(tái)打印出SQL語(yǔ)句,查詢(xún)出我們剛剛插入的一條數(shù)據(jù),主鍵為自動(dòng)生成的UUID
到此這篇關(guān)于Springboot引入hibernate配置自動(dòng)建表并進(jìn)行增刪改查的文章就介紹到這了,更多相關(guān)Springboot hibernate增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序介紹
這篇文章主要介紹了Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Springboot實(shí)現(xiàn)高吞吐量異步處理詳解(適用于高并發(fā)場(chǎng)景)
這篇文章主要介紹了Springboot實(shí)現(xiàn)高吞吐量異步處理詳解(適用于高并發(fā)場(chǎng)景),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11springboot 多數(shù)據(jù)源的實(shí)現(xiàn)(最簡(jiǎn)單的整合方式)
這篇文章主要介紹了springboot 多數(shù)據(jù)源的實(shí)現(xiàn)(最簡(jiǎn)單的整合方式),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11java ArrayBlockingQueue的方法及缺點(diǎn)分析
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于java ArrayBlockingQueue的方法及缺點(diǎn)分析,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。2021-01-01Java中String和StringBuffer及StringBuilder?有什么區(qū)別
這篇文章主要介紹了Java中String和StringBuffer及StringBuilder?有什么區(qū)別,String?是?Java?語(yǔ)言非?;A(chǔ)和重要的類(lèi),更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容2022-06-06MyBatis-Plus樂(lè)觀鎖插件的用法小結(jié)
樂(lè)觀鎖很樂(lè)觀,對(duì)任何事情都保持著一個(gè)樂(lè)觀的態(tài)度,認(rèn)為別人不會(huì)修改數(shù)據(jù),所以不會(huì)上鎖,只是在更新數(shù)據(jù)的時(shí)候,去判斷這條數(shù)據(jù)有沒(méi)有被別人修改過(guò),這篇文章主要介紹了MyBatis-Plus樂(lè)觀鎖插件的用法,需要的朋友可以參考下2022-08-08RedisTemplate.opsForHash()用法簡(jiǎn)介并舉例說(shuō)明
redistemplate.opsforhash是RedisTemplate模板類(lèi)中的一個(gè)方法,用于獲取操作哈希數(shù)據(jù)類(lèi)型的接口,這篇文章主要給大家介紹了關(guān)于RedisTemplate.opsForHash()用法簡(jiǎn)介并舉例說(shuō)明的相關(guān)資料,需要的朋友可以參考下2024-06-06MapStruct實(shí)體轉(zhuǎn)換及List轉(zhuǎn)換的方法講解
今天小編就為大家分享一篇關(guān)于MapStruct實(shí)體轉(zhuǎn)換及List轉(zhuǎn)換的方法講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03