houbb / mybatis

The simple mybatis.(手写简易版 mybatis)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

项目简介

mybatis 是一款简化版的 mybatis 实现。

Maven Central Build Status Open Source Love

创作目的

  • 学习 mybatis 的原理

  • 便于拓展自己的数据库工具

快速开始

需要

  • jdk 1.7+

  • maven 3.x+

maven 引入

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>mybatis</artifactId>
    <version>0.1.0</version>
</dependency>

准备工作

  • sql 建表

在 test 数据库执行下面的建表语句。

-- auto-generated definition
use test;
create table user
(
  id   int auto_increment
    primary key comment '唯一主键',
  name varchar(100) not null comment '姓名',
  password varchar(100) not null comment '密码',
  create_time char(17) comment '创建时间'
) CHARACTER SET utf8 COLLATE utf8_general_ci;

-- init
insert into user (name, password) value ('luna', '123456');
  • 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <dataSource>
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </dataSource>

    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>

    <plugins>
        <plugin interceptor="com.github.houbb.mybatis.plugin.SimpleLogInterceptor"/>
    </plugins>

    <typeHandlers>
        <typeHandler javaType="java.util.Date" handler="com.github.houbb.mybatis.typehandler.DateTypeHandler"/>
    </typeHandlers>

</configuration>

备注:默认使用的是 mysql 5.7,如果为 8.0+,需要自行引入 jar。

运行测试代码

public static void main(String[] args) {
    Config config = new XmlConfig("mybatis-config-5-7.xml");

    SqlSession sqlSession = new DefaultSessionFactory(config).openSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

    User user = userMapper.selectById(1L);
    System.out.println(user);
}
  • 输出
User{id=1, name='ryo', password='123456', createTime=Wed Jul 01 22:03:01 CST 2020}

拓展阅读

从零开始手写 mybatis(一)MVP 版本

手写 mybatis 系列(二)mybatis interceptor 插件机制详解

从零开始手写 mybatis (三)jdbc pool 从零实现数据库连接池

后期 road-map

  • 日志组合

  • 连接池管理

  • TX 管理

- [ ] 数据库厂商标识(databaseIdProvider)

  • api 方法配置全接口,便于直接配置使用。

  • 添加 MBG

  • 添加 spring 整合实现

  • 添加 spring-boot 整合实现

  • 自增强-类似 mybatis-plus 模块。让组件使用起来更加方便

保持核心功能的简单,保证使用的强大便捷。

About

The simple mybatis.(手写简易版 mybatis)

License:Other


Languages

Language:Java 97.3%Language:Shell 1.9%Language:Batchfile 0.8%