此為基礎配置,不包括其他高級配置,需要其他高級配置請查閱官方文檔:[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 {@Beanpublic MapperFactory mapperFactory() {return new MapperFactory();}}
配置掃描
在啟動類添加mapper路徑,注意Fluent的Mapper是不需要手動編寫的,直接編譯生成即可,在 target 目錄下可以看到生成出來的文件。
@MapperScan("com.example.fluent.mapper")
創建表
創建 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 {@Resourceprivate 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.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.2.6:3306/test?useSSL=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=trueusername: rootpassword: root
測試接口
使用postman請求 http://localhost:8002/data 接口,測試是否可以拿到 id 為1的數據
可以看到結果是可以拿到的,整合完成。
這個例子是很簡單的,很多參數都沒有配置,比如下劃線轉駝峰之類的,有需要的可以去官方文檔查詢相關配置,這篇文章里就不贅述了。