手写ORM框架

掌握:JDBC+反射+注解+泛型

(1)创建Maven工程

(2)添加pom.xml

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.27</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.4</version>
    </dependency>
</dependencies>

(3)创建属性文件

# 定义数据库连接的信息。
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/springcloud?serverTimezone=Asia/Shanghai
username=root
password=root
maxActive=10
initialSize=5
maxWait=3000

(4)创建一个链接数据库的工具类

package com.zd.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class ConnectionUtil {
    //声明一个数据源对象
    private static DataSource dataSource;
    //为数据源赋值
    static {
        try {
            //通过类加载器加载db.properties形成一个字节输出流
            InputStream resource = ConnectionUtil.class.getClassLoader().getResourceAsStream("db.properties");
            //创建一个属性文件对象
            Properties properties = new Properties();
            //输出流加载到文件对象中
            properties.load(resource);
            //得到数据源=通过druid工程创建数据源对象
            dataSource= DruidDataSourceFactory.createDataSource(properties);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //获取连接对象
    public static Connection getConnection() throws Exception{
        Connection connection = dataSource.getConnection();
        return connection;
    }

    //关闭所有资源
    public static void closeAll(ResultSet rs, PreparedStatement ps, Connection connection){
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

(5)创建annotation

@Target(ElementType.TYPE)//作用在类上
@Retention(RetentionPolicy.RUNTIME)//运行时生效
public @interface TableName {
    String value();
}
@Target(ElementType.FIELD)//作用在属性上
@Retention(RetentionPolicy.RUNTIME)//运行时生效
public @interface TableId {
    String value() default "id";//默认为id
}
public @interface TableField {
    String value();
}

(6)实体类

@TableName(value = "tb_order")
public class Order {
    @TableId(value = "oid")
    private Integer oid;
    private Integer uid;
    private String username;
    private Integer  pid;
    private String pname;
    private double pprice;
    private Integer number;
}

@TableName(value = "tb_product")
public class Product {
    @TableId(value = "pid")
    private Integer pid;

    private String pname;
    private double pprice;
    private Integer stock;

    @Override
    public String toString() {
        return "Product{" +
                "pid=" + pid +
                ", pname='" + pname + '\'' +
                ", pprice=" + pprice +
                ", stock=" + stock +
                '}';
    }

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public double getPprice() {
        return pprice;
    }

    public void setPprice(double pprice) {
        this.pprice = pprice;
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }
}

(7)dao层

public class OrderDao extends BaseDao<Order> {
}
public class ProductDao extends BaseDao<Product> {
}

(8)BaseDao类

package com.zd.util;

import com.zd.annotation.TableId;
import com.zd.annotation.TableName;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

public class BaseDao<T> {
    public Connection connection;
    public PreparedStatement ps;
    public Class<?> aClass;//实体类反射类对象
    public BaseDao(){
        //获取子类Dao的反射类对象
        Class<? extends BaseDao> clazz = this.getClass();
        //获取父类对象以及父类对象的泛型
        ParameterizedType genericSuperclass = (ParameterizedType) clazz.getGenericSuperclass();
        //获取子类中的泛型对象
        Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
        //第一个属性
        aClass= (Class<?>) actualTypeArguments[0];
    }
    //添加功能:所有表的添加功能.sql="insert into 表名(字段1,字段2.。。) values(值1,值2.。。。)"
    public int insert(T t){
        try {
            //声明一个字符串表示sql语句
            StringBuffer buffer = new StringBuffer("insert into ");
            //获取表名
            Class<?> aClass = t.getClass();//获取T的反射类对象
            TableName tableNameAnnotation = aClass.getAnnotation(TableName.class);//获取注解类上的注解对象
            //获取表名
            String tableNameValue = tableNameAnnotation.value();//获取注解类上的value值
            //把获取的表名拼接到sql上
            buffer.append(tableNameValue);
            //声明两个集合  分配存储列名  列值
            List<String> columns = new ArrayList<>();
            List<Object> values = new ArrayList<>();
            //获取添加的列名
            Field[] declaredFields = aClass.getDeclaredFields();//获取反射类中所有的属性对象
            for (Field field:declaredFields){
                //开启属性可见性
                field.setAccessible(true);
                //判断表的主键是否为null
                if (field.getAnnotation(TableId.class)==null){
                    //得到列名
                    String name = field.getName();
                    //得到值
                    Object o = field.get(t);
                    columns.add(name);//将值添加到集合列名
                    values.add("'"+ o +"'");//将值添加到集合值里
                }
            }
            buffer.append(columns.toString().replace("[","(").replace("]",")"));
            buffer.append(" values ");
            buffer.append(values.toString().replace("[","(").replace("]",")"));
            connection=ConnectionUtil.getConnection();
            ps=connection.prepareStatement(buffer.toString());//转换成String类型
            int i = ps.executeUpdate();
            return i;

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeAll(null,ps,connection);
        }
        return 0;
    }

    //修改功能:sql="update  表名  set  字段名=’值‘,字段名=’值‘。。。。 where  主键=值"
    public int update(T t){
        try {
            //创建一个字符串对象
            StringBuffer buffer = new StringBuffer("update ");
            //获取表名
            Class<?> aClass = t.getClass();//通过T 的反射类对象
            String tableName = aClass.getAnnotation(TableName.class).value();//获取注解类里的value的值
            buffer.append(tableName+" set ");
            //获取所有的Field对象
            Field[] fields = aClass.getDeclaredFields();
            String where =" where ";
            for (Field field:fields){
                //开启属性的可见性
                field.setAccessible(true);
                TableId tableIdAnnotation = field.getAnnotation(TableId.class);//得到主键id
                String name = field.getName();//得到列名
                Object o = field.get(t);//得到值
                if (tableIdAnnotation==null){//判断主键id是否等于null
                    buffer.append(name+"='"+o+"',");
                }else {
                    where+=name+"="+o;
                }
            }
            String sql=buffer.substring(0,buffer.lastIndexOf(","))+ where;
            connection=ConnectionUtil.getConnection();
            ps=connection.prepareStatement(sql);
            int i = ps.executeUpdate();
            return i;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeAll(null,ps,connection);
        }
        return 0;
    }

    //删除: sel=“delete from 表名  where id=”;
    public int delete(Object id){
        try {
            StringBuffer buffer = new StringBuffer("delete from ");
            //获取实体类的反射类对象
            String value = aClass.getAnnotation(TableName.class).value();
            buffer.append(value+ " where ");
            //获取主键
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field field:declaredFields){
                //开启私有属性可见性
                field.setAccessible(true);
                TableId annotation = field.getAnnotation(TableId.class);
                if (annotation!=null){
                    String s = annotation.value();//获取列名
                    buffer.append(s+"="+id);//拼接id
                    break;
                }
            }
            connection=ConnectionUtil.getConnection();
            ps=connection.prepareStatement(buffer.toString());//转换成字符串
            int i = ps.executeUpdate();
            return i;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ConnectionUtil.closeAll(null,ps,connection);
        }
        return 0;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值