首页 > 代码库 > 注解:java 自定义注解应用实例
注解:java 自定义注解应用实例
本例子旨在使用自定义注解为实体打上标记,为自动生成 sql 提供依据,模拟 hibernate 的注解,至于注解的原理自己搜吧
1.定义 Table 注解
[java] view plain copy
- package test;
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Inherited
- @Target({ElementType.TYPE})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- public @interface Table {
- String value() default "";
- }
2.定义 Column 注解
[java] view plain copy
- package test;
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Inherited;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Inherited
- @Target({ElementType.FIELD})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- public @interface Column {
- String value() default "";
- }
3.定义使用注解的实体
[java] view plain copy
- package test;
- @Table("tb_test")
- public class TestDto {
- @Deprecated
- private String tt;
- @Column("_id")
- private String id;
- @Column("username")
- private String name;
- public TestDto(String id, String name) {
- super();
- this.id = id;
- this.name = name;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
4.测试注解
[java] view plain copy
- package test;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- public class Test {
- public static void main(String[] args) {
- TestDto testDto = new TestDto("123", "34");
- TestDto testDto1 = new TestDto("123", "test1");
- TestDto testDto2 = new TestDto("", "test1,test2,test3,test4");
- String sql = assembleSqlFromObj(testDto);
- String sql1 = assembleSqlFromObj(testDto1);
- String sql2 = assembleSqlFromObj(testDto2);
- System.out.println(sql);
- System.out.println(sql1);
- System.out.println(sql2);
- }
- /**
- * 通过注解来组装查询条件,生成查询语句
- *
- * @param obj
- * @return
- */
- public static String assembleSqlFromObj(Object obj) {
- Table table = obj.getClass().getAnnotation(Table.class);
- StringBuffer sbSql = new StringBuffer();
- String tableName = table.value();
- sbSql.append("select * from " + tableName + " where 1=1 ");
- Field[] fileds = obj.getClass().getDeclaredFields();
- for (Field f : fileds) {
- String fieldName = f.getName();
- String methodName = "get" + fieldName.substring(0, 1).toUpperCase()
- + fieldName.substring(1);
- try {
- Column column = f.getAnnotation(Column.class);
- if (column != null) {
- Method method = obj.getClass().getMethod(methodName);
- String value = (String) method.invoke(obj);
- if (value != null && !value.equals("")) {
- if (!isNum(column.value()) && !isNum(value)) {
- // 判断参数是不是 in 类型参数 1,2,3
- if (value.contains(",")) {
- sbSql.append(" and " + column.value() + " in (" + value + ") ");
- } else {
- sbSql.append(" and " + column.value() + " like ‘%" + value + "%‘ ");
- }
- } else {
- sbSql.append(" and " + column.value() + "=" + value + " ");
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return sbSql.toString();
- }
- /**
- * 检查给定的值是不是 id 类型 1.检查字段名称 2.检查字段值
- *
- * @param target
- * @return
- */
- public static boolean isNum(String target) {
- boolean isNum = false;
- if (target.toLowerCase().contains("id")) {
- isNum = true;
- }
- if (target.matches("\\d+")) {
- isNum = true;
- }
- return isNum;
- }
- }
测试结果:
select * from tb_test where 1=1 and _id=123 and username=34
select * from tb_test where 1=1 and _id=123 and username like ‘%test1%‘
select * from tb_test where 1=1 and username in (test1,test2,test3,test4)
注解:java 自定义注解应用实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。