赞
踩
说明:MyBatis-Flex(官网地址:https://mybatis-flex.com/),是一款数据访问层框架,可实现项目中对数据库的访问,类比MyBatis-Plus。本文介绍,在Spring Boot项目整合MyBatis-Flex。
首先,创建一个Spring boot项目,pom.xml文件内容如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.12</version> <relativePath/> </parent> <groupId>com.hezy</groupId> <artifactId>mybatis-flex-demo</artifactId> <version>1.0-SNAPSHOT</version> <name>Archetype - mybatis-flex-demo</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> </project>
创建一个实体类对象,User,如下:
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String password;
}
创建一个接口,如下:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
application.yml,配置文件如下:
server:
port: 8080
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/demo
username: postgres
password: 123456
启动项目,测试,没得问题
引入下面这两个依赖;
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.9.3</version>
<scope>provided</scope>
</dependency>
在实体类上关联表,包括实体类对应的表名,以及对应的字段,像主键、别名(如果有)等;
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Table; import lombok.Data; import java.io.Serializable; @Data @Table("tb_user") public class User implements Serializable { private static final long serialVersionUID = 1L; @Id private Long id; private String username; private String password; }
创建数据访问层对象,可继承MyBatis-Flex提供的接口,并指定泛型为当前操作的实体类对象
import com.hezy.pojo.User;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
创建一个接口,根据ID查询User,可直接调用MyBatis-Flex提供的相关API,如下:
@Autowired
private UserMapper userMapper;
@GetMapping("/getUser/{id}")
public User getUserById(@PathVariable("id") Integer id) {
return userMapper.selectOneById(id);
}
一般来说,还需要Services过一层,校验参数等,这里是是一个Demo。数据库内容如下:
启动项目,调用接口,如下,查询完成
本文介绍了如何在Spring Boot项目中整合MyBatis-Flex,当然,MyBatis-Flex还提供了许多数据访问的API,以及扩展功能,如多数据源访问、数据库配置加密、多租户、读写分离等等,可在官网上学习。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。