案例五:利用注解封装
重复步骤
- 我们使用jdbc操作mysql时发现,操作不同表中数据,所写的方法基本相同;比如我们根据id向用户表添加数据,根据id删除商品表的数据,或者查询所有数据并用list集合接收
| int add(int id); |
| |
| int del(int id); |
| |
| List<Blog> getAll(); |
| |
| List<User> getAll(); |
| |
解决思路
- 我们发现实现这些方法的sql语句基本上是相同的,操作不同表中的数据时,需要的表名和字段不同;那么我们是否可以将共有的sql语句提取出来,获取表名和字段后,动态拼接sql语句
- 所要解决的问题是:获取将要操作的表的表名和字段,这里我们通过自定义注解获取
- 思路:通过自定义注解保存实体类对应的表的信息,获取表的表名和字段,最后拼接sql语句
实践操作
自定义注解
| @Target(ElementType.TYPE) |
| @Retention(RetentionPolicy.RUNTIME) |
| public @interface Table { |
| |
| public String name() default ""; |
| } |
| |
| @Target(ElementType.FIELD) |
| @Retention(RetentionPolicy.RUNTIME) |
| public @interface Cloumn { |
| |
| public String value() default ""; |
| } |
| |
编写实体类
| |
| |
| |
| @Table(name = "t_user") |
| public class User { |
| |
| private Integer id; |
| private Integer age; |
| @Cloumn(value = "user_name") |
| private String userName; |
| private String sex; |
| private Date birthday; |
| public Integer getId() { |
| return id; |
| } |
| public void setId(Integer id) { |
| this.id = id; |
| } |
| public Integer getAge() { |
| return age; |
| } |
| public void setAge(Integer age) { |
| this.age = age; |
| } |
| public String getUserName() { |
| return userName; |
| } |
| public void setUserName(String userName) { |
| this.userName = userName; |
| } |
| public String getSex() { |
| return sex; |
| } |
| public void setSex(String sex) { |
| this.sex = sex; |
| } |
| public Date getBirthday() { |
| return birthday; |
| } |
| public void setBirthday(Date birthday) { |
| this.birthday = birthday; |
| } |
| @Override |
| public String toString() { |
| return "User [id=" + id + ", age=" + age + ", userName=" + userName + ", sex=" + sex + ", birthday=" + birthday |
| + "]"; |
| } |
| } |
| |
封装工具类
| public class AnnotationUtil { |
| |
| |
| |
| public static String getTable(Class<?> clazz) { |
| Table table = clazz.getAnnotation(Table.class); |
| if(table != null) { |
| String name = table.name(); |
| if(name != null && name.trim().length() > 0) { |
| return name; |
| } |
| } |
| |
| return clazz.getSimpleName(); |
| } |
| |
| |
| |
| |
| public static List<String> getFields(Class<?> clazz) { |
| Field[] fields = clazz.getDeclaredFields(); |
| List<String> list = new ArrayList<String>(); |
| for(Field field : fields) { |
| |
| Cloumn cloumn = field.getAnnotation(Cloumn.class); |
| |
| if(cloumn != null) { |
| String value = cloumn.value(); |
| if(value != null && value.trim().length() > 0) { |
| list.add(value); |
| } |
| } else { |
| list.add(field.getName()); |
| } |
| } |
| return list; |
| } |
| } |
| |
拼接sql语句
| |
| |
| |
| public String getAll(Class<?> clazz) { |
| SQL sql = new SQL(); |
| List<String> fields = AnnotationUtil.getFields(clazz); |
| StringBuilder sb = new StringBuilder(); |
| fields.forEach(f -> sb.append(f + ",")); |
| String str = sb.substring(0, sb.length()-1); |
| sql.SELECT(str); |
| sql.FROM(AnnotationUtil.getTable(clazz)); |
| System.out.println("sql:" + sql.toString()); |
| return sql.toString(); |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术