此為基礎(chǔ)配置,不包括其他高級配置,需要其他高級配置請查閱官方文檔:[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數(shù)據(jù)庫依賴
<properties>
<fluent-mybatis.version>1.9.9</fluent-mybatis.version>
</properties>
<dependencies>
<!--mysql驅(qū)動-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 引入fluent-mybatis 運(yùn)行依賴包, 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設(shè)置為provider 編譯需要,運(yùn)行時不需要 -->
<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")
創(chuàng)建表
創(chuàng)建 test
測試庫,并創(chuàng)建 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);
添加實(shí)體類
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 //主鍵,不指定默認(rèn)主鍵自增
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的數(shù)據(jù)
return person;
}
}
配置文件添加數(shù)據(jù)庫連接信息
server:
port: 8002 //指定8002端口運(yùn)行
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的數(shù)據(jù)
可以看到結(jié)果是可以拿到的,整合完成。文章來源:http://www.zghlxwxcb.cn/news/detail-640395.html
這個例子是很簡單的,很多參數(shù)都沒有配置,比如下劃線轉(zhuǎn)駝峰之類的,有需要的可以去官方文檔查詢相關(guān)配置,這篇文章里就不贅述了。文章來源地址http://www.zghlxwxcb.cn/news/detail-640395.html
到了這里,關(guān)于SpringBoot 3.x整合Fluent Mybatis極簡流程的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!