Git Product home page Git Product logo

mybatis's Introduction

项目简介

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.0.16</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 版本

后期 road-map

  • 连接池管理

  • TX 管理

  • 数据库厂商标识(databaseIdProvider)

  • 添加 MBG

  • 添加 spring 整合实现

  • 添加 spring-boot 整合实现

mybatis's People

Contributors

houbb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.