免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
java annotation(注解)
通過一個(gè)例子來認(rèn)識(shí)注解:由javaBean注解生成建表sql
定義表名注解
Java代碼 
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/*注解的分類
* 1.標(biāo)記注解(marker annotation)
* 注解體內(nèi)沒有定義任何元素,只起一個(gè)標(biāo)記提示作用
* 常見的就是java.lang包下的Deprecated,Override,SuppressWarnings
Deprecated 編譯時(shí)會(huì)提示方法過時(shí)
Override 編譯時(shí)驗(yàn)證重寫父類方法簽名是否正確
SuppressWarnings 取消警告
2.元注解
只用來修飾注解定義的注解
下面用到的Retention,Target
Retention用來指定定義的注解要保留到什么時(shí)候
有三個(gè)枚舉值:
RetentionPolicy.SOURCE 編譯是會(huì)調(diào)用,不會(huì)保留到class文件中
RetentionPolicy.CLASS  會(huì)跟隨保留到class文件中
RetentionPolicy.RUNTIME 保留到class文件中,并且class被加載時(shí)還可以通過反射操作注解
Target用來規(guī)定注解可以修飾的程序元素的種類
其有一個(gè)ElementType[]的枚舉數(shù)組參數(shù)
ElementType.PACKAGE 包
ElementType.TYPE 類,接口,注解,枚舉
ElementType.METHOD 方法聲明
ElementType.FIELD  字段
......
* 注解一旦定義好之后,就可以像使用public,static這樣的的modifiers一樣,用注解修飾類,方法或?qū)傩?div style="height:15px;">
*/
@Retention(RetentionPolicy.RUNTIME)//可以保留到類被加載運(yùn)行時(shí)
@Target(ElementType.TYPE) //指定該注解用來修飾類...
public @interface Table { //定義注解的關(guān)鍵字@interface
/*
* 元素定義的返回類型限定為:基本類型,String,Class,emum,annotation
或者是前述類型的數(shù)組
*/
String name();
}
定義字段注解
Java代碼 
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/*
* 定義字段的注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) //該注解只能用在成員變量上
public @interface Column {
int length() default 0; //用來存放字段的長度
String name() default "" ;//用來存放字段的名字
//至于數(shù)據(jù)庫字段的類型,后面根據(jù)反射獲取成員變量的類型獲取
}
定義普通javaBean,用上面的注解界定建表sql
Java代碼 
 
import java.util.Date;
/*
* 一個(gè)簡(jiǎn)單使用例子,根據(jù)注解生成創(chuàng)建表語句
* 使用注解時(shí),可以用key-value鍵值對(duì)的形式為注解的元素賦值
*/
@Table(name="table_person") //表名
public class PersonBean {
@Column(length=8,name="person_id")
private Integer id;
@Column(length=32,name="pname")
private String name;
@Column(name="birth") //Date類型不需要指定length
private Date birth;
public Integer getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
解析注解生成建表sql
Java代碼 
 
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
public class TestMain {
//用來解析所有成員變量的方法
public static String[] getColumns(Field[] fArr){
String[] columns = new String[fArr.length];
String columnName="";
int columnLength=0;
String columnType = "";
for(int i=0;i<fArr.length;i++){
Field f = fArr[i];
String name = f.getName(); //成員變量名
Class type = f.getType(); //成員變量類型
//判斷該成員變量上是不是存在Column類型的注解
if(f.isAnnotationPresent(Column.class)){
//存在
Column c = f.getAnnotation(Column.class);//獲取實(shí)例
//獲取元素值
columnName = c.name();
columnLength = c.length();
}
//如果未指定列名,默認(rèn)列名使用成員變量名
if("".equals(columnName)){
columnName = name;
}
//如果未指定字段長度,默認(rèn)32
if(0 == columnLength){
columnLength = 32;
}
//如果成員變量是String類型的,數(shù)據(jù)庫字段是varchar類型
if(String.class == type){
columnType = "varchar";
}else if(Integer.class == type){
columnType = "number";//Integer類型的用number
}
//把每一個(gè)成員變量相關(guān)信息存放到返回?cái)?shù)組中
if(Date.class == type){//Date類型的用date
columns[i] = columnName+" date";
}else{
columns[i] =  columnName+" "+columnType+"("+columnLength+")";
}
}
return columns;
}
public static void main(String[] args) throws Exception {
StringBuffer sql = new StringBuffer("create table ");
Class c1 = Class.forName("com.cao.annotation.PersonBean");//加載使用注解的bean,(bean的路徑包括bean的包)
if(c1.isAnnotationPresent(Table.class)){
//該class存在Table類型的注解,獲取指定的表名
Table table = (Table) c1.getAnnotation(Table.class);
String tableName = table.name();
sql.append(tableName+" (");
}
//獲取bean所聲明的成員變量(include private)
Field[] fArr = c1.getDeclaredFields();
//解析這些字段的注解設(shè)定值
String[] columns = getColumns(fArr);
//拼接解析后的成員變量信息成創(chuàng)建表語句
for(int i=0;i<columns.length;i++){
if(i==(columns.length-1)){
sql.append("\n"+columns[i]+")");
}else{
sql.append("\n"+columns[i]+",");
}
}
System.out.println(sql.toString());
/*結(jié)果:
*  create table table_person (
person_id number(8),
pname varchar(32),
birth date)
*/
}
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
深入理解Java:注解(Annotation)自定義注解入門
Java自定義注解
[直呼內(nèi)行] 注解開發(fā)才是簡(jiǎn)潔之道,讓同行直呼內(nèi)行的Java注解技術(shù)
Java深入注解
深入理解Java 注解原理
@interface java注解
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服