当前位置:网站首页>注解式编程小记
注解式编程小记
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(侵权即删)
边栏推荐
猜你喜欢

Network Skill Tree

玩转mysql之查看mysql版本号

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

Weekly recommended short video: your commonly used Polaroid, its predecessor turned out to be like this!

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

并发编程之线程基础

IDEA模板总结

Configure checkstyle in IDEA

阿里云无法远程连接数据库MySQL错误码10060解决办法_转载

BGP综合实验
随机推荐
C statement: data storage
Four functional interfaces
2022建筑焊工(建筑特殊工种)考题及模拟考试
Paper Notes: BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition
HAVE FUN | “SOFA 星球”飞船计划、源码解析活动最新进展
FPGA engineer interview questions collection 111~120
2.2 user manual] [QNX Hypervisor 10.15 vdev timer8254
Trilium使用总结
[ARM] rk3399 mounts nfs error
面试宝典一: code题目记录
Tips to improve your productivity, you have to know - Navitcat shortcuts
Thymeleaf
将double类型的数据转为字符串
How IP-Guard prohibits running U disk programs
2022 coal mine gas inspection test, simulation test question and answer
Prometheus :(一)基本概念
@Resource和@Autowired的区别
MySQL必知必会(初级篇)
BGP Comprehensive Experiment
【Mysql】----基础练习