生活随笔
收集整理的這篇文章主要介紹了
注解和反射实现dao层增删改查
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注解和反射寫dao
- 針對上一篇文章,使用xml映射文件和反射實現dao,提出了擴展功能,利用注解來體現實體類和表的映射關系
- 本文是上一篇文章的擴展
- 使用反射和xml實現dao
1. 注解的使用
@Overridepublic String
toString() {return super.toString();}
- @Override 就是一個注解,表示此方法是重寫了父類的方法,此外還有許多Java里面的內置注解
- 元注解(Retention, ElementType,Documented,Inherited)
- 常用的兩個元注解 (Retention, ElementType)
@Retention(RetentionPolicy
.RUNTIME
)
@Target(ElementType
.TYPE
)
package com
.lovely
.test
;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
, ElementType
.TYPE
})
public @
interface MyAnnotation {String
name();
}
2. 使用注解體現映射關系
package com
.lovely
.base
;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
.TYPE
)
public @
interface Table {String
name();
}
package com
.lovely
.base
;@Retention(RetentionPolicy
.RUNTIME
)
@Target(ElementType
.FIELD
)
public @
interface Id {String
idName();String
idColumn();String
seqName();
}
package com
.lovely
.base
;@Retention(RetentionPolicy
.RUNTIME
)
@Target(ElementType
.FIELD
)
public @
interface Column {String
columnName();
}
package com
.lovely
.entity
;import java
.sql
.Date
;import com
.lovely
.base
.Column
;
import com
.lovely
.base
.Id
;
import com
.lovely
.base
.Table
;
@Table(name
="goods_info")
public class Goods {@Id(idName
="gid", idColumn
="goods_id", seqName
="goods_seq")private Integer gid
;@Column(columnName
="goods_name")private String gname
;@Column(columnName
="goods_price")private Double gprice
;@Column(columnName
="goods_date")private Date gdate
;@Column(columnName
="goods_factory")private String gfactory
;
public static HashMap
<String, MapperData> map
= new HashMap<String, MapperData>();static {try {Class
<?> baseDaoClass
= Class
.forName("com.lovely.dao.BaseDao");String filePath
= baseDaoClass
.getResource("/com/lovely/entity/").getFile();File dir
= new File(filePath
);File
[] files
= dir
.listFiles();for (File file
: files
) {String className
= "com.lovely.entity." + file
.getName().substring(0, file
.getName().indexOf("."));Class
<?> entityClass
= Class
.forName(className
);Table table
= entityClass
.getAnnotation(Table
.class);if (table
!= null
) {MapperData mapperData
= new MapperData();mapperData
.setClassName(className
);mapperData
.setTableName(table
.name());Field
[] fields
= entityClass
.getDeclaredFields();for (int i
= 0; i
< fields
.length
; i
++) {Id id
= fields
[i
].getAnnotation(Id
.class);if (id
!= null
) {MapperId mapperId
= new MapperId();mapperId
.setIdName(id
.idName());mapperId
.setIdColumn(id
.idColumn());mapperId
.setSeqName(id
.seqName());mapperData
.setMapperId(mapperId
);}Column column
= fields
[i
].getAnnotation(Column
.class);if (column
!= null
) {mapperData
.getProperties().put(fields
[i
].getName(), column
.columnName());} }map
.put(className
, mapperData
);}}} catch (Exception e
) {e
.printStackTrace();}}
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的注解和反射实现dao层增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。