注釈によるSQLクエリ文の書き込み

4298 ワード

目次:
   1.表注記
    2.表フィールド注記
    3.注釈の使用
    4.解析注記
表注記
package com.pibigstar.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)//     
@Retention(RetentionPolicy.RUNTIME)//     
@Documented//   javadoc
public @interface Table {

	//        ,           value
	//  value ,     , Table("user")
	String value();
}

表フィールド注記
package com.pibigstar.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)//       
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
	
	String value();

}

注釈の使用
package com.pibigstar.bean;

import com.pibigstar.annotation.Column;
import com.pibigstar.annotation.Table;

@Table("user")
public class User {
	
	@Column("id")
	private Integer id;
	
	@Column("user_name")
	private String userName;

	@Column("password")
	private String password;
	
	@Column("age")
	private Integer age;
	
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	
}

解析注記
package com.pibigstar.main;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.pibigstar.annotation.Column;
import com.pibigstar.annotation.Table;
import com.pibigstar.bean.User;

public class Main {
	
	public static void main(String[] args) {
		
		User user1 = new User();
		user1.setId(10);
		
		User user2 = new User();
		user2.setPassword("pibigstar");
		
		User user3 = new User();
		user3.setAge(18);
		user3.setUserName("   ");
		
		String sql1 = query(user1);
		String sql2 = query(user2);
		String sql3 = query(user3);
		
		System.out.println(sql1);
		System.out.println(sql2);
		System.out.println(sql3);
		
		
	}

	/**
	 *     ,  sql  
	 * @param user1
	 * @return
	 */
	private static String query(User user) {
		StringBuffer sql = new StringBuffer();
		/**
		 * 1.      
		 */
		Class c = user.getClass();
		
		/**
		 * 2.    
		 */
		//   user  Table       null
		if(!c.isAnnotationPresent(Table.class)) {
			return null;
		}
		Table table = (Table)c.getAnnotation(Table.class);//  Table    
		String tableName = table.value();//    
		//1=1          
		sql.append("select * from ").append(tableName).append(" where 1=1");
		
		/**
		 * 3.      
		 * 3.1        
		 * 3.2       
		 * 3.3   SQL
		 */
		Field[] fields = c.getDeclaredFields();//      
		for (Field field : fields) {
			if (!field.isAnnotationPresent(Column.class)) {
				continue;
			}
			Column column =(Column)field.getAnnotation(Column.class); 
			String colnumName = column.value();//       
			//     
			String fileName = field.getName();
			String methodName ="get"+fileName.substring(0, 1).toUpperCase()+fileName.substring(1);
			
			Object value = null;
			try {
				Method method = c.getMethod(methodName, null);
				value = method.invoke(user);//    ,      
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//  value   ,     SQL
			if (value==null||(value instanceof Integer && (Integer)value == 0)) {
				continue;
			}
			if (value instanceof String) {
				sql.append(" and ").append(colnumName)
				.append("=").append("'").append(value).append("'");
			}else if (value instanceof Integer) {
				sql.append(" and ").append(colnumName).append("=").append(value);
			}
			
		}
		
		return sql.toString();
	}

}

注釈のより詳細な使用については、私の前のブログを参照してください.http://blog.csdn.net/junmoxi/article/details/77744656