一、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、自定義添加用戶
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;
}
}
}
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>)
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)); }
}
3.@JsonDeserialize:作用在類或字段上,轉化json格式到java對象(需自定義轉化類繼承JsonDeserializer<T>)
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; }
}
4.@JsonProperty:作用在屬性上,把屬性名稱序列化為另一個名稱(trueName屬性序列化為name)
5.@JsonIgnoreProperties(ignoreUnknown = true):作用在類上,忽略掉json數據里包含了實體類沒有的字段
6.@JsonIgnore:在json序列化時將java bean中的一些屬性忽略掉,序列化和反序列化都受影響