Springboot引入hibernate配置自動建表并進(jìn)行增刪改查操作
前言
有些業(yè)務(wù)比較復(fù)雜,比如我們需要新建10張表,每張表有10個(gè)字段,如果用手工來操作,肯定非常浪費(fèi)時(shí)間,而且隨著代碼中對實(shí)體類的修改,還要同時(shí)修改數(shù)據(jù)庫表,有時(shí)候?qū)懼鴮懼屯?,代碼改了,數(shù)據(jù)庫沒改,這種問題使用 hibernate 的自動建表就好啦。
一、引入依賴
<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
自動建表的配置是ddl-auto,有多個(gè)屬性可選
# 1. none 永遠(yuǎn)以數(shù)據(jù)表字段為準(zhǔn),不做任何修改
# 2. validate 加載hibernate時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu),會和數(shù)據(jù)庫中的表進(jìn)行比較,不會創(chuàng)建新表,但是會插入新值
# 3. create 每次加載hibernate,重新創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu),這就是導(dǎo)致數(shù)據(jù)庫表數(shù)據(jù)丟失的原因
# 4. create-drop 加載hibernate時(shí)創(chuàng)建,退出是刪除表結(jié)構(gòu)
# 5. update 加載hibernate自動更新數(shù)據(jù)庫結(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語句
show-sql: true
三、寫代碼
1、新建數(shù)據(jù)庫(空數(shù)據(jù)庫即可,不要新建表)
2、實(shí)體類
@Id代表這是主鍵,@GeneratedValue和@GenericGenerator設(shè)置主鍵策略是UUID
@Column可以不寫,name是數(shù)據(jù)庫中的字段名,如果數(shù)據(jù)庫中要新建的對應(yīng)字段也叫name,可以不寫,columnDefinition指定字段在數(shù)據(jù)庫中的類型、長度、注釋等
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í)體類注解
@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可以不寫,name是數(shù)據(jù)庫中的字段名,如果數(shù)據(jù)庫中要新建的對應(yīng)字段也叫name,可以不寫,columnDefinition指定字段在數(shù)據(jù)庫中的類型、長度、注釋等
@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>尖括號里面填寫的是實(shí)體類和實(shí)體類的主鍵數(shù)據(jù)類型,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;
}
}四、測試結(jié)果
1、表已建好

2、請求接口,打印SQL,插入和查詢數(shù)據(jù)
使用瀏覽器調(diào)用接口

控制臺打印出SQL語句,查詢出我們剛剛插入的一條數(shù)據(jù),主鍵為自動生成的UUID


到此這篇關(guān)于Springboot引入hibernate配置自動建表并進(jìn)行增刪改查的文章就介紹到這了,更多相關(guān)Springboot hibernate增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序介紹
這篇文章主要介紹了Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Springboot實(shí)現(xiàn)高吞吐量異步處理詳解(適用于高并發(fā)場景)
這篇文章主要介紹了Springboot實(shí)現(xiàn)高吞吐量異步處理詳解(適用于高并發(fā)場景),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
springboot 多數(shù)據(jù)源的實(shí)現(xiàn)(最簡單的整合方式)
這篇文章主要介紹了springboot 多數(shù)據(jù)源的實(shí)現(xiàn)(最簡單的整合方式),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
java ArrayBlockingQueue的方法及缺點(diǎn)分析
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于java ArrayBlockingQueue的方法及缺點(diǎn)分析,對此有興趣的朋友們可以跟著學(xué)習(xí)下。2021-01-01
Java中String和StringBuffer及StringBuilder?有什么區(qū)別
這篇文章主要介紹了Java中String和StringBuffer及StringBuilder?有什么區(qū)別,String?是?Java?語言非?;A(chǔ)和重要的類,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容2022-06-06
RedisTemplate.opsForHash()用法簡介并舉例說明
redistemplate.opsforhash是RedisTemplate模板類中的一個(gè)方法,用于獲取操作哈希數(shù)據(jù)類型的接口,這篇文章主要給大家介紹了關(guān)于RedisTemplate.opsForHash()用法簡介并舉例說明的相關(guān)資料,需要的朋友可以參考下2024-06-06
MapStruct實(shí)體轉(zhuǎn)換及List轉(zhuǎn)換的方法講解
今天小編就為大家分享一篇關(guān)于MapStruct實(shí)體轉(zhuǎn)換及List轉(zhuǎn)換的方法講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03

