Because when writing custom annotations in the front, it is really difficult to write a demo. Of course, after learning some reflections, I will write a demo. Then take advantage of this weekend's free time, write an incoming Class, and automatically generate a database A demo of the statement
1. Let's use it first
String userSql = SqlSentence.create ( User.class ) ; //The calling instance created by the database statement
结果:create table UserTable(id integer primary key autoincrement,userName varchar(8),info text,password varchar(16))
2. Look at the User class
public class User { @SqlField (length = 8 ) private String userName ; //The generated data statement is userName varchar(8) @SqlField private int password ; //The generated data statement is password varchar(16) because the default length is 16 @SqlField (type = "text" ) private String info ; //The generated data statement is info text private int age ; //There is no declaration that @SqlField annotation does not generate private String sex ; //There is no declaration that @SqlField annotation does not generate public String getUserName() { return userName ; } public void setUserName(String userName) { this.userName = userName; } public int getPassword() { return password; } public void setPassword(int password) { this.password = password; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
3. Look at the custom annotation class SqlField
/** * Configure database statement fields */ @Retention (RetentionPolicy. RUNTIME ) @Target ({ElementType. FIELD }) public @ interface SqlField { /** * 字段的类型 * @return varchar */ String type() default "varchar"; /** * 字段的长度 * @return 16 */ int length() default 16; }
4.看SqlSentence类
public class SqlSentence { private SqlSentence() { /* cannot be instantiated */ } private final static String VARCHAR = "varchar"; /** * @param clz class * @return sqlite sentence */ public static <T> String create(Class<T> clz) { if (clz == null) { return null; } try { Object instance = Class.forName(clz.getName()).newInstance(); Class<?> aClass = instance.getClass(); Field[] fields = aClass.getDeclaredFields(); StringBuilder sb = new StringBuilder(); for (Field field : fields) { field.setAccessible(true); if (field.isAnnotationPresent(SqlField.class)) { //判断是否存在此注解 SqlField sqlField = field.getAnnotation(SqlField.class); //获取此注解 String fieldName = field.getName(); String type = sqlField.type(); int length = sqlField.length(); if (type.contains(VARCHAR)) { sb.append(fieldName).append(" " + VARCHAR + "(").append(length).append("),"); } else { //用户自定义类型 sb.append(fieldName).append(" ").append(type).append(","); } } } int length = sb.toString().length(); if (length > 1) { //使用生成的字符串长度判断是否存在注解 sb = sb.deleteCharAt(sb.toString().length() - 1); //删除字符串的最后一个字符 return "create table " + clz.getSimpleName() + "Table" + "(id integer primary key autoincrement," + sb + ")"; } else { throw new NullPointerException("No @SqlField annotations are configured on a field"); } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } }
好了,一个简单的反射+自定义注解实现生成数据库语句的demo就ok了,如果对Android开发有所了解的人,可以看一看我在
Android系列中所写的Sqlite数据库SDK,这是一个完全运用到反射+自定义注解去写的一个小框架,当然如果在Android系列中
没有这篇文章,说明我暂时还木有更新上去。