反射 读取 注解 生成sql建表语句(demo)

最后看了太多的源码,为了应聘做了复习,简单地做了一个demo(反射 读取 注解 生成sql建表语句(demo))。。

二话不说,贴代码

//两个注解类,Tab用于注解实体类,Col用于注解字段或方法

package cn.zw.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;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Tab {
   public String name()default  "";
}

package cn.zw.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;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.METHOD})
public @interface Col {
   public String name()default  "";
}

//被注解的实体student

package cn.zw.model;

import cn.zw.annotation.Col;
import cn.zw.annotation.Tab;

@Tab(name="t_student")
public class Student {
 
    private Integer idCard;
    @Col(name="st_age")
    private Integer age;
    private String name;
    private String address;
   
 public Student(){}
 @Col(name="st_idCard")
 public Integer getIdCard() {
  return idCard;
 }
 public void setIdCard(Integer idCard) {
  this.idCard = idCard;
 }
 
 public Integer getAge() {
  return age;
 }
 public void setAge(Integer age) {
  this.age = age;
 }
 @Col(name="st_name")
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 @Col(name="st_address")
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
}

//解析生成sql  生查询被注解的get方法,如果存在注解,就读取注解类属性,否则读取字段上的注解,如果还是没注解,就不理了。

package cn.zw.main;

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

import cn.zw.annotation.Col;
import cn.zw.annotation.Tab;
import cn.zw.model.Student;

public class main {
 public static void main(String[] args) throws Exception {
  Class cls = Student.class;

  parseClass(cls);

 }

 public static String  parseClass(Class cls) throws Exception {
        //拼接建表语句
  StringBuffer retSb = new StringBuffer("insert into ");
  Tab tab = (Tab) cls.getAnnotation(Tab.class);
  if (null != tab) {
   System.out.println("在class类上注解==" + tab.name());
   retSb.append(tab.name()).append(" values( ");
  }else{
   throw new Exception("类未被注解异常");
  }
  for (Method method : cls.getDeclaredMethods()) {
   if (method.getName().startsWith("get")) {
    Col col = method.getAnnotation(Col.class);
    if (null == col) {
     String fieldName = method.getName().replace("get", "");
     StringBuffer sb = new StringBuffer();
     for (int i = 0; i < fieldName.length(); i++) {
      if (0 == i
        && Character.isUpperCase(fieldName.charAt(i))) {
       sb.append(Character.toLowerCase(fieldName.charAt(i)));
      } else {
       sb.append(fieldName.charAt(i));
      }
     }
     Field field = null;
     try {
      field = cls.getDeclaredField(sb.toString());
     } catch (SecurityException e) {
      e.printStackTrace();
     } catch (NoSuchFieldException e) {
      e.printStackTrace();
     } finally {
      if (null != field) {
       col = field.getAnnotation(Col.class);
       System.out.println("在字段上注解==" + col.name());
       retSb.append(col.name()).append(",");
      }
     }

    } else {
     System.out.println("在get方法上注解==" + col.name());
     retSb.append(col.name()).append(",");
    }
   }
  }
  String retStr = "";
  if(retSb.toString().endsWith(",")){
   retStr = retSb.toString().substring(0, retSb.toString().length()-1)+");";
  }
  System.out.println("建表语句==" +retStr );
  return retStr;
 }
}

 

 

 

 

你可能感兴趣的:(反射 读取 注解 生成sql建表语句(demo))