日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

注解和反射实现dao层增删改查

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 注解和反射实现dao层增删改查 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

注解和反射寫dao

  • 1. 注解的使用
  • 2. 使用注解體現映射關系

  • 針對上一篇文章,使用xml映射文件和反射實現dao,提出了擴展功能,利用注解來體現實體類和表的映射關系
  • 本文是上一篇文章的擴展
  • 使用反射和xml實現dao

1. 注解的使用

  • 什么是注解?
@Overridepublic String toString() {return super.toString();}
  • @Override 就是一個注解,表示此方法是重寫了父類的方法,此外還有許多Java里面的內置注解
  • 元注解(Retention, ElementType,Documented,Inherited)
  • 常用的兩個元注解 (Retention, ElementType)
//定義注解在代碼中的保留策略 @Retention(RetentionPolicy.RUNTIME) //RetentionPolicy.RUNTIME 當前注解存在于源代碼和編譯后的class文件中,并加載到jvm中,可以在運行時獲取 //RetentionPolicy.CLASS 當前注解存在于源代碼和編譯后的class文件中,但不會加載到jvm虛擬機 //RetentionPolicy.SOURCE 當前注解只存在于源代碼中,不會編譯到class文件中,也不會加載到jvm虛擬機 @Target(ElementType.TYPE) //指定當前注解能夠用在什么地方 //ElementType.ANNOTATION_TYPE 當前注解可以使用在其他注解上 //ElementType.CONSTRUCTOR 當前注解可以用在構造函數上 //ElementType.FIELD 當前注解可以用在全局變量上 //ElementType.LOCAL_VARIABLE 當前注解可以用在局部變量上 //ElementType.METHOD 當前注解可以用在方法上 //ElementType.PACKAGE 當前注解可以用在包上 //ElementType.PARAMETER 當前注解可以用在參數上 //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() default "default value: 123";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) // 運行時jvm可取到值 @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(); }
  • 以實體類goods為例
package com.lovely.entity;import java.sql.Date;import com.lovely.base.Column; import com.lovely.base.Id; import com.lovely.base.Table;/*** * @author echo lovely* * 注解的缺點 修改代碼 要改變對應的列名**/ @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;// 為減少代碼/我去掉了set/get/構造
  • BaseDao里面增加了, 注解加載映射數據
public static HashMap<String, MapperData> map = new HashMap<String, MapperData>();static {// 利用注解解析 實體類和表的映射關系try {// 通過BaseDao 類的信息 拿到entity里面的所有類Class<?> baseDaoClass = Class.forName("com.lovely.dao.BaseDao");String filePath = baseDaoClass.getResource("/com/lovely/entity/").getFile();// System.out.println(filePath);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 id = fields[i].getAnnotation(Id.class);// System.out.println(id);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();}} // 有了注解加載映射數據,就不要添加xml映射數據了。 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的注解和反射实现dao层增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。