Mybatis-Flex之基础搭建

1、是什么?

MyBatis-Flex 是一个优雅的 MyBatis 增强框架,它非常轻量、同时拥有极高的性能与灵活性。我们可以轻松的使用 Mybaits-Flex 链接任何数据库,其内置的QueryWrapper亮点 帮助我们极大的减少了 SQL 编写的工作的同时,减少出错的可能性。

更轻量
MyBatis-Flex 除了 MyBatis 本身,再无任何第三方依赖,因此会带来更高的自主性、把控性和稳定性。在任何一个系统中,依赖越多,稳定性越差。

更灵活
MyBatis-Flex 提供了非常灵活的 QueryWrapper,支持关联查询、多表查询、多主键、逻辑删除、乐观锁更新、数据填充、数据脱敏、等等…

更高的性能
MyBatis-Flex 通过独特的架构,没有任何 MyBatis 拦截器、在 SQL 执行的过程中,没有任何的 SQL Parse,因此会带来指数级的性能增长。

总而言之,MyBatis-Flex 能够极大地提高我们的开发效率和开发体验,让我们有更多的时间专注于自己的事情。

2、环境搭建

(1) 创建一个SpringBoot工程(说明:当然,也可以不依赖于Spring框架;官方声明)

Mybatis-Flex之基础搭建_第1张图片

(2) 导入相关依赖

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.lygroupId>
    <artifactId>springboot-mybatis-flexartifactId>
    <version>1.0-SNAPSHOTversion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>3.1.0version>
    parent>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>com.mybatis-flexgroupId>
            <artifactId>mybatis-flex-spring-boot-starterartifactId>
            <version>1.5.3version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.33version>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>com.zaxxergroupId>
            <artifactId>HikariCPartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.28version>
        dependency>
    dependencies>

project>
(3) 配置文件

application.yml

spring:
  profiles:
    active: home
mybatis-flex:
  mapper-locations: classpath:/mapper/**/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

application-demo.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/ly_test
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.zaxxer.hikari.HikariDataSource

application-company.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/ly
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.zaxxer.hikari.HikariDataSource
(4) 主启动类
package com.ly;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author : ly
 */
@SpringBootApplication
public class MybatisFlexApplication {
    public static void main(String[] args) {
        SpringApplication.run(MybatisFlexApplication.class, args);
    }
}
(5) 测试环境搭建
/**
     * 测试环境是否构建成功
     */
    @Test
    public void testSelectAll() {
        /**
         * SELECT `id`, `user_name`, `age`, `birthday` FROM `tb_account`
         */
        List<Account> accounts = accountMapper.selectAll();
        Assertions.assertTrue(accounts.size() > 0);
    }

    /**
     * 自定义xml
     * 以前mybatis怎么用现在继续怎么用
     */
    @Test
    public void testCustomXml() {
        /**
         * select user_name from tb_account where age >= 20
         */
        List<Account> accounts = accountMapper.gtAge(20);
        Assertions.assertTrue(accounts.size() > 0);
    }

Mybatis-Flex之基础搭建_第2张图片

详细使用参考 MybatisFlex之增、删、改

你可能感兴趣的:(MybatisFlex,mybatis)