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

  • 以下知识本人都是用 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.hqwc.cn/news/144960.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JVM 堆外内存查看方法

JVM 堆外内存查看方法 概述 是否曾经想过为什么Java应用程序通过众所周知的*-Xms和-Xmx调整标志消耗的内存比指定的数量大得多 &#xff1f;由于各种原因和可能的优化&#xff0c;JVM可能会分配额外的本机内存。这些额外的分配最终可能使消耗的内存超出-Xmx* 限制。在本教程中…

vue源码笔记之——响应系统

vue是一种声明式范式编程&#xff0c;使用vue者只需要告诉其想要什么结果&#xff0c;无需关心具体实现&#xff08;vue内部做了&#xff0c;底层是利用命令式范式&#xff09; 1. reactive为什么只能操作对象&#xff0c;对于基本数据类型&#xff0c;需要用ref&#xff1f; …

YOLOv8改进实战 | 更换主干网络Backbone之2023最新模型LSKNet,旋转目标检测SOTA

前言 传统的YOLOv8系列中,Backbone采用的是较为复杂的C2f网络结构,这使得模型计算量大幅度的增加,检测速度较慢,应用受限,在某些真实的应用场景如移动或者嵌入式设备,如此大而复杂的模型时难以被应用的。为了解决这个问题,本章节通过采用LSKNet轻量化主干网络作为Backb…

【技能树笔记】网络篇——练习题解析(八)

目录 前言 一、LAN技术 1.1 堆叠与集群 1.2 MSTP的特点 二、WAN技术 2.1 PPP链路建立 2.2 PPPoE 2.3 组播 2.3.1 组播的IP 2.3.2 组播分发树 2.3.3 组播协议 三、IPv6基础 3.1 IPv6地址 3.2 IPv6协议 3.3 IPv6过渡技术 总结 &#x1f308;嗨&#xff01;我是Filotimo__&#x1…

用python计算积分

先安装这个包 pip install scipy运行 import tkinter as tk from scipy.integrate import quad# 创建主窗口 root tk.Tk() root.title("积分计算器")# 定义计算积分的函数 def calculate_integral():# 获取用户输入的函数表达式function function_entry.get()# 获…

Android 10.0 Launcher3定制化之动态时钟图标功能实现

1.概述 在10.0的系统产品rom定制化开发中,在Launcher3中的定制化的一些功能中,对于一些产品要求需要实现动态时钟图标功能,这就需要先绘制时分秒时针表盘,然后 每秒刷新一次时钟图标,时钟需要做到实时更新,做到动态时钟的效果,接下来就来分析这个功能的实现 如图: 2.动…

基于指数分布优化的BP神经网络(分类应用) - 附代码

基于指数分布优化的BP神经网络&#xff08;分类应用&#xff09; - 附代码 文章目录 基于指数分布优化的BP神经网络&#xff08;分类应用&#xff09; - 附代码1.鸢尾花iris数据介绍2.数据集整理3.指数分布优化BP神经网络3.1 BP神经网络参数设置3.2 指数分布算法应用 4.测试结果…

canvas绘制刮涂层抽奖效果

实现的效果&#xff1a;主要用到画布设置图层覆盖效果globalCompositeOperation属性 实现的源代码&#xff1a; <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8" /><meta name"viewport" content"…

IDEA 新版本设置菜单展开

使用了新版本的IDEA 新UI后&#xff0c;常用的file&#xff0c;view&#xff0c;菜单看不见了&#xff0c;不太适应&#xff0c;找了一下&#xff0c;有个配置可以修改。 打开settings里面把show main menu in a separate toolbar勾选上&#xff0c;应用保存就可以了

冲刺学习-MySQL-常见问题

MySQL索引的最左原则 联合索引的说明 建立三个字段的联合索引联合索引&#xff08;a&#xff0c;b&#xff0c;c&#xff09;相当于建立了索引&#xff1a;&#xff08;a&#xff09;&#xff0c;&#xff08;a&#xff0c;b&#xff09;&#xff0c;&#xff08;a&#xff0…

如何在linux服务器上安装Anaconda与pytorch

如何在linux服务器上安装Anaconda与pytorch 1&#xff0c;安装anaconda1.1 下载anaconda安装包1.2 安装anaconda1.3 设计环境变量1.4 安装完成验证 2 Anaconda安装pytorch2.1 创建虚拟环境2.2 查看现存环境2.3 激活环境2.4 选择合适的pytorch版本下载2.5 检测是否安装成功&…

造车先做三蹦子220101--机器学习字符(字母、和数字识别)的“小白鼠”与“果蝇”

“0”数字字符零 的图片(16*16点阵)&#xff1a; #Letter23Digital23R231006d.pyimport torch import torch.nn as nn import torch.optim as optim #optimizer optim.SGD(model.parameters(), lr0.01) from PIL import Image from PIL import ImageDraw from PIL import Im…