使用Java注解实现拼接sql语句的功能

雨点打透心脏的1/2处 2022-07-24 05:21 1446阅读 0赞

使用到的两个注解类:

  1. <span style="font-size:18px;">package dao;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. /**
  7. * Created by raid on 2016/5/14.
  8. */
  9. @Target({ElementType.TYPE})
  10. @Retention(RetentionPolicy.RUNTIME)
  11. public @interface Table {
  12. String value();
  13. }
  14. </span>
  15. package dao;
  16. import java.lang.annotation.ElementType;
  17. import java.lang.annotation.Retention;
  18. import java.lang.annotation.RetentionPolicy;
  19. import java.lang.annotation.Target;
  20. /**
  21. * Created by raid on 2016/5/14.
  22. */
  23. @Target({ElementType.FIELD})
  24. @Retention(RetentionPolicy.RUNTIME)
  25. public @interface Column {
  26. String value();
  27. }
  28. package dao;
  29. /**
  30. * Created by raid on 2016/5/14.
  31. */
  32. @Table("user")
  33. public class Filter {
  34. @Column("id")
  35. private int id;
  36. @Column("user_name")
  37. private String userName;
  38. @Column("nick_name")
  39. private String nickName;
  40. @Column("age")
  41. private int age;
  42. @Column("city")
  43. private String city;
  44. @Column("email")
  45. private String email;
  46. @Column("mobile")
  47. private String mobile;
  48. public int getId() {
  49. return id;
  50. }
  51. public void setId(int id) {
  52. this.id = id;
  53. }
  54. public String getUserName() {
  55. return userName;
  56. }
  57. public void setUserName(String userName) {
  58. this.userName = userName;
  59. }
  60. public String getNickName() {
  61. return nickName;
  62. }
  63. public void setNickName(String nickName) {
  64. this.nickName = nickName;
  65. }
  66. public int getAge() {
  67. return age;
  68. }
  69. public void setAge(int age) {
  70. this.age = age;
  71. }
  72. public String getCity() {
  73. return city;
  74. }
  75. public void setCity(String city) {
  76. this.city = city;
  77. }
  78. public String getEmail() {
  79. return email;
  80. }
  81. public void setEmail(String email) {
  82. this.email = email;
  83. }
  84. public String getMobile() {
  85. return mobile;
  86. }
  87. public void setMobile(String mobile) {
  88. this.mobile = mobile;
  89. }
  90. }

demo类:

  1. package dao;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. /**
  5. * Created by raid on 2016/5/14.
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. Filter f1 = new Filter();
  10. f1.setId(10);//查询ID为10的用户
  11. Filter f2 = new Filter();
  12. f2.setUserName("lucy");
  13. Filter f3 = new Filter();
  14. f3.setEmail("123@12.com,456@45.com,789@78.com");//查询邮箱为其中任意一个
  15. String sql1 = query(f1);
  16. String sql2 = query(f2);
  17. String sql3 = query(f3);
  18. System.out.println(sql1);;
  19. System.out.println(sql2);;
  20. System.out.println(sql3);;
  21. }
  22. private static String query(Filter f) {
  23. StringBuilder sb = new StringBuilder();
  24. //1.获取到class
  25. Class c = f.getClass();
  26. //2.获取到table的名字
  27. boolean exists = c.isAnnotationPresent(Table.class);
  28. if (!exists) {
  29. return null;
  30. }
  31. Table t = (Table) c.getAnnotation(Table.class);
  32. String tableName = t.value();
  33. sb.append("select * from ").append(tableName).append(" where 1=1");
  34. //2.遍历所有的字段
  35. Field[] fArray = c.getDeclaredFields();
  36. for (Field field : fArray) {
  37. //4.处理每个字段对应的sql
  38. //4.1拿到字段名
  39. boolean fExists = field.isAnnotationPresent(Column.class);
  40. if (!fExists) {
  41. continue;
  42. }
  43. Column column = field.getAnnotation(Column.class);
  44. String columName = column.value();
  45. //4.2拿到字段值
  46. String fieldName = field.getName();
  47. String getMethodName = "get" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
  48. Object fieldValue = null;
  49. try {
  50. Method getMethod = c.getMethod(getMethodName);
  51. fieldValue = getMethod.invoke(f);
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. //4.3拼装sql
  56. if (fieldValue == null || (fieldValue instanceof Integer && (Integer)fieldValue == 0)) {
  57. continue;
  58. }
  59. if (fieldValue instanceof String) {
  60. if (((String)fieldValue).contains(",")) {
  61. String[] values = ((String)fieldValue).split(",");
  62. fieldValue = " in(";
  63. for (String v : values) {
  64. fieldValue += "'" + v + "'" + ",";
  65. }
  66. fieldValue = ((String)fieldValue).substring(0, ((String) fieldValue).length()-1) + ")";
  67. } else {
  68. fieldValue = "'" + fieldValue + "'";
  69. }
  70. }
  71. sb.append(" and ").append(fieldName).append("=").append(fieldValue);
  72. }
  73. return sb.toString();
  74. }
  75. }

发表评论

表情:
评论列表 (有 0 条评论,1446人围观)

还没有评论,来说两句吧...

相关阅读

    相关 巧用SQL拼接语句

    前言: 在日常数据库运维过程中,可能经常会用到各种拼接语句,巧用拼接SQL可以让我们的工作方便很多,达到事半功倍的效果。本篇文章将会分享几个日常会用到的SQL拼接案例,类似的

    相关 拼接Sql语句小心得

    在往数据库插入数据时,需要根据数据和数据库中的列信息进行拼接,在本篇文章中,输出小心得。使用语言为 `python`。 拼接原始列信息 比如待插入数据库列信息为 `de