使用反射拼接SQL语句 和 使用 反射 + 注解 拼接SQL语句

news/2024/5/19 2:26:59 标签: 反射, 注解, Properties, Class, ClassLoader
  • 以下知识本人都是用 Maven工程 总结的

 1、使用反射拼接SQL语句 

package com.csdn.anno;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;
public class AnnotationTest {
    public static void main(String[] args) throws ClassNotFoundException, IOException {
//        String sql = "select name,age,gender,city,score from student";
//        StringBuilder sql = new StringBuilder("select XXX,XXX,XXX");
        StringBuilder sql = new StringBuilder("select ");

        //通过 类加载器 读取文件
        InputStream inputStream = AnnotationTest.class.getClassLoader().getResourceAsStream("bean.properties");
        System.out.println(inputStream);//java.io.BufferedInputStream@7cca494b
        //读取properties配置文件,获取到配置文件里指定的类名
        Properties prop = new Properties();
        prop.load(AnnotationTest.class.getClassLoader().getResourceAsStream("bean.properties"));
        String className = (String) prop.get("className");
        //使用反射通过类名加载类
        Class<?> clz = Class.forName(className);
        String tableName = clz.getSimpleName().toLowerCase();

        Field[] fields = clz.getDeclaredFields();

        for (int i = 0; i < fields.length; i++) {
            //遍历字段,获取字段名,添加到SQL语句
            String name = fields[i].getName();
            sql.append(name);
            if (i!= fields.length-1) {
                sql.append(",");
            }
        }

        sql.append(" from ").append(tableName);

        System.out.println(sql);//select name,age,gender,city,score from student

    }
}
class Student {
    private String name;
    private int age;
    private String gender;
    private String city;
    private String score;
}
className=com.csdn.anno.Student

2、使用 反射 + 注解 拼接SQL语句 

package com.csdn.anno;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.util.Properties;
public class AnnotationTest {
    public static void main(String[] args) throws ClassNotFoundException, IOException {
//        String sql = "select name,age,gender,city,score from student";
//        StringBuilder sql = new StringBuilder("select XXX,XXX,XXX");
        StringBuilder sql = new StringBuilder("select ");

        //通过 类加载器 读取文件
        InputStream inputStream = AnnotationTest.class.getClassLoader().getResourceAsStream("bean.properties");
        System.out.println(inputStream);//java.io.BufferedInputStream@7cca494b
        //读取properties配置文件,获取到配置文件里指定的类名
        Properties prop = new Properties();
        prop.load(AnnotationTest.class.getClassLoader().getResourceAsStream("bean.properties"));
        String className = (String) prop.get("className");
        //使用反射通过类名加载类
        Class<?> clz = Class.forName(className);
        Table tableAnnotation = clz.getAnnotation(Table.class);  //获取到类上的注解
        String tableName = tableAnnotation != null ? tableAnnotation.value() : clz.getSimpleName().toLowerCase();

        //使用反射获取类里的字段
        Field[] fields = clz.getDeclaredFields();

        for (int i = 0; i < fields.length; i++) {
            //获取字段上的注解
            Column columnAnnotation = fields[i].getAnnotation(Column.class);

            //遍历字段,获取字段名,添加到SQL语句
            String name = columnAnnotation != null ? columnAnnotation.value() : fields[i].getName();
            sql.append(name);
            if (i != fields.length - 1) {
                sql.append(",");
            }
        }

        sql.append(" from ").append(tableName);

        System.out.println(sql);//select name,age,sex,city,score from t_student

    }
}
@Table("t_student")
class Student {

    private String name;
    private int age;
    @Column("sex")
    private String gender;
    private String city;
    private String score;
}
@Retention(RetentionPolicy.RUNTIME)
@interface Table {
    String value();
}

@Retention(RetentionPolicy.RUNTIME)
@interface Column {
    String value();
}

http://www.niftyadmin.cn/n/5122928.html

相关文章

实战排查内存泄漏

1.问题一个springboot应用内存占用过高 2. Jvisualvm排查 打开Jvisualvm后发现本地没有相应名称的进程 jps 查看进程pid为4308 根据进程pid在Jvisualvm中选择相应的进程 发现堆空间占用过高 安装插件GC插件 选择可用插件 如果没有插件的话检查最新版本 插件中心无法连…

Hadoop3.0大数据处理学习4(案例:数据清洗、数据指标统计、任务脚本封装、Sqoop导出Mysql)

案例需求分析 直播公司每日都会产生海量的直播数据&#xff0c;为了更好地服务主播与用户&#xff0c;提高直播质量与用户粘性&#xff0c;往往会对大量的数据进行分析与统计&#xff0c;从中挖掘商业价值&#xff0c;我们将通过一个实战案例&#xff0c;来使用Hadoop技术来实…

读高性能MySQL(第4版)笔记20_Performance Schema和其他

1. 线程 1.1. MySQL服务端是多线程软件。它的每个组件都使用线程 1.2. 每个线程至少有两个唯一标识符 1.2.1. 操作系统线程ID 1.2.2. MySQL内部线程ID 2. 对象类型 2.1. OBJECT_TYPE列 2.2. EVENT 2.3. FUNCTION 2.4. PROCEDURE 2.5. TABLE 2.6. TRIGGER 3. Perfor…

工作:三菱伺服驱动器连接参数及其电机钢性参数配置与调整

工作&#xff1a;三菱伺服驱动器参数及电机钢性参数配置与调整 一、三菱PLC与伺服驱动器连接参数的设置 1. 伺服配置 单个JET伺服从站链接侧占用点数:Rx/Ry占用64点、RWw/RWr占用32点 图中配置了22个JET伺服从站&#xff0c;占用点数:Rx/Ry占用64222048‬点、RWw/RWr占用322…

Flutter Android IOS 获取通讯录联系人列表

1.在pubspec.yaml 文件中添加 contacts_service 和 permission_handler 插件的依赖&#xff1a; dependencies:contacts_service: ^0.6.3 #获取联系人permission_handler: ^11.0.1 #权限请求2.在你的 Dart 代码中&#xff0c;导入 contacts_service 插件&#xff1a; impo…

网络安全(黑客技术)—自学

目录 一、自学网络安全学习的误区和陷阱 二、学习网络安全的一些前期准备 三、网络安全学习路线 四、学习资料的推荐 想自学网络安全&#xff08;黑客技术&#xff09;首先你得了解什么是网络安全&#xff01;什么是黑客&#xff01; 网络安全可以基于攻击和防御视角来分类&am…

Postman使用

Postman使用 1、创建工作空间2、创建collections3、添加请求4、添加案例5、发送 POST请求 接口测试用例方法 postman&#xff1a;支持调试和测试的工具 1、创建工作空间 2、创建collections 改名后回车即可 3、添加请求 4、添加案例 5、发送 POST请求 发送成功

超简洁ubuntu linux 安装 cp2k

文章目录 打开下载网址解压接下来的步骤讲解 将解压的包移到对应路径下最后运行 打开下载网址 需要从github下载&#xff1a;下载网址 两个都可以从windows下先下载&#xff0c;再复制到linux中&#xff0c; 如果不能复制&#xff0c;右键这两个&#xff0c;复制链接&#xf…