Springboot整合FluentMybatis

为什么要使用FluentMybatis

FluentMybatis特性

Fluent Mybatis比较吸引人的地方正如它所说的,1、No XML,No Mapper, No If else,No String

2、只需要Entity就可以实现强大的FluentAPI:支持分页、嵌套查询,AND OR组合,聚合函数...

 第一步:添加依赖

注意:1、不可少了mybatis和springboot的整合包;2、junit依赖默认生效限制

<!--mybatis和springboot的整合包-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- 引入fluent-mybatis 运行依赖包, scope为compile -->
        <dependency>
            <groupId>com.github.atool</groupId>
            <artifactId>fluent-mybatis</artifactId>
            <version>${fluent.mybatis.version}</version>
        </dependency>
        <!-- 引入fluent-mybatis-processor, scope设置为provider 编译需要,运行时不需要 -->
        <dependency>
            <groupId>com.github.atool</groupId>
            <artifactId>fluent-mybatis-processor</artifactId>
            <scope>provided</scope>
            <version>${fluent.mybatis.version}</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.5.0</version>
        </dependency>
        <!-- 测试依赖 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

第二步:配置代码生成类

注意:代码生成类与启动类同一目录级别

import cn.org.atool.generator.FileGenerator;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;
import org.junit.Test;

/**
 * FluentMybatis的Entity代码生成
 */
public class EntityGenerator {

    //数据源url
    static final String url = "jdbc:mysql://localhost:3306/fluent_mybatis?useUnicode=true$characterEncoding=utf-8";

    //数据库用户名
    static final String username = "root";

    //数据库密码
    static final String password = "zhangpei@123";

    @Test
    public void generate() throws Exception {
        //引用配置类,build方法允许有多个配置类
        FileGenerator.build(Empty.class);
    }

    @Tables(
            //设置数据库连接信息
            url = url,username = username,password = password,
            //设置entity类生成src目录,相对于user.dir
            srcDir = "src/main/java",
            //设置entity类的package值
            basePack = "com.smile.fluent",
            //设置dao接口和实现的src目录,相对于user.dir
            daoDir = "src/main/java",
            //设置哪些表要生成Entity文件
            tables = {@Table(value = "hello_world")}
    )
    static class Empty{ //类名随便取,只是配置定义的一个载体

    }

}

第三步:指定代码生成类,生成Entity类和其它层级代码

        生成代码后,可能会有报错,继续往下操作即可

第四步:重新构建项目,或者使用maven进行compile

 编译后的项目,注意target下回生成以下文件

 第五步:创建配置文件,定义MapperFactory bean

注意:一定要使用@MapperScan指定mapper类所在包路径

import cn.org.atool.fluent.mybatis.spring.MapperFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan({"com.smile.fluent.mapper"})    //生成mapper所在包路径
public class ApplicationConfig {

    @Bean
    public MapperFactory mapperFactory(){
        return new MapperFactory();
    }

}

第六步:书写controller,进行使用

import com.smile.fluent.entity.HelloWorldEntity;
import com.smile.fluent.mapper.HelloWorldMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

    @Autowired
    private HelloWorldMapper helloWorldMapper;

    @GetMapping("/insert")
    public void insert(){
        helloWorldMapper.insert(new HelloWorldEntity()
                .setYourName("sysadmin")
                .setSayHello("Hello"));
        HelloWorldEntity entity = helloWorldMapper.findOne(helloWorldMapper.query()
                .where.sayHello().eq("Hello").
                        and.yourName().eq("sysadmin")
                .end());
        System.out.println(entity);
    }

}

具体方法使用查阅官方文档

fluent-mybatis-docs: fluent-mybatis使用文档https://gitee.com/fluent-mybatis/fluent-mybatis-docs#/fluent-mybatis/fluent-mybatis-docs/blob/master/02-advanced-hello-world/README.md

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

one_smail

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值