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的数据

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
OSZAR »