SpringBoot 3.x整合Fluent Mybatis极简流程

此为基础配置,不包括其他高级配置,需要其他高级配置请查阅官方文档:[fluent mybatis特性总览 - Wiki - Gitee.com](https://gitee.com/fluent-mybatis/fluent-mybatis/wikis/fluent mybatis特性总览)

版本信息

  • Spring Boot 版本:3.1.2
  • Fluent Mybatis 版本:1.9.9
  • mybatis-spring-boot-starter 版本:3.0.0
  • JDK 版本:JDK17

Maven依赖

spring boot依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath/> 
    </parent>

<dependencies>
	<dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
    
        <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>
     

Fluent Mybatis和MySQL数据库依赖


<properties>
    <fluent-mybatis.version>1.9.9</fluent-mybatis.version>
</properties>
<dependencies>
    <!--mysql驱动-->
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>

 	<!-- 引入fluent-mybatis 运行依赖包, scope为compile -->
    <dependency>
        <groupId>com.github.atool</groupId>
        <artifactId>fluent-mybatis</artifactId>
        <version>${fluent-mybatis.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
            </exclusion>
        </exclusions>
    </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>

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
</dependencies>

添加配置类

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

@Configuration
public class MybatisConfig {

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

}

配置扫描

在启动类添加mapper路径,注意Fluent的Mapper是不需要手动编写的,直接编译生成即可,在 target 目录下可以看到生成出来的文件。

@MapperScan("com.example.fluent.mapper")

image-20230810091528000

创建表

创建 test测试库,并创建 person表。

CREATE TABLE `person` (
  `id` int NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `age` int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10000001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;



INSERT INTO `test`.`person` (`id`, `first_name`, `last_name`, `email`, `age`) VALUES (1, 'First0000001', 'Last0000001', 'email0000001@example.com', 92);
INSERT INTO `test`.`person` (`id`, `first_name`, `last_name`, `email`, `age`) VALUES (2, 'First0000002', 'Last0000002', 'email0000002@example.com', 33);
INSERT INTO `test`.`person` (`id`, `first_name`, `last_name`, `email`, `age`) VALUES (3, 'First0000003', 'Last0000003', 'email0000003@example.com', 19);

添加实体类

import cn.org.atool.fluent.mybatis.annotation.FluentMybatis;
import cn.org.atool.fluent.mybatis.annotation.TableId;
import cn.org.atool.fluent.mybatis.base.IEntity;
import lombok.Data;

@FluentMybatis(table = "person")
@Data
public class Person implements IEntity {

    @TableId //主键,不指定默认主键自增
    private Long id;
    private String firstName;
    private String lastName;
    private String email;
    private int age;

}

添加测试接口

@RestController
public class PersonController {

    @Resource
    private PersonMapper personMapper;

    @GetMapping("/data")
    public Object getData() {
        Person person = personMapper.findById(1); //查询ID为1的数据
        return person;
    }

}

配置文件添加数据库连接信息

server:
  port: 8002 //指定8002端口运行

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.2.6:3306/test?useSSL=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
    username: root
    password: root

测试接口

使用postman请求 http://localhost:8002/data 接口,测试是否可以拿到 id 为1的数据

可以看到结果是可以拿到的,整合完成。

这个例子是很简单的,很多参数都没有配置,比如下划线转驼峰之类的,有需要的可以去官方文档查询相关配置,这篇文章里就不赘述了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fluent Mybatis是一个基于Mybatis的ORM框架,它提供了一种更加简洁、流畅的方式来操作数据库。通过使用Fluent Mybatis,开发者可以更轻松地进行数据库的增删改查操作。引用\[1\]中的代码片段展示了在使用Fluent Mybatis时需要引入的相关依赖。其中,fluent-mybatis是运行时的依赖包,而fluent-mybatis-processor是编译时的依赖包。 引用\[2\]中的代码片段展示了一个使用Fluent Mybatis进行数据插入的示例。在这个示例中,通过@Autowired注解注入了TestFluentMybatisMapper,然后使用该Mapper对象进行数据插入操作。 引用\[3\]是一篇关于Fluent Mybatis的文章,作者在文章中分享了自己学习该框架的过程,并展示了一些实用的代码。这篇文章介绍了Fluent Mybatis相对于其他ORM框架的优势,并提到了作者尝试去除一些项目中用不到的功能,以展示一些实用且有帮助的代码。 综上所述,Fluent Mybatis是一个基于Mybatis的ORM框架,它提供了一种简洁、流畅的方式来操作数据库。通过引入相关依赖并使用相应的Mapper对象,开发者可以轻松地进行数据库操作。 #### 引用[.reference_title] - *1* *3* [FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践](https://blog.csdn.net/zhiweihongyan1/article/details/120848199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [FluentMybatis 项目构建、代码生成(二) | FluentMybatis实践](https://blog.csdn.net/zhiweihongyan1/article/details/120854377)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值