当前位置:网站首页>注解式编程小记
注解式编程小记
2022-08-11 05:13:00 【SunForYou】
自定义注解@Column
package com.hzcominfo.hermes_study;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String value();
}
自定义注解@Table
package com.hzcominfo.hermes_study;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
}
测试用实体类
package com.hzcominfo.hermes_study;
@Table(value = "student")
public class Student {
@Column(value = "name")
private String name;
@Column(value = "age")
private int age;
@Column(value = "xh")
private String xh;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
}
测试用main方法
public static void main(String[] args) {
Student s = new Student();
s.setAge(18);
s.setName("xx");
System.out.println(querySql(s));
}
public static String querySql(Object o) {
StringBuffer sb = new StringBuffer();
Class<? extends Object> clazz = o.getClass();
if(clazz.isAnnotationPresent(Table.class)) {
Table t = clazz.getAnnotation(Table.class);
sb.append("select * from " +t.value() +" where 1=1 ");
for (Field field : clazz.getDeclaredFields()) {
if(field.isAnnotationPresent(Column.class)) {
Column c = field.getAnnotation(Column.class);
String fieldName = c.value();
String method = "get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);
try {
Method m = clazz.getDeclaredMethod(method);
Object fieldValue = m.invoke(o);
if(fieldValue == null) continue;
sb.append(" and "+fieldName+" = ");
if(fieldValue instanceof String) {
sb.append("'"+fieldValue+"'");
}else {
sb.append(fieldValue);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return sb == null ? null : sb.toString();
}
总结 1注解式编程方便且b格高
2 可代替文件配置,可以参考https://www.cnblogs.com/everxs/p/4575131.html(侵权即删)
边栏推荐
- Thymeleaf
- nodes服务器
- [ARM] rk3399 mounts nfs error
- C language antithesis: who is the murderer!
- 2022 coal mine gas inspection test, simulation test question and answer
- [Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library
- guava RateLimiter均匀限流
- 代码在线审查(添加网页批注)的实现
- Tips to make your code more and more taller and taller - code specification, you have to know
- C statement: data storage
猜你喜欢

postman脚本的应用

MySQL must know and must know (primary articles)

prometheus:(二)监控概述(你永远逃不出我的手掌哈哈)

普林斯顿微积分读本05第四章--求解多项式的极限问题

Switches and routers technology - 26 - configure OSPF peripheral area
![[Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module](/img/7b/e265305df01eb405a131d0de2154d3.png)
[Embedded open source library] The use of MultiButton, an easy-to-use event-driven button driver module

C statement: data storage

Paper Notes: BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition

Idea essential skills to improve work efficiency

每周推荐短视频:你常用的拍立淘,它的前身原来是这样的!
随机推荐
Unity WebGL RuntimeError: integer overflow
3 Module 2: Use of scientific research tools
提升你工作效率的技巧,你得知道——Navitcat 快捷键
CentOS7静默安装Oracle11g_转载
guava RateLimiter uniform current limit
MFC 进程间通信(共享内存)
C语言题解:谁是凶手!
4 Module 3: Literature Reading and Research Methods
Switch and Router Technology - 36-Port Mirroring
Day38 LeetCode
关于ie下href有中文出现RFC 7230 and RFC 3986问题的研究
Switch and Router Technology-35-NAT to PAT
nodes服务器
CentOS卸载Oracle 11gR2(x64)_转载
[QNX Hypervisor 2.2 User Manual] 10.16 vdev virtio-blk
四大函数式接口
输入字符串,替换其中敏感词进行输出
Sub-database sub-table ShardingSphere-JDBC notes arrangement
Switch and Router Technology - 32 - Named ACL
Unity WebGL RuntimeError: integer overflow(整数溢出问题)