此为基础配置,不包括其他高级配置,需要其他高级配置请查阅官方文档:[fluent mybatis特性总览 - Wiki - Gitee.com](https://gitee.com/fluent-mybatis/fluent-mybatis/wikis/fluent mybatis特性总览)
spring boot依赖
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>3.1.2version>
<relativePath/>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
Fluent Mybatis和MySQL数据库依赖
<properties>
<fluent-mybatis.version>1.9.9fluent-mybatis.version>
properties>
<dependencies>
<dependency>
<groupId>com.mysqlgroupId>
<artifactId>mysql-connector-jartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.github.atoolgroupId>
<artifactId>fluent-mybatisartifactId>
<version>${fluent-mybatis.version}version>
<exclusions>
<exclusion>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
exclusion>
<exclusion>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.github.atoolgroupId>
<artifactId>fluent-mybatis-processorartifactId>
<scope>providedscope>
<version>${fluent-mybatis.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>3.0.0version>
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")
创建 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', '[email protected]', 92);
INSERT INTO `test`.`person` (`id`, `first_name`, `last_name`, `email`, `age`) VALUES (2, 'First0000002', 'Last0000002', '[email protected]', 33);
INSERT INTO `test`.`person` (`id`, `first_name`, `last_name`, `email`, `age`) VALUES (3, 'First0000003', 'Last0000003', '[email protected]', 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的数据
可以看到结果是可以拿到的,整合完成。
这个例子是很简单的,很多参数都没有配置,比如下划线转驼峰之类的,有需要的可以去官方文档查询相关配置,这篇文章里就不赘述了。