Java注解学习

利用Java注解实现一个简单的SQL语句生成


User类:对应数据库user表

package cn.guyouda.annotation;

@Table("user")
public class User {
	
	@Column("name")
	private String name;
	
	@Column("password")
	private String password;
	
	@Column("age")
	private int age;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	
}

注解Table:

package cn.guyouda.annotation;

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)
public @interface Table {
	String value();
}

注解Column

package cn.guyouda.annotation;

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)

public @interface Column {
	String value();
}

SQLTest测试类:

package cn.guyouda.annotation;

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

public class SQLTest {

	public static void main(String[] args) {
		User user1 = new User();
		user1.setName("AAA");
		
		User user2 = new User();
		user2.setName("BBB");
		user2.setPassword("bbb");
		
		User user3 = new User();
		user3.setName("CCC");
		user3.setPassword("ccc");
		user3.setAge(21);
		
		String sql1 = createSQL(user1);
		String sql2 = createSQL(user2);
		String sql3 = createSQL(user3);
		
		System.out.println(sql1);
		System.out.println(sql2);
		System.out.println(sql3);
		
	}
	
	/**
	 * 解析注解并生成SQL语句
	 * @param user
	 * @return
	 */
	private static String createSQL(User user){
		StringBuffer sb = new StringBuffer();
		
		Class c = user.getClass();
		
		/**
		 * 判断是否有类注解
		 */
		boolean isExist = c.isAnnotationPresent(Table.class);
		if(!isExist){
			return null;
		}
		
		/**
		 * 获取类注解内容
		 * 即获取表名
		 */
		Table t = (Table)c.getAnnotation(Table.class);
		String tableName = t.value();
		
		sb.append("select * from ").append(tableName).append(" where 1=1");
		
		//获取参数注解
		Field[] field = c.getDeclaredFields();
		for(Field f : field){
			boolean fieldExist = f.isAnnotationPresent(Column.class);
			//只针对自己定义的注解进行处理
			if(!fieldExist){
				continue;
			}
			
			//获取注解值和参数名
			Column col = f.getAnnotation(Column.class);
			String colName = col.value();
			String fieldName = f.getName();
			
			//获取方法名,利用反射方式调用方法
			String methodName = "get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
			Object fieldValue = null;
			
			try {
				Method method = c.getMethod(methodName);
				fieldValue = method.invoke(user);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//过滤不符合要求条件
			if(fieldValue==null || (fieldValue instanceof Integer)&&(Integer)fieldValue==0){
				continue;
			}
			
			//条件拼接
			sb.append(" and ").append(colName);
			if(fieldValue instanceof String){
				sb.append("='").append(fieldValue).append("'");
			}else if(fieldValue instanceof Integer){
				sb.append("=").append(fieldValue);
			}
			
		}
		
		
		return sb.toString();
	}

}



结果:













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值