利用jdbc,泛型,注解,反射设计sql单表,查询全部和id查询

对上次的写的框架进行一下修改,上次删除根据id需要建一个对象在进行添加id,这次简化了一下,不再需再建对象,直接再方法写个id参数就行了;

1.准备工具类

添加mysql驱动依赖

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.31</version>
</dependency>
<dependency>

 Dbutils工具类

package com.linghua.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DbUtils {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;



    static {
        //1.使用property类 读取文件
        Properties properties = new Properties();

        //2.加载属性文件
        InputStream systemResourceAsStream = ClassLoader.getSystemResourceAsStream("jdbc.properties");

        try {
            //读取对应文件属性
            properties.load(systemResourceAsStream);
            driver=properties.getProperty("driver");
            url=properties.getProperty("url");
            username=properties.getProperty("username");
            password=properties.getProperty("password");
        } catch (IOException e) {
            throw new RuntimeException("jdbc.properties没找到");
        }

    }


    public static Connection getcomparable() throws Exception {

        Class.forName(driver);

        Connection conn = DriverManager.getConnection(url, username, password);

        return conn;
    }

    public void close(ResultSet set, Connection con, PreparedStatement pre){
        try {
            if(set !=null){
                set.close();
            }
            if(con!=null){
                con.close();
            }
            if(pre!=null){
                pre.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

2.查询

2.1根据id查询

public class BaseDao<T> {
        private Class student;
    //获取泛型中实体的反射类
    public BaseDao(){
        //this 表示类dao对象
        //获取子类dao的反射类
        Class<? extends BaseDao> aClass = this.getClass();
        //获取父类的反射类以及泛型的信息
        ParameterizedType genericSuperclass = (ParameterizedType) aClass.getGenericSuperclass();
        //得到泛型的反射类
        Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
        student=(Class) actualTypeArguments[0];

        System.out.println(this);
        System.out.println(aClass);
        System.out.println(genericSuperclass);
        System.out.println(actualTypeArguments);

    }

    public T selectById(Object id) {
        StringBuffer ById = new StringBuffer("select * from ");
        //拿到类名
        String simpleName = student.getSimpleName();
        //获取类名注解值
        On annotation = (On) student.getAnnotation(On.class);
        if(annotation!=null){
            simpleName= annotation.value();
        }
        ById.append(simpleName+" where ");
        //获取 属性名id
        Field[] declaredFields = student.getDeclaredFields();
        for (Field field:declaredFields){
            Sid annotation1 = (Sid) field.getAnnotation(Sid.class);
            if(annotation1!=null){
                String value = annotation1.value();
                ById.append(value+" = "+"'"+id+"'");
                break;
            }
        }
        try {
            Connection getcomparable = DbUtils.getcomparable();
            PreparedStatement statement = getcomparable.prepareStatement(ById.toString());
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()){
                //把resulset 里的内容封装到 对应对象里
                //通过实体类的反射类 获取实体类对象
                T t = (T)student.newInstance();
                for (Field field:declaredFields){
                    field.setAccessible(true);
                    //获取属性名当作列名
                    String name = field.getName();
                    Sid annotation1 = field.getAnnotation(Sid.class);
                    if(annotation1!=null){
                        name=annotation1.value();
                    }
                    In annotation2 = field.getAnnotation(In.class);
                    if(annotation2!=null){
                        name=annotation2.value();
                    }
                    Object object = resultSet.getObject(name);

                    field.set(t,object);

                }
                return t;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }



        return null;
    }
}

2.2查询全部

public class BaseDao<T> {
        private Class student;
    //获取泛型中实体的反射类
    public BaseDao(){
        //this 表示类dao对象
        //获取子类dao的反射类
        Class<? extends BaseDao> aClass = this.getClass();
        //获取父类的反射类以及泛型的信息
        ParameterizedType genericSuperclass = (ParameterizedType) aClass.getGenericSuperclass();
        //得到泛型的反射类
        Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
        student=(Class) actualTypeArguments[0];

        System.out.println(this);
        System.out.println(aClass);
        System.out.println(genericSuperclass);
        System.out.println(actualTypeArguments);

    }

    public List<T> selectAll(){
        StringBuffer All = new StringBuffer("select * from ");
        //获取类名
        String simpleName = student.getSimpleName();
        //获取注解
        On annotation = (On) student.getAnnotation(On.class);
        if(annotation!=null){
            //得到注解名
            simpleName= annotation.value();
        }
        All.append(simpleName);

        List<T> list = new ArrayList<T>();
        //获取属性名
        Field[] declaredFields = student.getDeclaredFields();
        try {
            Connection getcomparable = DbUtils.getcomparable();
            PreparedStatement statement = getcomparable.prepareStatement(All.toString());
            ResultSet resultSet = statement.executeQuery();



            // 把resultset 的内容封装到对应对象
            while (resultSet.next()){
                //反射得到类对象
                T t = (T)student.newInstance();
                //给对象t 进行赋值
                for (Field field:declaredFields){
                    field.setAccessible(true);
                    String name = field.getName();

                    Sid annotation1 = field.getAnnotation(Sid.class);
                    if(annotation1!=null){
                        name=annotation1.value();
                    }
                    In annotation2 = field.getAnnotation(In.class);
                    if(annotation2!=null){
                        name=annotation2.value();
                    }
                    Object object = resultSet.getObject(name);

                    field.set(t,object);
                }

                    list.add(t);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值