Spring Data JPA 中常用注解

时间:2023-03-09 04:38:39
Spring Data JPA 中常用注解

一、java对象与数据库字段转化

1.@Entity:标识实体类是JPA实体,告诉JPA在程序运行时生成实体类对应表

2.@Table:设置实体类在数据库所对应的表名

3.@Id:标识类里所在变量为主键

4.@GeneratedValue:设置主键生成策略,此方式依赖于具体的数据库

5.@Basic:表示简单属性到数据库表字段的映射(几乎不用)

6.@Column:表示属性所对应字段名进行个性化设置

7.@Transient:表示属性并非数据库表字段的映射,ORM框架将忽略该属性

8.@Temporal:(很重要)

  当我们使用到java.util包中的时间日期类型,则需要此注释来说明转化成java.util包中的类型。

  注入数据库的类型有三种:

    TemporalType.DATE(2008-08-08)

    TemporalType.TIME(20:00:00)

    TemporalType.TIMESTAMP(2008-08-08 20:00:00.000000001)

9.@Enumerated:(很重要)

  使用此注解映射枚举字段,以String类型存入数据库

  注入数据库的类型有两种:EnumType.ORDINAL(Interger)、EnumType.STRING(String)

10.@Embedded@Embeddable

  当一个实体类要在多个不同的实体类中进行使用,而其不需要生成数据库表

  @Embeddable:注解在类上,表示此类是可以被其他类嵌套

  @Embedded:注解在属性上,表示嵌套被@Embeddable注解的同类型类

11.@ElementCollection:集合映射

12.@CreatedDate@CreatedBy@LastModifiedDate@LastModifiedBy:(很重要)

  表示字段为创建时间字段(insert自动设置)、创建用户字段(insert自动设置)、最后修改时间字段(update自定设置)、最后修改用户字段(update自定设置)

  用法:

    1、@EntityListeners(AuditingEntityListener.class):申明实体类并加注解

    2、@EnableJpaAuditing:在启动类中加此注解

    3、在实体类中属性中加上面四种注解

    4、自定义添加用户

Spring Data JPA 中常用注解
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder; @Configuration

public class UserIDAuditorBean implements AuditorAware<Long> {

@Override

public Long getCurrentAuditor() {

SecurityContext ctx = SecurityContextHolder.getContext();

if (ctx == null) {

return null;

}

if (ctx.getAuthentication() == null) {

return null;

}

if (ctx.getAuthentication().getPrincipal() == null) {

return null;

}

Object principal = ctx.getAuthentication().getPrincipal();

if (principal.getClass().isAssignableFrom(Long.class)) {

return (Long) principal;

} else {

return null;

}

}

}
Spring Data JPA 中常用注解

13.@MappedSuperclass:(很重要)

  实现将实体类的多个属性分别封装到不同的非实体类中

  注解的类将不是完整的实体类,不会映射到数据库表,但其属性将映射到子类的数据库字段
  注解的类不能再标注@Entity或@Table注解,也无需实现序列化接口

  注解的类继承另一个实体类 或 标注@MappedSuperclass类,他可使用@AttributeOverride 或 @AttributeOverrides注解重定义其父类属性映射到数据库表中字段。


二、java对象与json转化

1.@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8"):将Date属性转换为String类型, timezone解决(相差8小时)

2.@JsonSerialize:作用在类或字段上,转化java对象到json格式(需自定义转化类继承JsonSerializer<T>)

Spring Data JPA 中常用注解
class DateSerializer extends JsonSerializer<Date> {
@Override
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
</span><span style="color: #0000ff;">throws</span><span style="color: #000000;"> IOException {
SimpleDateFormat formatter </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> SimpleDateFormat(BankAccount.DATE_PATTERN);
jgen.writeString(formatter.format(value));
}

}

Spring Data JPA 中常用注解

3.@JsonDeserialize:作用在类或字段上,转化json格式到java对象(需自定义转化类继承JsonDeserializer<T>)

Spring Data JPA 中常用注解
class DateDeSerializer extends JsonDeserializer<Date> {
@Override
</span><span style="color: #0000ff;">public</span> Date deserialize(JsonParser jp, DeserializationContext ctxt) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> IOException {
Date date;
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
date </span>=<span style="color: #000000;"> DateUtils.parseDate(jp.getText(), BankAccount.DATE_PATTERN);
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> date;
}

}

Spring Data JPA 中常用注解

4.@JsonProperty:作用在属性上,把属性名称序列化为另一个名称(trueName属性序列化为name)

5.@JsonIgnoreProperties(ignoreUnknown = true):作用在类上,忽略掉json数据里包含了实体类没有的字段

6.@JsonIgnore:在json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响

原文地址:https://www.cnblogs.com/pascall/p/10280825.html