亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring?boot?Jpa添加對象字段使用數(shù)據(jù)庫默認(rèn)值操作

 更新時間:2021年11月23日 11:01:25   作者:AgPtAu  
這篇文章主要介紹了Spring?boot?Jpa添加對象字段使用數(shù)據(jù)庫默認(rèn)值操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

jpa做持久層框架,項目中數(shù)據(jù)庫字段有默認(rèn)值和非空約束,這樣在保存對象是必須保存一個完整的對象,但在開發(fā)中我們往往只是先保存部分特殊的字段其余字段用數(shù)據(jù)庫默認(rèn)值,要是直接用idea生成實體類操作的話會報SQLIntegrityConstraintViolationException異常,我們需要jpa根據(jù)傳入的對象存在的屬性動態(tài)生成更新和添加語句需要給實體類添加@DynamicUpdate,@DynamicInsert根據(jù)對象屬性生成動態(tài)update和insert語句。

建庫建表

CREATE TABLE `student` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL COMMENT '姓名',
  `age` int(3) NOT NULL DEFAULT '0' COMMENT '年齡',
  `adder` varchar(255) NOT NULL DEFAULT '北京' COMMENT '地址',
  `class` int(15) NOT NULL DEFAULT '0' COMMENT '班級',
  `is_ali` tinyint(1) NOT NULL DEFAULT '0' COMMENT '阿里認(rèn)證',
  `is_tx` tinyint(1) NOT NULL DEFAULT '0' COMMENT '騰訊認(rèn)證',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

項目搭建

代碼

配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

StudnetController

package com.myjpa.demo.controller;
import com.myjpa.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudnetController {
    @Autowired
    StudentService student;
    @PutMapping("/add")
    public String insertStudent(String name, Integer age) {
        student.addStudent(name, age);
        return "添加成功";
    }
}

StudentService

package com.myjpa.demo.service;
import com.myjpa.demo.entity.StudentEntity;
import com.myjpa.demo.respository.StudentRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(rollbackFor = Exception.class)
public class StudentService {
    @Autowired
    StudentRespository studentRespository;
    public void addStudent(String name, Integer age) {
        StudentEntity s = new StudentEntity();
        s.setName(name);
        s.setAge(age);
        studentRespository.save(s);
    }
}

StudentRespository

package com.myjpa.demo.respository;
import com.myjpa.demo.entity.StudentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRespository extends JpaRepository<StudentEntity, Long> {
}

StudentEntity 實體類可以使用idea反向生成

package com.myjpa.demo.entity;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "student", schema = "test")
public class StudentEntity {
    private long id;
    private String name;
    private int age;
    private String adder;
    private int clazz;
    private byte isAli;
    private byte isTx;
    @Id
    @Column(name = "id")
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    @Basic
    @Column(name = "name")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Basic
    @Column(name = "age")
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Basic
    @Column(name = "adder")
    public String getAdder() {
        return adder;
    }
    public void setAdder(String adder) {
        this.adder = adder;
    }
    @Basic
    @Column(name = "class")
    public int getClazz() {
        return clazz;
    }
    public void setClazz(int clazz) {
        this.clazz = clazz;
    }
    @Basic
    @Column(name = "is_ali")
    public byte getIsAli() {
        return isAli;
    }
    public void setIsAli(byte isAli) {
        this.isAli = isAli;
    }
    @Basic
    @Column(name = "is_tx")
    public byte getIsTx() {
        return isTx;
    }
    public void setIsTx(byte isTx) {
        this.isTx = isTx;
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        StudentEntity that = (StudentEntity) o;
        return id == that.id &&
                age == that.age &&
                clazz == that.clazz &&
                isAli == that.isAli &&
                isTx == that.isTx &&
                Objects.equals(name, that.name) &&
                Objects.equals(adder, that.adder);
    }
    @Override
    public int hashCode() {
        return Objects.hash(id, name, age, adder, clazz, isAli, isTx);
    }
}

DemoApplication

package com.myjpa.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

錯誤測試

問題主要在于實體類,因為jpa生成sql依靠實體類

發(fā)送請求

服務(wù)器錯誤

解決問題

修改StudentEntity添加兩個注解

  • @DynamicUpdate
  • @DynamicInsert

服務(wù)器更新,再次發(fā)送請求

數(shù)據(jù)庫結(jié)果

成功~

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Java設(shè)計模式之職責(zé)鏈模式

    詳解Java設(shè)計模式之職責(zé)鏈模式

    責(zé)任鏈模式是一種行為設(shè)計模式,使多個對象都有機(jī)會處理請求,從而避免請求的發(fā)送者和接收者之間的耦合關(guān)系,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • 使用反射方式獲取JPA Entity的屬性和值

    使用反射方式獲取JPA Entity的屬性和值

    這篇文章主要介紹了使用反射方式獲取JPA Entity的屬性和值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java實現(xiàn)冒泡排序與雙向冒泡排序算法的代碼示例

    Java實現(xiàn)冒泡排序與雙向冒泡排序算法的代碼示例

    這篇文章主要介紹了Java實現(xiàn)冒泡排序與雙向冒泡排序算法的代碼示例,值得一提的是所謂的雙向冒泡排序并不比普通的冒泡排序效率來得高,注意相應(yīng)的時間復(fù)雜度,需要的朋友可以參考下
    2016-04-04
  • 詳解JNI到底是什么

    詳解JNI到底是什么

    JNI是Java Native Interface的縮寫,通過使用 Java本地接口書寫程序,可以確保代碼在不同的平臺上方便移植。從Java1.1開始,JNI標(biāo)準(zhǔn)成為java平臺的一部分,它允許Java代碼和其他語言寫的代碼進(jìn)行交互
    2021-06-06
  • 幾個好用Maven鏡像倉庫地址(小結(jié))

    幾個好用Maven鏡像倉庫地址(小結(jié))

    這篇文章主要介紹了幾個好用Maven鏡像倉庫地址(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java使用二分法進(jìn)行查找和排序的示例

    Java使用二分法進(jìn)行查找和排序的示例

    這篇文章主要介紹了Java使用二分法進(jìn)行查找和排序的示例,二分插入排序和二分查找是基礎(chǔ)的算法,需要的朋友可以參考下
    2016-04-04
  • idea遠(yuǎn)程調(diào)試spark的步驟講解

    idea遠(yuǎn)程調(diào)試spark的步驟講解

    今天小編就為大家分享一篇關(guān)于idea遠(yuǎn)程調(diào)試spark的步驟講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • 詳解Spring的@Value作用與使用場景

    詳解Spring的@Value作用與使用場景

    這篇文章主要介紹了詳解Spring的@Value作用與使用場景,Spring為大家提供許多開箱即用的功能,@Value就是一個極其常用的功能,它能將配置信息注入到bean中去,需要的朋友可以參考下
    2023-05-05
  • Springboot關(guān)于自定義stater的yml無法提示問題解決方案

    Springboot關(guān)于自定義stater的yml無法提示問題解決方案

    這篇文章主要介紹了Springboot關(guān)于自定義stater的yml無法提示問題及解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06
  • 解決sharding JDBC 不支持批量導(dǎo)入問題

    解決sharding JDBC 不支持批量導(dǎo)入問題

    這篇文章主要介紹了解決sharding JDBC 不支持批量導(dǎo)入問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評論