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

编程入门 行业动态 更新时间:2024-10-26 14:37:22

使用<a href=https://www.elefans.com/category/jswz/34/1765951.html style=反射拼接SQL语句 和 使用 反射 + 注解 拼接SQL语句"/>

使用反射拼接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();
}

更多推荐

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

本文发布于:2023-12-05 06:27:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1663384.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   语句   注解   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!