欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring boot Jpa添加对象字段使用数据库默认值操作

程序员文章站 2022-03-10 13:25:49
目录项目搭建错误测试解决问题jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其余字段用数据库默认值,要是直接用...

jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其余字段用数据库默认值,要是直接用idea生成实体类操作的话会报sqlintegrityconstraintviolationexception异常,我们需要jpa根据传入的对象存在的属性动态生成更新和添加语句需要给实体类添加@dynamicupdate,@dynamicinsert根据对象属性生成动态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 '阿里认证',
  `is_tx` tinyint(1) not null default '0' comment '腾讯认证',
  primary key (`id`)
) engine=innodb auto_increment=2 default charset=utf8

项目搭建

Spring boot Jpa添加对象字段使用数据库默认值操作

代码

配置文件

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依靠实体类

发送请求

Spring boot Jpa添加对象字段使用数据库默认值操作

服务器错误

Spring boot Jpa添加对象字段使用数据库默认值操作

解决问题

修改studententity添加两个注解

  • @dynamicupdate
  • @dynamicinsert

Spring boot Jpa添加对象字段使用数据库默认值操作

服务器更新,再次发送请求

Spring boot Jpa添加对象字段使用数据库默认值操作

Spring boot Jpa添加对象字段使用数据库默认值操作

数据库结果

Spring boot Jpa添加对象字段使用数据库默认值操作

成功~

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。