古道长亭

Contact me with ixiaoqiang0011@gmail.com


  • 首页

  • 归档

  • 分类

  • 关于

  • Book

  • 搜索

手写mybatis框架

时间: 2022-08-02   |   分类: mybatis   | 字数: 63 字 | 阅读约: 1分钟

一、思路

  1. 解析sqlMapConfig.xml
  2. 解析sql Xml配置文件

二、代码

代码详见mybatis-tec

  • custom-batis: 核心代码
  • custom-batis-test: 测试代码

JDBC回顾

时间: 2022-08-01   |   分类: mybatis   | 字数: 588 字 | 阅读约: 2分钟

JDBC

一、依赖包

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

二、jdbc问题

  1. 数据库连接创建、释放频繁造成系统资源浪费,从而影响系统性能
  2. Sql语句在代码中硬编码,造成代码不易维护,实际应用中sql变化的可能较大,sql变动需要改变java代码
  3. 使用preparedStatement向占有位符号传参数存在硬编码,因为sql语句的where条件不一定,可能多也可能少,修改sql还要修改代码,系统不易维护
  4. 对结果集解析存在硬编码(查询列名),sql变化导致解析代码变化,系统不易维护,如果能将数据 库记录封装成pojo对象解析比较方便

三、示例代码

/**
 * jdbc demo
 *
 * @author zhaojianqiang
 */
public class JDBCDemo {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 通过驱动管理类获取数据库链接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tec-mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "123456");
            // 定义sql语句?表示占位符
            String sql = "select * from user where name = ?";
            // 获取预处理statement
            preparedStatement = connection.prepareStatement(sql);
            // 设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值r
            preparedStatement.setString(1, "aaa");
            // 向数据库发出sql执行查询,查询出结果集
            resultSet = preparedStatement.executeQuery();
            // 遍历查询结果集
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                // 封装User
                System.out.println("用户id:" + id);
                System.out.println("用户名称:" + name);
            }
        } catch (
            Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

自定义rpc框架

时间: 2022-07-26   |   分类: RPC框架   netty   | 字数: 2029 字 | 阅读约: 5分钟

自定义rpc框架

1.分布式架构网络通信

在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现 远程通讯的技术,例如:RMI、Hessian、SOAP、ESB和JMS等,它们背后到底是基于什么原理实现的呢

阅读全文 »

Netty源码剖析

时间: 2022-07-25   |   分类: RPC框架   netty   | 字数: 619 字 | 阅读约: 2分钟

Netty源码剖析

1.源码构建

https://github.com/netty/netty 源码地址

源码包 example里有常用示例代码。

2.EventLoopGroup事件循环组(线程组)源码

EventLoopGroup 是一组 EventLoop 的抽象,Netty 为了更好的利用多核 CPU 资源,一般会有多个 EventLoop 同时工作,每个 EventLoop 维护着一个 Selector 实例。

阅读全文 »

Netty高级应用

时间: 2022-07-24   |   分类: RPC框架   netty   | 字数: 3676 字 | 阅读约: 8分钟

Netty高级应用

1.netty编解码器

1.1 java的编解码

  • 编码(Encode)称为序列化, 它将对象序列化为字节数组,用于网络传输、数据持久化或者其它用途。

  • 解码(Decode)称为反序列化,它把从网络、磁盘等读取的字节数组还原成原始对象(通常是原始对象的拷贝),以方便后续的业务逻辑操作。

阅读全文 »
35 36 37 38 39 40 41 42 43
古道长亭

古道长亭

Always remember that your present situation is not your final destination. The best is yet to come.

226 日志
57 分类
104 标签
GitHub Gitee
友情链接
  • 古道长亭的BOOK
  • JAVA学习
标签云
  • Mysql
  • 搜索引擎
  • Mybatis
  • 容器
  • 架构
  • 消息队列
  • Flink
  • Sharding sphere
  • 流处理
  • 缓存
© 2019 - 2024 京ICP备19012088号-1
0%