Fluent Mybatis

官方文档:https://gitee.com/fluent-mybatis/fluent-mybatis/wikis

新的ORM框架,整个设计理念非常符合工程师思维。

  1. Fluent Mybatis 介绍

何为 Fluent Mybatis?

Fluent Mybatis, 是一款 Mybatis 语法增强框架, 综合了 Mybatis Plus, Dynamic SQL,JPA等框架特性和优点, 利用 annotation processor 生成代码。

Fluent Mybatis 有什么亮点?

使用 Fluent Mybatis 可以不用写具体的 XML 文件,通过 Java API 可以构造出比较复杂的业务 SQL 语句,做到代码逻辑和 SQL 逻辑的合一。不再需要在 Dao 中组装查询或更新操作,在 XML 或 Mapper 中再组装参数。

项目地址:https://gitee.com/fluent-mybatis/fluent-mybatis

  1. Fluent Mybatis 特性一览

  1. 各种实现方式对比
  2. Fluent Mybatis

我们可以看到 fluent api 的能力,以及 IDE 对代码的渲染效果。

  1. Mybatis

直接使用 Mybatis,实现步骤还是相当的繁琐,效率太低

  1. Mybatis Plus

Mybatis Plus 实现用到了比较多字符串的硬编码。字符串的硬编码,会给开发同学造成不小的使用门槛,个人觉的主要有 2 点:

  1. 字段名称的记忆和敲码困难
  2. Entity 属性跟随数据库字段发生变更后的运行时错误
  3. Fluent Mybatis 实战
  4. 引入依赖

新建 Maven 工程,设置项目编译级别为 Java8 及以上,引入 Fluent Mybatis 依赖包。

<dependencies>
    <!-- 引入fluent-mybatis 运行依赖包, scope为compile -->
    <dependency>
        <groupId>com.github.atool</groupId>
        <artifactId>fluent-mybatis</artifactId>
        <version>1.9.3</version>
    </dependency>
    <!-- 引入fluent-mybatis-processor, scope设置为provider 编译需要,运行时不需要 -->
    <dependency>
        <groupId>com.github.atool</groupId>
        <artifactId>fluent-mybatis-processor</artifactId>
        <version>1.9.3</version>
    </dependency>
</dependencies>

  1. 创建表
create schema fluent_mybatis;

create table hello_world
(
    id  bigint unsigned auto_increment primary key,
    say_hello    varchar(100) null,
    your_name    varchar(100) null,
    gmt_created   datetime   DEFAULTNULLCOMMENT'创建时间',
    gmt_modified datetime   DEFAULTNULLCOMMENT'更新时间',
    is_deleted   tinyint(2) DEFAULT0COMMENT'是否逻辑删除'
) ENGINE = InnoDB
  CHARACTERSET = utf8 comment'简单演示表';

  1. 创建数据库表对应的 Entity 类

创建数据库表对应的 Entity 类: HelloWorldEntity

  1. 根据驼峰命名规则命名 Entity 类和字段
  2. HelloWorldEntity 继承 IEntity 接口类
  3. 在 HelloWorldEntity 类上加注解 @FluentMybatis
@FluentMybatis
public class HelloWorldEntity extends RichEntity{
    private Long id;

    private String sayHello;

    private String yourName;

    private Date gmtCreated;

    private Date gmtModified;

    private Boolean isDeleted;

    // get, set, toString 方法

   @Override
   public Class<? extends IEntity> entityClass() {
      return HelloWorldEntity.class;
   }
}

执行编译。

IDE 编译:

Maven 编译:mvn clean compile

Gradle 编译:gradle clean compile

  1. 配置数据源
  2. 数据源 DataSource 配置
  3. Mybatis 的 mapper 扫描路径
  4. Mybatis 的 SqlSessionFactoryBean
@ComponentScan(basePackages = "cn.org.atool.fluent.mybatis.demo1")
@MapperScan("cn.org.atool.fluent.mybatis.demo1.entity.mapper")
@Configuration
public class HelloWorldConfig{
    /**
     * 设置dataSource属性
     *
     * @return
     */
    @Bean
    public DataSource dataSource(){
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/fluent_mybatis?useUnicode=true&characterEncoding=utf8");
        dataSource.setUsername("root");
        dataSource.setPassword("password");
        return dataSource;
    }

    /**
     * 定义mybatis的SqlSessionFactoryBean
     *
     * @param dataSource
     * @return
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean;
    }

   @Bean
   public MapperFactory mapperFactory(){
      returnnew MapperFactory();
   }
}

  1. @FluentMybatis

Fluent Mybatis 根据 Entity 类上 @FluentMybatis 注解在编译时, 会在 target 目录 class 目录下自动编译生成一系列文件:

这些文件的具体作用如下:

  • mapper/*Mapper : Mybatis 的 Mapper 定义接口, 定义了一系列通用的数据操作接口方法。
  • dao/*BaseDao : Dao 实现基类, 所有的 DaoImpl 都继承各自基类 根据分层编码的原则,我们不会在 Service 类中直接使用 Mapper 类,而是引用 Dao 类。我们在 Dao 实现类中根据条件实现具体的数据操作方法。
  • wrapper/*Query : Fluent Mybatis 核心类, 用来进行动态 sql 的构造, 进行条件查询。
  • wrapper/*Updater : Fluent Mybatis 核心类, 用来动态构造 update 语句。
  • helper/*Mapping : Entity 表字段和 Entity 属性映射定义类
  • helper/*SegmentQuery 和 Updater 具体功能实现, 包含几个实现: selectwheregroup byhaving byorder bylimit
  • IEntityRelation : 处理 Entity 关联(一对一, 一对多, 多对多)关系的接口
  • Ref : 引用 Fluent Mybatis 生成的对象的快捷入口工具类
  • 27
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值