java注解在web框架中使用比较广泛,这使得对象之间的关系配置起来更加容易
目前web框架中最常用的两种配置对象依赖关系的方式就是注解和xml配置文件的方法,api配置相对来说用的少一些,
下面实现一个Table注解来实现数据库表和实体bean之间的对应关系,实现一个Column注解来实现数据库表中每个字段和实体bean每个属性之间的
对应关系。java中的orm基本上就是根据这种思想来实现的。
Table注解代码:
package com.panther.dong.annotation.anno; import java.lang.annotation.*; /** * Created by panther on 15-8-3. */ @Target( { ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Table { String value() default ""; }
package com.panther.dong.annotation.anno; import java.lang.annotation.*; /** * Created by panther on 15-8-3. */ @Target( { ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Column { String value() default ""; }创建一个student实体bean,给这个bean打上Table注解和Column注解
package com.panther.dong.annotation.pojo; import com.panther.dong.annotation.anno.Column; import com.panther.dong.annotation.anno.Table; /** * Created by panther on 15-8-3. */ @Table("student") public class Student { @Column("id") private int id; @Column("name") private String name; @Column("age") private int age; @Column("sex") private String sex; public Student() { } public Student(int id, String name, int age, String sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } 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; } }
的取值,拼凑出sql。代码如下:
package com.panther.dong.annotation.parse; import com.panther.dong.annotation.anno.Column; import com.panther.dong.annotation.anno.Table; import com.panther.dong.annotation.pojo.Student; import java.lang.reflect.Field; /** * Created by panther on 15-8-3. */ public class ParsePojo { private final static String QUERY_SQL = "select * from "; /** * 解析注解 * * @param object 被注解的对象 * @return */ public String getSelect(Object object) { String sql = QUERY_SQL; //获得需要查询的表名称 Class cls = object.getClass(); boolean isTable = cls.isAnnotationPresent(Table.class); if (isTable) { Table tableName = (Table) cls.getAnnotation(Table.class); sql += tableName.value(); } //拼凑sql if (object != null) { sql += " where "; } //获取属性对应注解的名称和属性运行时的值 Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); Object val = null; try { val = (Object) field.get(object); } catch (Exception e) { System.out.println("get fileld value failure"); } boolean isColumn = field.isAnnotationPresent(Column.class); if (isColumn) { Column columnName = (Column) field.getAnnotation(Column.class); if (val != null && val instanceof String) { sql += columnName.value() + "=\'" + val + "\'&&"; } else if (val != null && val instanceof Integer) { sql += columnName.value() + "=" + val + "&&"; } } } sql = sql.substring(0, sql.length() - 2); return sql; } public static void main(String[] args) { Student student = new Student(); student.setAge(19); student.setName("panther"); String sql = new ParsePojo().getSelect(student); System.out.println(sql); } }
运行结果得到sql语句,之后再根据sql语句查询数据库得到结果!!!(后续查询数据库应该很简单了)
代码的目录结构为:
java反射和注解在实现框架的类关系之间的配置和资源之间的配置非常有用
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/liu136313/article/details/47361817