說明:
獲取實體對象表名,主要是爲了獲取表名做特殊的業務,但是獲取表名不一定要這樣獲取,但是這是爲了統一而實現的功能,其實是在這裏面實現的,https://blog.csdn.net/qq_38428623/article/details/105234896
1、實體類型
import javax.persistence.Table;
@Data
@Table(name = "Test")
public class Test {
/**
* 主鍵
*/
@Id
private String id;
/**
* 請假人ID
*/
@Column(name = "name")
private String name;
}
2、基本查詢(僅核心代碼)
/**
* T:實體類
*/
public class BaseQuery<T> {
/**
* 獲取實體類的表名
*/
public String getTableName(){
finalParameterizedType paraType = (ParameterizedType)
this.getClass().getGenericSuperclass();
final Type[] types = paraType.getActualTypeArguments();
String tableName = null;
for (final Type type : types) {
final Annotation annotation = ((Class) type).getAnnotation(Table.class);
if (annotation == null) {
continue;
}
tableName = ((Table) annotation).name();
break;
}
return tableName;
}
}