当前位置:   article > 正文

java 自定义注解应用实例_java 注解例子

java 注解例子

本例子旨在使用自定义注解为实体打上标记,为自动生成 sql 提供依据,模拟 hibernate 的注解,至于注解的原理自己搜吧

1.定义 Table 注解

  1. package test;
  2. import java.lang.annotation.Documented;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Inherited;
  5. import java.lang.annotation.Retention;
  6. import java.lang.annotation.RetentionPolicy;
  7. import java.lang.annotation.Target;
  8. @Inherited
  9. @Target({ElementType.TYPE})
  10. @Retention(RetentionPolicy.RUNTIME)
  11. @Documented
  12. public @interface Table {
  13. String value() default "";
  14. }

2.定义 Column 注解

  1. package test;
  2. import java.lang.annotation.Documented;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Inherited;
  5. import java.lang.annotation.Retention;
  6. import java.lang.annotation.RetentionPolicy;
  7. import java.lang.annotation.Target;
  8. @Inherited
  9. @Target({ElementType.FIELD})
  10. @Retention(RetentionPolicy.RUNTIME)
  11. @Documented
  12. public @interface Column {
  13. String value() default "";
  14. }

3.定义使用注解的实体

  1. package test;
  2. @Table("tb_test")
  3. public class TestDto {
  4. @Deprecated
  5. private String tt;
  6. @Column("_id")
  7. private String id;
  8. @Column("username")
  9. private String name;
  10. public TestDto(String id, String name) {
  11. super();
  12. this.id = id;
  13. this.name = name;
  14. }
  15. public String getId() {
  16. return id;
  17. }
  18. public void setId(String id) {
  19. this.id = id;
  20. }
  21. public String getName() {
  22. return name;
  23. }
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27. }

4.测试注解

  1. package test;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. public class Test {
  5. public static void main(String[] args) {
  6. TestDto testDto = new TestDto("123", "34");
  7. TestDto testDto1 = new TestDto("123", "test1");
  8. TestDto testDto2 = new TestDto("", "test1,test2,test3,test4");
  9. String sql = assembleSqlFromObj(testDto);
  10. String sql1 = assembleSqlFromObj(testDto1);
  11. String sql2 = assembleSqlFromObj(testDto2);
  12. System.out.println(sql);
  13. System.out.println(sql1);
  14. System.out.println(sql2);
  15. }
  16. /**
  17. * 通过注解来组装查询条件,生成查询语句
  18. *
  19. * @param obj
  20. * @return
  21. */
  22. public static String assembleSqlFromObj(Object obj) {
  23. Table table = obj.getClass().getAnnotation(Table.class);
  24. StringBuffer sbSql = new StringBuffer();
  25. String tableName = table.value();
  26. sbSql.append("select * from " + tableName + " where 1=1 ");
  27. Field[] fileds = obj.getClass().getDeclaredFields();
  28. for (Field f : fileds) {
  29. String fieldName = f.getName();
  30. String methodName = "get" + fieldName.substring(0, 1).toUpperCase()
  31. + fieldName.substring(1);
  32. try {
  33. Column column = f.getAnnotation(Column.class);
  34. if (column != null) {
  35. Method method = obj.getClass().getMethod(methodName);
  36. String value = (String) method.invoke(obj);
  37. if (value != null && !value.equals("")) {
  38. if (!isNum(column.value()) && !isNum(value)) {
  39. // 判断参数是不是 in 类型参数 1,2,3
  40. if (value.contains(",")) {
  41. sbSql.append(" and " + column.value() + " in (" + value + ") ");
  42. } else {
  43. sbSql.append(" and " + column.value() + " like '%" + value + "%' ");
  44. }
  45. } else {
  46. sbSql.append(" and " + column.value() + "=" + value + " ");
  47. }
  48. }
  49. }
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. return sbSql.toString();
  55. }
  56. /**
  57. * 检查给定的值是不是 id 类型 1.检查字段名称 2.检查字段值
  58. *
  59. * @param target
  60. * @return
  61. */
  62. public static boolean isNum(String target) {
  63. boolean isNum = false;
  64. if (target.toLowerCase().contains("id")) {
  65. isNum = true;
  66. }
  67. if (target.matches("\\d+")) {
  68. isNum = true;
  69. }
  70. return isNum;
  71. }
  72. }

测试结果:

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)


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/240230
推荐阅读
相关标签
  

闽ICP备14008679号