MyBatis-Flex(上)

简介: MyBatis-Flex

一款好的DAO层框架,往往会简化我们操作数据库的方式,开发人员在使用框架时直接调用封装好的API,还可以省去很多代码的编写工作,从而提高工作效率和开发速度。


小编过去已经接触并使用过了Mybatis-Plus和tk-Mapper,最近因为有些契机,想了解下比较轻量的MyBatis 增强框架,看了上面两个在非Spring下场景的用法,有点不太满意,这时候突然就看到了我们今天的主角Mybatis-Flex,仅仅依赖Mybatis的,它非常轻量、同时拥有极高的性能与灵活性。我们可以轻松的使用 Mybaits-Flex 链接任何数据库,其内置的 QueryWrapper亮点 帮助我们极大的减少了 SQL 编写的工作的同时,减少出错的可能性。


特征:


1、很轻量,整个框架只依赖 Mybatis 再无其他第三方依赖

2、只增强,支持 Entity 的增删改查、及分页查询,但不丢失 Mybatis 原有功能

3、Db + Row,可以无需实体类对数据库进行增删改查

4、支持多种数据库类型,自由通过方言持续扩展

5、支持联合主键,以及不同的主键内容生成策略

6、极其友好的 SQL 联动查询,IDE 自动提示不再担心出错

7、更多小惊喜(Mybatis-Plus和tk-Mapper有的功能他基本都有


对比:


接下来我们来写个小例(两张一模一样的账户表,使用H2数据库(一款内存数据库,就比如我们这中写demo的场景没必要真的去装一个数据库来建个表,使用H2数据库可以简化非重要步骤))来演示下Mybatis-Flex是多么的轻量(无spring)和易用,以及展示部分功能:


引入pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>mybatiesFlex</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.mybatis-flex</groupId>
            <artifactId>mybatis-flex-core</artifactId>
            <version>1.4.3</version>
        </dependency>
        <!--这个依赖只是为了使用 h2 数据源-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.25</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.199</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.26</version>
        </dependency>
    </dependencies>
</project>


初始化表结构及数据的脚本:data.sql

CREATE TABLE IF NOT EXISTS `tb_account`
(
    `id`        INTEGER PRIMARY KEY auto_increment,
    `user_name` VARCHAR(100),
    `age`       Integer,
    `birthday`  DATETIME,
    `options`   VARCHAR(1024)
);
CREATE TABLE IF NOT EXISTS `tb_account2`
(
    `id`        INTEGER PRIMARY KEY auto_increment,
    `user_name` VARCHAR(100),
    `age`       Integer,
    `birthday`  DATETIME,
    `options`   VARCHAR(1024)
);
schema.sql
CREATE TABLE IF NOT EXISTS `tb_account`
(
    `id`        INTEGER PRIMARY KEY auto_increment,
    `user_name` VARCHAR(100),
    `age`       Integer,
    `birthday`  DATETIME,
    `options`   VARCHAR(1024)
);
CREATE TABLE IF NOT EXISTS `tb_account2`
(
    `id`        INTEGER PRIMARY KEY auto_increment,
    `user_name` VARCHAR(100),
    `age`       Integer,
    `birthday`  DATETIME,
    `options`   VARCHAR(1024)
);
配置文件:mybatis-flex.config
processor.mappersGenerateEnable = false


表映射对象:Account

package com.mybatisflex.test;
import com.mybatisflex.annotation.*;
import com.mybatisflex.core.handler.Fastjson2TypeHandler;
import com.mybatisflex.core.mask.Masks;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Table(value = "tb_account",dataSource = "ds2")
public class Account {
    @Id(keyType = KeyType.Auto)
    private Long id;
    @ColumnMask(Masks.CHINESE_NAME)
    private String userName;
    private int age;
    private Date birthday;
    @Column(typeHandler = Fastjson2TypeHandler.class)
    private Map<String, Object> options;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public Map<String, Object> getOptions() {
        return options;
    }
    public void setOptions(Map<String, Object> options) {
        this.options = options;
    }
    public void addOption(String key, Object value) {
        if (options == null) {
            options = new HashMap<>();
        }
        options.put(key, value);
    }
    @Override
    public String toString() {
        return "{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", options=" + options +
                '}';
    }
}
Account2
package com.mybatisflex.test;
import com.mybatisflex.annotation.*;
import com.mybatisflex.core.handler.Fastjson2TypeHandler;
import com.mybatisflex.core.mask.Masks;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Table(value = "tb_account2",dataSource = "ds2")
public class Account2 {
    @Id(keyType = KeyType.Auto)
    private Long id;
    @ColumnMask(Masks.CHINESE_NAME)
    private String userName;
    private int age;
    private Date birthday;
    @Column(typeHandler = Fastjson2TypeHandler.class)
    private Map<String, Object> options;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public Map<String, Object> getOptions() {
        return options;
    }
    public void setOptions(Map<String, Object> options) {
        this.options = options;
    }
    public void addOption(String key, Object value) {
        if (options == null) {
            options = new HashMap<>();
        }
        options.put(key, value);
    }
    @Override
    public String toString() {
        return "{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", options=" + options +
                '}';
    }
}



例子用到的非表对象Account3:

package com.mybatisflex.test;
public class Account3 {
    private String userName3;
    private int age;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getUserName3() {
        return userName3;
    }
    public void setUserName3(String userName3) {
        this.userName3 = userName3;
    }
    @Override
    public String toString() {
        return "Account3{" +
                "userName3='" + userName3 + '\'' +
                ", age=" + age +
                '}';
    }
}


表对应的mapper:AccountMapper

package com.mybatisflex.test;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface AccountMapper extends BaseMapper<Account> {
    @Select("select * from tb_account where id = #{id}")
    Account selectById(@Param("id") Integer id);
}
Account2Mapper
package com.mybatisflex.test;
import com.mybatisflex.core.BaseMapper;
public interface Account2Mapper extends BaseMapper<Account2>
相关文章
|
3月前
|
SQL Java 数据库连接
|
11月前
|
XML Java 数据库连接
|
23小时前
|
SQL XML Java
Mybatis02(二)
Mybatis02(二)
5 0
|
23小时前
|
SQL Java 数据库连接
Mybatis02(一)
Mybatis02(一)
10 0
|
5天前
|
SQL Java 数据库连接
MyBatis常用的小技巧
这篇文章分享了MyBatis中的一些常用技巧,包括使用`in`语句处理字符串或集合参数、在`insert`语句后获取自增ID,以及使用`like concat`进行模糊查询以防止SQL注入。
5 0
|
3月前
|
SQL 缓存 Java
mybatis使用总结
mybatis使用总结
|
3月前
|
SQL 缓存 Java
|
8月前
|
Java 数据库连接 数据库
Mybatis及Mybatis-Plus使用
Mybatis及Mybatis-Plus使用
706 2
Mybatis及Mybatis-Plus使用
|
8月前
|
SQL 安全 Java
Mybatis中# 和 $ 的使用详解
Mybatis中# 和 $ 的使用详解
127 0
|
10月前
|
SQL Java 数据库连接
14MyBatis - MyBatis介绍
14MyBatis - MyBatis介绍
32 0