Halo(四)

欢迎光临我的博客[http://poetize.cn],前端使用Vue2,聊天室使用Vue3,后台使用Spring Boot

BeanWrapper 接口 操作属性

package org.springframework.beans;

BeanWrapper bw = new BeanWrapperImpl(beanObject);

bw.isReadableProperty("age");	//判断age属性是否可读

bw.setPropertyValue("name", "tom");	//设置name属性的值为tom

bw.getPropertyValue("name");	//取得属性值


PropertyDescriptor[] propertyDescriptors = beanWrapper.getPropertyDescriptors();

Set<String> emptyNames = new HashSet<>();

for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
	String propertyName = propertyDescriptor.getName();
	Object propertyValue = beanWrapper.getPropertyValue(propertyName);
	if (propertyValue == null) {
		emptyNames.add(propertyName);
	}
}

ParameterizedType 参数化类型

public interface ParameterizedType extends Type {

    Type[] getActualTypeArguments();

    Type getRawType();

    Type getOwnerType();
}


List<String>

具有 <> 符号的变量是参数化类型。

Type[] getActualTypeArguments()	//获取参数化类型(String)

Type getRawType()	//获取类型(List)

Type getOwnerType()	//O<T>.I<S>类型的变量,调用getOwnerType()会返回O


(ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
(ParameterizedType) this.getClass().getGenericInterfaces()).getActualTypeArguments()[0];


获取类的接口信息

1. 返回实现接口信息的Type数组,包含泛型信息

	Type[] java.lang.Class.getGenericInterfaces()

2.返回实现接口信息的Class数组,不包含泛型信息

	Class<?>[] java.lang.Class.getInterfaces()

1中包含2

@MappedSuperclass 数据库到实体类映射(JPA 注解)

1. 标注为 @MappedSuperclass 的类将不是一个完整的实体类。
	他将不会映射到数据库表,但是他的属性都将映射到其子类的数据库字段中。

2. 标注为 @MappedSuperclass 的类不能再标注 @Entity 或 @Table 注解,也无需实现序列化接口。

@Temporal 注解(Hibernate JPA 日期注解)

储存在数据库的日期格式

TemporalType.DATE:'yyyy-MM-dd' 格式的日期

TemporalType.TIME:'HH:MM:SS' 格式的日期

TemporalType.TIMESTAMP:'yyyy-MM-dd hh:MM:ss' 格式的日期

在核心的 JPA API 中没有定义 Date 类型的精度,
数据库中表示 Date 类型的数据有 DATE,TIME,TIMESTAMP 三种精度,可通过 @Temporal 注解进行调整。

columnDefinition 属性(JPA 注解)

columnDefinition 属性表示创建表时,该字段创建的SQL语句,一般用于通过 Entity 生成表定义时使用。
如果数据库中表已经建好,该属性没有必要使用。

@Column(name = "Email",columnDefinition="varchar(128) not null")
private String email;

@PreUpdate 和 @PrePersist 注解(JPA 注解)

@PreUpdate
	Update 之前被调用。
    该注释可以应用于实体类,映射超类或回调监听器类的方法,用于setter()。

@PrePersist
	帮助您在持久化之前自动填充实体属性。
	Save 之前被调用(实体插入数据库之前)。

@PostPersist
	Save 到 Datastore 之后被调用(实体插入数据库之后)。

@PostLoad
	在 Entity 被映射(数据载入实体)之后被调用。

@PreRemove 和 @PostRemove
	事件的触发由删除实体引起。
	@PreRemove 事件在实体从数据库删除之前触发。

@NoRepositoryBean(JPA 注解)

使用了该注解的接口不会被单独创建实例,只会作为其他接口的父接口而被使用。


@NoRepositoryBean
public interface BaseRepository<DOMAIN, ID> extends JpaRepository<DOMAIN, ID> {

    @NonNull
    List<DOMAIN> findAllByIdIn(@NonNull Iterable<ID> ids, @NonNull Sort sort);
}

public interface OptionRepository extends BaseRepository<Option, Integer> {

    Optional<Option> findByKey(String key);

    void deleteByKey(String key);
}
posted @ 2019-11-21 18:00  LittleDonkey  阅读(179)  评论(0编辑  收藏  举报