- 如果我们建表时,使用了设置默认值 ↓ ( 时间,sex 字段被设置默认值) ```java import io.swagger.annotations.ApiModelProperty; import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
/**
- @author tn
- @version 1
- @ClassName TestDemo
- @description 测试
@date 2020/10/2 10:16 */ @Entity @Table(name =”test_demo”,schema = “test”) @org.hibernate.annotations.Table(appliesTo = “test_demo”,comment = “test_demo”) @Getter @Setter @Accessors(chain = true) @DynamicUpdate //动态赋值 @DynamicInsert //动态插入 public class TestDemo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(columnDefinition=”int COMMENT ‘主键,自动生成’”) @ApiModelProperty(value = “id”) private Integer id;
/**
全系统唯一 */ @Column(columnDefinition=” varchar(36) COMMENT ‘全系统唯一uuid’”) @ApiModelProperty(value=”uuid”) @GeneratedValue(generator = “uuid”) @GenericGenerator(name = “uuid”, strategy = “org.hibernate.id.UUIDGenerator”) private String uuid;
@Column(columnDefinition = “ varchar(100) not null comment ‘名字’” ) private String name;
@Column(name = “sex”,columnDefinition=”varchar(100) default ‘男’ COMMENT ‘字典名’”) private String sex;
/**
* 表示该字段为创建时间字段,
*/
@ApiModelProperty(value = "创建时间",hidden=true)
@Column(columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建日期' ")
private String createTime;
/**
* 表示该字段为创建人,
*/
@ApiModelProperty(value = "创建人",hidden=true)
@Column(columnDefinition = "varchar(100) COMMENT '创建人'")
private String createBy;
/**
* 表示该字段为修改时间字段,
*/
@ApiModelProperty(value = "修改时间",hidden=true)
@Column(columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新日期'")
private String updateTime;
/**
* 表示该字段为修改人,
*/
@ApiModelProperty(value = "修改人",hidden=true)
@Column(columnDefinition = "varchar(100) COMMENT '更新人'")
private String updateBy;
}
2. 使用save 方法后直接抛出存入的数据 ,得到的结果如下 ↓
```java
/**
* 新增成功后返回的数据是缓存里的所以没有数据库里默认的数据
* @param jpaDemo
* @return
*/
@GetMapping("addTestDemo")
@ApiOperationSupport(ignoreParameters = {"id","uuid"})//ui中隐藏id字段
@ApiOperation(value = "新增数据TestDemo",notes = "新增数据")
public ResultVO<TestDemo> addTestDemo(TestDemo jpaDemo){
TestDemo save = testDemo.save(jpaDemo);
return ResultVO.resultDataMsgForT(save!=null,save,"新增");
}
{
"code": 0,
"message": "新增成功",
"data": {
"id": 5,
"uuid": null,
"name": "去",
"sex": null,
"createTime": null,
"createBy": null,
"updateTime": null,
"updateBy": null
},
"ts": 1601607038408,
"success": true
}
- 使用 查询语句重新查询一次的结果 如下 ↓ ```java @GetMapping(“findAllTestDemo”) @ApiOperation(value = “findAllTestDemo”) public ResultVO