标签:long name 相关 rom 判断 问题 int 创建 mic
package anno; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //此注解依次可以在类 属性 方法进行 @Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD}) //运行时可以查看注解信息 @Retention(RetentionPolicy.RUNTIME) public @interface Entity { //注解的参数 public String value(); }
2、定义dao(模拟映射)
package dao; import anno.Entity; @Entity("entity") public class CityEntity { private String id; private String name; public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } }
package util; import anno.Entity; import dao.CityEntity; public class UtilSql { public static String builSqlStr(Object object){ Class clazz = object.getClass( ); // 先判断是否存在注解 if(clazz.isAnnotationPresent(Entity.class)){ //判断是否是自定义Entity注解 Entity entity = (Entity) clazz.getAnnotation(Entity.class); //获取相关注解传的值 String tableName = entity.value(); CityEntity cityEntity = (CityEntity) object; String sql = "select * from "+tableName+" where id =‘"+cityEntity.getId()+"‘ and name = ‘"+cityEntity.getName()+"‘"; return sql; } return "没有获取到相关sql"; } }
4、测试
package test; import dao.CityEntity; import util.UtilSql; public class Test { public static void main(String args[]){ CityEntity cityEntity = new CityEntity(); cityEntity.setId("1"); cityEntity.setName("wjw"); String sql = UtilSql.builSqlStr(cityEntity); System.out.println(sql); } }
5、测试结果
6、先关项目截图
标签:long name 相关 rom 判断 问题 int 创建 mic
原文地址:https://www.cnblogs.com/gnwzj/p/11086945.html