项目作者: houbb

项目描述 :
The simple mybatis.(手写简易版 mybatis)
高级语言: Java
项目地址: git://github.com/houbb/mybatis.git
创建时间: 2020-06-30T09:49:51Z
项目社区:https://github.com/houbb/mybatis

开源协议:Other

下载


项目简介

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

Maven Central
Build Status

Open Source Love

创作目的

  • 学习 mybatis 的原理

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

快速开始

需要

  • jdk 1.7+

  • maven 3.x+

maven 引入

  1. <dependency>
  2. <groupId>com.github.houbb</groupId>
  3. <artifactId>mybatis</artifactId>
  4. <version>0.1.0</version>
  5. </dependency>

准备工作

  • sql 建表

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

  1. -- auto-generated definition
  2. use test;
  3. create table user
  4. (
  5. id int auto_increment
  6. primary key comment '唯一主键',
  7. name varchar(100) not null comment '姓名',
  8. password varchar(100) not null comment '密码',
  9. create_time char(17) comment '创建时间'
  10. ) CHARACTER SET utf8 COLLATE utf8_general_ci;
  11. -- init
  12. insert into user (name, password) value ('luna', '123456');
  • 配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <dataSource>
  4. <property name="driver" value="com.mysql.jdbc.Driver"></property>
  5. <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
  6. <property name="username" value="root"></property>
  7. <property name="password" value="123456"></property>
  8. </dataSource>
  9. <mappers>
  10. <mapper resource="mapper/UserMapper.xml"></mapper>
  11. </mappers>
  12. <plugins>
  13. <plugin interceptor="com.github.houbb.mybatis.plugin.SimpleLogInterceptor"></plugin>
  14. </plugins>
  15. <typeHandlers>
  16. <typeHandler javaType="java.util.Date" handler="com.github.houbb.mybatis.typehandler.DateTypeHandler"></typeHandler>
  17. </typeHandlers>
  18. </configuration>

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

运行测试代码

  1. public static void main(String[] args) {
  2. Config config = new XmlConfig("mybatis-config-5-7.xml");
  3. SqlSession sqlSession = new DefaultSessionFactory(config).openSession();
  4. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  5. User user = userMapper.selectById(1L);
  6. System.out.println(user);
  7. }
  • 输出
  1. 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

  • [x] 日志组合

  • [x] 连接池管理

  • [x] TX 管理

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

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

  • [ ] 添加 MBG

  • [ ] 添加 spring 整合实现

  • [ ] 添加 spring-boot 整合实现

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

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