# 注解Annotation
特別在系統框架的搭建過程中,往往需要很多的配置文件,比如數據源、IOC、AOP、框架本身的映射(如 Hibernate 表與字段),需要的配置文件會導致系統越來越復雜,而且維護很不方便,一旦配置文件出錯,很容易導致系統無法運行。
在 JDK1.5 以后,引入了注解 Annotation 技術,極大的方便了配置化的編程,很多的技術和框架都使用了技術,如 Servlet/Filter/Spring/SpringMVC/Hibernate/Nutz。
## 定義
~~~
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@Documented
public @interface Column {
boolean hump() default true;
String name() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
public @interface Table {
String value();
}
~~~
## 應用
~~~
package com.ntqingniao.annotation;
import java.util.Date;
@Table("t_sys_account")
public class Account {
@Column(name="ID")
private Integer id;
@Column
private String name;
@Column(name="PASSWOR")
private String password;
@Column(hump=true)
private Date createTime;
private String desc;
}
~~~
## 解析注解數據
~~~
package com.ntqingniao.annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
public class Client2 {
public static void main(String[] args) throws Exception {
Account account = new Account();
account.setId(10);
account.setName("Tom");
account.setPassword("123456");
account.setCreateTime(new Date());
Student stu = new Student();
stu.setId(12);
stu.setName("helen");
stu.setPassword("aaaaaa");
stu.setDesc("hello,helen");
stu.setCreateTime(new Date());
System.out.println(gerInsertSql(account));
System.out.println(gerInsertSql(stu));
// insert into t_sys_account(ID,NAME,PASSWORD,CREATE_TIME) values(10,
// "Tom", "123456", "20170711");
}
public static String gerInsertSql(Object account) throws Exception {
String sql = "insert into ";
String values = "";
Class<?> c = null;
c = account.getClass();
Table table = c.getAnnotation(Table.class);
String tableName = table.value();
sql += tableName + "(";
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
Column column = field.getAnnotation(Column.class);
String columnName = field.getName();
if (null != column) {
// 首先判斷hump屬性是否為true
columnName = Client3.toHump(columnName);
// 否則取name屬性值
String cn = column.name();
if (null != cn && !cn.trim().equals("")) {
columnName = cn;
} else {
// 如果name屬性值為空,則取字段名
}
sql += columnName + ",";
// 獲取字段的屬性值,并拼寫到values變量中
Method getMethod = c.getMethod("get" + Client3.captureName(field.getName()));
Object obj = getMethod.invoke(account);
values += Client3.changeToSql(obj) + ",";
}
}
sql = sql.substring(0, sql.length() - 1) + ")" + " values(" + values.substring(0, values.length() - 1) + ")";
return sql;
}
}
~~~
- 前言
- 計算機概論
- 數據庫
- 數據庫介紹
- MySQL的安裝
- SQL
- 表基本操作
- 修改數據語句
- 數據檢索操作
- 多表數據操作
- 表結構設計
- 綜合應用
- JAVA
- JAVA 介紹
- JAVA 運行原理
- JDK 配置
- 類和對象
- 數據類型
- 變量
- 直接量
- 運算符
- 流程控制
- 數組結構
- 面向對象
- 隱藏和封裝
- 深入構造器
- 類的繼承
- 多態
- 包裝類
- final 修飾符
- 抽象類
- 接口
- 集合框架
- 常用類學習
- 異常處理
- 設計模式-單例模式
- JDBC
- JSP&Servlet
- Web應用
- Tomcat
- JSP
- Scriptlet
- Page 指令
- 包含指令
- 跳轉指令
- 用戶注冊實例
- JSP練習
- 內置對象
- Servlet
- 過濾器
- Web分層思想
- EL表達式
- JSTL
- 分頁實現
- AJAX&JSON
- 開發步驟
- 路徑問題
- Log4j
- 電子書城
- 案例分析
- 核心代碼
- Java 高級
- 文件操作
- 泛型
- 類加載機制和反射
- 注解 Annotation
- Mybatis框架
- 框架介紹
- Mybatis簡單實現
- 表基本操作
- 優化配置文件
- 表字段名與實體類屬性名不同的解決方案
- 一對一關聯
- 一對多關聯
- 教學管理
- 學員名錄
- 周測統計
- 2017-10-27
- 2017-11-03
- 2017-11-10
- 2017-11-17
- 課堂作業
- 班會紀要
- 2017-10-24
- 缺勤記錄
- 班級備忘錄
- 違紀統計
- 編程素養
- Day001
- Day002
- Day003
- Day004
- Day005
- Day006
- Day007
- Day008
- Day009
- Day010
- Day011
- Day012
- Day013
- Day014
- Day015
- Day016
- Day017
- Day018
- Day019