自定义ORM(mybatis)源码(六)-类型处理器

自定义ORM(mybatis)源码(六)-类型处理器

模仿mybatis

用于处理 sql 设置参数类型和 执行 sql 后响应字段的类型处理

TypeHandler

public interface TypeHandler<T> {
   

    /**
     * sql 设置参数值
     * @param pstmt
     * @param i
     * @param value
     * @throws SQLException
     */
    void setParameter(PreparedStatement pstmt, int i, T value) throws SQLException;

    /**
     * sql 执行结果获取字段
     * @param rs
     * @param columnName
     * @return
     * @throws SQLException
     */
    T getResult(ResultSet rs, String columnName) throws SQLException;
}

TypeHandlerRegister

类型注册

public class TypeHandlerRegister {
   

    private static Map<Class<?>, TypeHandler<?>> handlerMaps = new ConcurrentHashMap<>();


    static {
   
        handlerMaps.put(Integer.class, new IntegerTypeHandler());
        handlerMaps.put(String.class, new StringTypeHandler());
        handlerMaps.put(Long.class,new LongTypeHandler());
    }


    /**
     * 根据 类型获取 类型处理器
     * @param claz
     * @return
     * @param <T>
     */
    public static <T> TypeHandler<T> getHandler(Class<T> claz){
   
        return (TypeHandler<T>) handlerMaps.get(claz);
    }
}

现有:

  • IntegerTypeHandler
  • LongTypeHandler
  • StringTypeHandler

如 LongTypeHandler:

public class LongTypeHandler implements TypeHandler<Long> {
   


    @Override
    public void setParameter(PreparedStatement pstmt, int i, Long value) throws SQLException {
   
        pstmt.setLong(i, value);
    }

    @Override
    public Long getResult(ResultSet rs, String columnName) throws SQLException {
   
        return rs.getLong(columnName);
    }
}

相关推荐

  1. 定义ORM(mybatis)()-类型处理器

    2023-12-21 08:34:04       58 阅读
  2. 定义ORM(mybatis)(二)-解析mapper.xml

    2023-12-21 08:34:04       59 阅读
  3. 定义ORM(mybatis)(一)-解析config.xml

    2023-12-21 08:34:04       54 阅读
  4. 在FFmpeg下增加定义程序

    2023-12-21 08:34:04       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-21 08:34:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-21 08:34:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-21 08:34:04       87 阅读
  4. Python语言-面向对象

    2023-12-21 08:34:04       96 阅读

热门阅读

  1. http通信 axios VS fetch

    2023-12-21 08:34:04       60 阅读
  2. Linux下新建用户,并进行授权

    2023-12-21 08:34:04       57 阅读
  3. 部署nginx虚拟主机及SSL虚拟主机

    2023-12-21 08:34:04       56 阅读
  4. CSS新手入门笔记整理:CSS3颜色样式

    2023-12-21 08:34:04       53 阅读
  5. ubuntu18 安装 cmake v3.26.1

    2023-12-21 08:34:04       56 阅读