Table of contents
2. What are the characteristics of Mybatis-Flex?
3. Comparison between Mybatis-Flex and similar frameworks
4. Database types supported by Mybatis-Flex
(3) Write entity classes and Mapper
(4) Start using Mybatis-Flex through the main method (scene without Spring)
1. What is Mybatis-Flex?
Mybatis-Flex is an elegant Mybatis enhanced framework, which is very lightweight and has high performance and flexibility. We can easily use Mybaits-Flex to link any database, and its built-in QueryWrapper^ highlights help us greatly reduce the work of SQL writing and reduce the possibility of errors.
Official website documentation: Mybatis-Flex - Mybatis-Flex Official Website
2. What are the characteristics of Mybatis-Flex?
1. Lightweight : Except for MyBatis, there is no third-party light dependency and no interceptors. The principle is light implementation through SqlProvider. At the same time, in the process of execution, there is no Sql parsing (Parse) light operation. This brings several benefits: 1. Extremely high performance; 2. Very easy to track and debug the code; 3. Higher controllability.
2. Flexible : While supporting the addition, deletion, modification and query of Entity, and paging query, Mybatis-Flex provides Db + Row^ flexible tools, which can add, delete, modify and query the database without entity classes and paging query. At the same time, Mybatis-Flex's built-in QueryWrapper^ is flexible and can easily help us realize common SQL scenarios such as multi-table query, link query, subquery, etc.
3. Powerful : It supports any relational database, and can also be continuously expanded through dialects. It also supports multiple (composite) primary keys, logical deletion, optimistic lock configuration, data desensitization, data auditing, data filling and other functions.
3. Comparison between Mybatis-Flex and similar frameworks
(1) Function comparison: Mybatis-Flex and similar framework "function" comparison - Mybatis-Flex official website
(2) Performance comparison: Mybatis-Flex and similar framework "performance" comparison - Mybatis-Flex official website
4. Database types supported by Mybatis-Flex
database | describe |
---|---|
mysql | MySql database |
mariadb | MariaDB database |
oracle | Oracle11g and below database |
oracle12c | Oracle12c and above database |
db2 | DB2 database |
hsql | HSQL database |
sqlite | SQLite database |
postgresql | PostgreSQL database |
sqlserver2005 | SQLServer2005 database |
sqlserver | SQL Server database |
dm | Dameng database |
xugu | Virtual Valley Database |
kingbasees | National People's Congress Golden Warehouse Database |
phoenix | Phoenix HBase database |
gauss | Gauss database |
clickhouse | ClickHouse database |
use | NTU General (Huaku) Database |
8s | NTU General Database GBase 8s |
oscar | Supernatural Database |
sybase | Sybase ASE database |
OceanBase | OceanBase database |
Firebird | Firebird database |
derby | Derby database |
highgo | Henkel Database |
cubrid | CUBRID database |
goldilocks | GOLDILOCKS database |
island | CSIIDB database |
hana | SAP_HANA database |
impala | Impala database |
vertical | Vertica database |
xcloud | Xingyun Database |
redshift | Amazon redshift database |
openGauss | Huawei openGauss database |
TDengine | TDengine database |
informix | Informix database |
greenplum | Greenplum Database |
uxdb | Youxuan database |
5. Quick start
(1) Introduce dependencies
1. Only Mybatis is used, and Spring is not used
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-core</artifactId>
<version>1.2.0</version>
</dependency>
2. Scenarios using Spring
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring</artifactId>
<version>1.2.0</version>
</dependency>
3. Scenarios using Spring Boot
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
(2) Create a database
CREATE TABLE IF NOT EXISTS `tb_account`
(
`id` INTEGER PRIMARY KEY auto_increment,
`user_name` VARCHAR(100),
`age` Integer,
`birthday` DATETIME
);
(3) Write entity classes and Mapper
@Table("tb_account")
public class Account {
@Id(keyType = KeyType.Auto)
private Long id;
private String userName;
private Integer age;
private Date birthday;
//getter setter
}
Use @Table("tb_account") to set the mapping relationship between entity class and table name
Use @Id(keyType = KeyType.Auto) to identify the primary key as auto-increment
The Mapper interface inherits the BaseMapper interface:
public interface AccountMapper extends BaseMapper<Account> {
}
(4) Start using Mybatis-Flex through the main method (scene without Spring)
public class HelloWorld {
public static void main(String[] args) {
//创建数据源
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mybatis-flex");
dataSource.setUsername("username");
dataSource.setPassword("password");
//配置数据源
MybatisFlexBootstrap.getInstance()
.setDatasource(dataSource)
.addMapper(AccountMapper.class)
.start();
//获取 mapper
AccountMapper mapper = MybatisFlexBootstrap.getInstance()
.getMapper(AccountMapper.class);
//示例1:查询 id=1 的数据
Account account = mapper.selectOneById(1);
//示例2:根据 QueryWrapper 查询 id >= 100 的数据列表
QueryWrapper query = QueryWrapper.create()
.where(ACCOUNT.ID.ge(100));
List<Account> accounts = mapper.selectListByQuery(query);
//示例3:者使用 Db + Row 查询
String sql = "select * from tb_account where age > ?";
List<Row> rows = Db.selectListBySql(sql, 18);
}
}
(5) More examples
- Example 1: Mybatis-Flex native (non-Spring)
- Example 2: Mybatis-Flex with Spring
- Example 3: Mybatis-Flex with Spring boot
- Example 4: Db + Row