โšก

MyBatis-Flex ์น˜ํŠธ์‹œํŠธ

MyBatis-Flex ์˜์กด์„ฑ ์„ค์ •(Gradle/Maven), ์—”ํ‹ฐํ‹ฐ ๋งคํ•‘, BaseMapper, QueryWrapper, Lambda ๋™์  ์ฟผ๋ฆฌ, CTE ์žฌ๊ท€ ์ฟผ๋ฆฌ, ์—ฐ๊ด€๊ด€๊ณ„ ๋ฐ ์‹ค๋ฌด ๋ ˆํผ๋Ÿฐ์Šค

--views
๋‹จ์ˆ˜:
#mybatis-flex#mybatis#database#sql#java#kotlin#orm#crud#cte#gradle#maven์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-09-27

1. ํ”„๋กœ์ ํŠธ ์˜์กด์„ฑ ์ถ”๊ฐ€ (Gradle & Maven)

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Linux, macOS, Windows 10/11Ubuntu 22.04 LTS, macOS 13+, Windows 11JVM 17+ ์ง€์› OS
CPU2 ์ฝ”์–ด ์ด์ƒ4 ์ฝ”์–ด ์ด์ƒAPT(Annotation Processor) ์ฝ”๋“œ ์ž๋™ ์ƒ์„ฑ ์—ฐ์‚ฐ
๋ฉ”๋ชจ๋ฆฌ (RAM)2 GB ์ด์ƒ4 GB ~ 8 GB ์ด์ƒSpring Boot ์ปจํ…Œ์ด๋„ˆ ๋ฐ IDE ์ฆ๋ถ„ ์ปดํŒŒ์ผ ๋ฉ”๋ชจ๋ฆฌ
๋””์Šคํฌ (Storage)2 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„5 GB ์ด์ƒGradle/Maven ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์บ์‹œ
๋„คํŠธ์›Œํฌ / ๋Ÿฐํƒ€์ž„JDK 17 / 21 / 25 LTS+, Spring Boot 3.x / 4.x+MyBatis-Flex 1.11.8+, APT ํ”Œ๋Ÿฌ๊ทธ์ธRDBMS JDBC ๋“œ๋ผ์ด๋ฒ„ (MySQL, PostgreSQL ๋“ฑ)

[!NOTE] Spring Boot ๋ฒ„์ „๋ณ„ ์Šคํƒ€ํ„ฐ ๋งคํ•‘

  • Spring Boot 4.x: mybatis-flex-spring-boot4-starter (Java 17/21+, Spring Boot 4 ๋Œ€์‘)
  • Spring Boot 3.x: mybatis-flex-spring-boot3-starter (Java 17+, Jakarta ๋„ค์ž„์ŠคํŽ˜์ด์Šค)
  • Spring Boot 2.x: mybatis-flex-spring-boot-starter (Java 8/11+, javax ๋„ค์ž„์ŠคํŽ˜์ด์Šค)
  • APT ํ”„๋กœ์„ธ์„œ: mybatis-flex-processor (๋ฒ„์ „ ๊ณตํ†ต, Q-Class ๋ฐ TableDef ์ƒ์„ฑ)
  • ์ฃผ์˜: ๊ธฐ์กด mybatis-spring-boot-starter์™€ ์ค‘๋ณต ์„ ์–ธ ์‹œ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ flex ์Šคํƒ€ํ„ฐ๋งŒ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

Gradle (Groovy DSL - build.gradle)

dependencies {
    // 1) Spring Boot 4.x ์Šคํƒ€ํ„ฐ (Spring Boot 4.0 ์ด์ƒ ํ™˜๊ฒฝ)
    implementation 'com.mybatis-flex:mybatis-flex-spring-boot4-starter:1.11.8'

    // 2) Spring Boot 3.x ์Šคํƒ€ํ„ฐ (Spring Boot 3.x ์‚ฌ์šฉ ์‹œ)
    // implementation 'com.mybatis-flex:mybatis-flex-spring-boot3-starter:1.10.6'

    // 3) Spring Boot 2.x ์Šคํƒ€ํ„ฐ (Spring Boot 2.x ๋ ˆ๊ฑฐ์‹œ ์‚ฌ์šฉ ์‹œ)
    // implementation 'com.mybatis-flex:mybatis-flex-spring-boot-starter:1.10.6'

    // APT ํ”„๋กœ์„ธ์„œ (Q-Class ๋ฐ TableDef ํด๋ž˜์Šค ์ž๋™ ์ƒ์„ฑ์„ ์œ„ํ•ด ํ•„์ˆ˜)
    annotationProcessor 'com.mybatis-flex:mybatis-flex-processor:1.11.8'

    // Lombok๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ˆœ์„œ: Lombok ์„ ์–ธ ํ›„ mybatis-flex-processor ์„ ์–ธ
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
}

Gradle (Kotlin DSL - build.gradle.kts)

dependencies {
    // 1) Spring Boot 4.x ์Šคํƒ€ํ„ฐ
    implementation("com.mybatis-flex:mybatis-flex-spring-boot4-starter:1.11.8")

    // 2) Spring Boot 3.x ์Šคํƒ€ํ„ฐ ์‚ฌ์šฉ ์‹œ
    // implementation("com.mybatis-flex:mybatis-flex-spring-boot3-starter:1.10.6")

    // 3) Spring Boot 2.x ์Šคํƒ€ํ„ฐ ์‚ฌ์šฉ ์‹œ
    // implementation("com.mybatis-flex:mybatis-flex-spring-boot-starter:1.10.6")

    // APT ํ”„๋กœ์„ธ์„œ (TableDef ์ž๋™ ์ƒ์„ฑ)
    annotationProcessor("com.mybatis-flex:mybatis-flex-processor:1.11.8")

    // (์„ ํƒ ์‚ฌํ•ญ) Kotlin ํ™•์žฅ ๊ธฐ๋Šฅ ๋ฐ DSL ์ง€์› ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
    implementation("com.mybatis-flex:mybatis-flex-kotlin:1.11.8")
}

Maven (pom.xml)

<dependencies>
    <!-- 1) Spring Boot 4.x ์Šคํƒ€ํ„ฐ -->
    <dependency>
        <groupId>com.mybatis-flex</groupId>
        <artifactId>mybatis-flex-spring-boot4-starter</artifactId>
        <version>1.11.8</version>
    </dependency>

    <!-- 2) Spring Boot 3.x ์Šคํƒ€ํ„ฐ ์‚ฌ์šฉ ์‹œ -->
    <!--
    <dependency>
        <groupId>com.mybatis-flex</groupId>
        <artifactId>mybatis-flex-spring-boot3-starter</artifactId>
        <version>1.10.6</version>
    </dependency>
    -->

    <!-- 3) Spring Boot 2.x ์Šคํƒ€ํ„ฐ ์‚ฌ์šฉ ์‹œ -->
    <!--
    <dependency>
        <groupId>com.mybatis-flex</groupId>
        <artifactId>mybatis-flex-spring-boot-starter</artifactId>
        <version>1.10.6</version>
    </dependency>
    -->
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.13.0</version>
            <configuration>
                <annotationProcessorPaths>
                    <!-- Lombok ์‚ฌ์šฉ ์‹œ (์ˆœ์„œ ์ค‘์š”: lombok -> mybatis-flex) -->
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                    <!-- MyBatis-Flex APT ํ”„๋กœ์„ธ์„œ (TableDef ์ž๋™ ์ƒ์„ฑ) -->
                    <path>
                        <groupId>com.mybatis-flex</groupId>
                        <artifactId>mybatis-flex-processor</artifactId>
                        <version>1.11.8</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

2. ์—”ํ‹ฐํ‹ฐ ๋งคํ•‘ & BaseMapper ๊ธฐ๋ณธ ๊ตฌ์กฐ

// 1. ์—”ํ‹ฐํ‹ฐ ์ •์˜ (@Table, @Id, @Column)
@Table("tb_user")
@Getter
@Setter
public class User {
    @Id(keyType = KeyType.Auto) // MySQL AUTO_INCREMENT
    private Long id;

    @Column("user_name")
    private String name;

    private Integer age;

    @Column(isLogicDelete = true) // ๋…ผ๋ฆฌ ์‚ญ์ œ ์ปฌ๋Ÿผ (์กฐํšŒ ์‹œ ์ž๋™ ์ œ์™ธ)
    private Boolean isDeleted;

    @Column(version = true) // ๋‚™๊ด€์  ๋ฝ ๋ฒ„์ „ ์ปฌ๋Ÿผ (์ˆ˜์ • ์‹œ ์ž๋™ +1)
    private Long version;
}

// 2. Mapper ์ธํ„ฐํŽ˜์ด์Šค ์„ ์–ธ (BaseMapper ์ƒ์†๋งŒ์œผ๋กœ ๊ธฐ๋ณธ CRUD ์ž๋™ ์™„์„ฑ)
public interface UserMapper extends BaseMapper<User> {}
// ๊ธฐ๋ณธ CRUD ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์˜ˆ์‹œ
userMapper.insert(user);             // ๋‹จ๊ฑด ๋“ฑ๋ก
userMapper.insertBatch(userList);    // ๋Œ€๋Ÿ‰ ๋ฐฐ์น˜ ๋“ฑ๋ก
userMapper.selectOneById(1L);        // PK ๋‹จ๊ฑด ์กฐํšŒ
userMapper.update(user);             // ๋‹จ๊ฑด ์ˆ˜์ • (null ํ•„๋“œ ์ œ์™ธ)
userMapper.deleteById(1L);           // ๋…ผ๋ฆฌ ์‚ญ์ œ ๋˜๋Š” ๋ฌผ๋ฆฌ ์‚ญ์ œ

3. Lambda ๊ธฐ๋ฐ˜ Type-Safe ๋™์  ์ฟผ๋ฆฌ (Q-Class ๋ถˆํ•„์š”)

// Q-Class๋‚˜ ์ฝ”๋“œ ์ƒ์„ฑ ์—†์ด Java ๋ฉ”์„œ๋“œ ์ฐธ์กฐ(Lambda)๋กœ 100% Type-Safe ์ฟผ๋ฆฌ ์ž‘์„ฑ!
String searchName = "kim";
Integer minAge = 20;

QueryWrapper query = QueryWrapper.create()
    .select(User::getId, User::getName, User::getAge)
    .from(User.class)
    .where(User::getName).like(searchName, hasText(searchName)) // ์กฐ๊ฑด์ด true์ผ ๋•Œ๋งŒ WHERE ์ ˆ ํฌํ•จ
    .and(User::getAge).ge(minAge, minAge != null)               // null-safe ๋™์  ์กฐ๊ฑด
    .orderBy(User::getId).desc();

List<User> users = userMapper.selectListByQuery(query);

4. APT ํ…Œ์ด๋ธ” ์ •์˜(Tables) ํ™œ์šฉ (์„ ํƒ ์‚ฌํ•ญ)

// APT(Annotation Processor) ์„ค์ • ์‹œ ์ƒ์„ฑ๋˜๋Š” ์ •์  ํ…Œ์ด๋ธ” ํด๋ž˜์Šค(USER) ํ™œ์šฉ ๊ฐ€๋Šฅ
import static com.example.entity.table.UserTableDef.USER;

QueryWrapper query = QueryWrapper.create()
    .select(USER.ID, USER.NAME, USER.AGE)
    .from(USER)
    .where(USER.AGE.between(20, 30))
    .and(USER.NAME.like("Lee"));

List<User> list = userMapper.selectListByQuery(query);

5. CTE (Common Table Expressions) & ์žฌ๊ท€ ์ฟผ๋ฆฌ (WITH)

// MyBatis-Flex๋Š” ๋ณต์žกํ•œ ๊ณ„์ธตํ˜• ์กฐ์ง๋„, ์นดํ…Œ๊ณ ๋ฆฌ ํŠธ๋ฆฌ ์กฐํšŒ๋ฅผ ์œ„ํ•œ ์žฌ๊ท€ CTE๋ฅผ ๋„ค์ดํ‹ฐ๋ธŒ ์ง€์›!
QueryWrapper query = QueryWrapper.create()
    .withRecursive("DeptTree")
    .asSelect(
        // Anchor Member (์‹œ์ž‘ ๋…ธ๋“œ)
        select(DEPARTMENT.ALL_COLUMNS)
        .from(DEPARTMENT)
        .where(DEPARTMENT.ID.eq(1L))
        .unionAll(
            // Recursive Member (ํ•˜์œ„ ๋…ธ๋“œ ํƒ์ƒ‰)
            select(DEPARTMENT.ALL_COLUMNS)
            .from(DEPARTMENT)
            .join("DeptTree").on(DEPARTMENT.PARENT_ID.eq(column("DeptTree.id")))
        )
    )
    .select()
    .from("DeptTree");

List<Department> departments = deptMapper.selectListByQuery(query);

6. ์—ฐ๊ด€๊ด€๊ณ„ ๋งคํ•‘ (@RelationOneToOne / ManyToOne / OneToMany)

@Table("tb_order")
@Getter @Setter
public class Order {
    @Id(keyType = KeyType.Auto)
    private Long id;
    private Long userId;

    // N:1 ๊ด€๊ณ„ - ๋‹จ์ผ ์ฟผ๋ฆฌ๋กœ ๋งคํ•‘ํ•˜์—ฌ N+1 ๋ฌธ์ œ ์›์ฒœ ์ฐจ๋‹จ
    @RelationManyToOne(selfField = "userId", targetField = "id")
    private User user;

    // 1:N ๊ด€๊ณ„
    @RelationOneToMany(selfField = "id", targetField = "orderId")
    private List<OrderItem> items;
}
// ์—ฐ๊ด€ ์—”ํ‹ฐํ‹ฐ๊นŒ์ง€ ํ•จ๊ป˜ ์กฐํšŒ (WithRelations ๊ณ„์—ด ๋ฉ”์„œ๋“œ)
Order order = orderMapper.selectOneWithRelationsById(orderId);
List<Order> orders = orderMapper.selectAllWithRelations();

7. ํŽ˜์ด์ง• & ์ •๋ ฌ (Pagination)

// 1. Page ๊ฐ์ฒด ์ƒ์„ฑ (ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘)
Page<User> page = Page.of(1, 10); // 1ํŽ˜์ด์ง€, 10๊ฑด

// 2. ํŽ˜์ด์ง• ์ฟผ๋ฆฌ ์‹คํ–‰ (์ž๋™ COUNT ์ฟผ๋ฆฌ + LIMIT/OFFSET ์ƒ์„ฑ)
QueryWrapper query = QueryWrapper.create()
    .where(User::getAge).gt(18)
    .orderBy(User::getId).desc();

Page<User> result = userMapper.paginate(page, query);

// 3. ๊ฒฐ๊ณผ ์ ‘๊ทผ
long totalRow = result.getTotalRow();     // ์ „์ฒด ๋ ˆ์ฝ”๋“œ ์ˆ˜
List<User> records = result.getRecords(); // ํ˜„์žฌ ํŽ˜์ด์ง€ ๋ฐ์ดํ„ฐ
long totalPage = result.getTotalPage();   // ์ „์ฒด ํŽ˜์ด์ง€ ์ˆ˜

8. UpdateChain (์•ˆ์ „ํ•˜๊ณ  ์œ ์—ฐํ•œ ์กฐ๊ฑด๋ถ€ ์ˆ˜์ •)

// ์—”ํ‹ฐํ‹ฐ๋ฅผ ํ†ต์งธ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ์›ํ•˜๋Š” ํ•„๋“œ๋งŒ ์ฆ‰์‹œ ์—…๋ฐ์ดํŠธ
boolean success = UpdateChain.of(User.class)
    .set(User::getAge, 25)
    .set(User::getName, "UpdatedName")
    .where(User::getId).eq(100L)
    .update();

// IN ์กฐ๊ฑด ๋Œ€๋Ÿ‰ ์ƒํƒœ ๋ณ€๊ฒฝ
UpdateChain.of(Order.class)
    .set(Order::getStatus, "SHIPPED")
    .where(Order::getId).in(orderIds)
    .update();

9. ๊ฐ์‚ฌ(Auditing) & ์ž๋™ ์ปฌ๋Ÿผ ์ฑ„์šฐ๊ธฐ

@Table("tb_article")
@Getter @Setter
public class Article {
    @Id(keyType = KeyType.Auto)
    private Long id;
    private String title;

    // 1. SQL ํ•จ์ˆ˜๋กœ ์ธ์„œํŠธ/์—…๋ฐ์ดํŠธ ์‹œ ์ž๋™ ์ฑ„์šฐ๊ธฐ
    @Column(onInsertValue = "now()")
    private LocalDateTime createdAt;

    @Column(onInsertValue = "now()", onUpdateValue = "now()")
    private LocalDateTime updatedAt;

    // 2. ๋˜๋Š” ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก์„ ํ†ตํ•œ ์„ธ์…˜ ์‚ฌ์šฉ์ž ID ์ฃผ์ž…
    @Column(onInsertValue = "sessionUserId()", onUpdateValue = "sessionUserId()")
    private Long updatedBy;
}

10. Kotlin ์ง€์› (Kotlin Extension & DSL)

// Kotlin์—์„œ๋Š” ํ”„๋กœํผํ‹ฐ ์ฐธ์กฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋”์šฑ ์ง๊ด€์ ์œผ๋กœ ์ž‘์„ฑ
val query = query {
    select(User::id, User::name, User::age)
    from(User::class.java)
    where(User::name like "ํ™%")
    and(User::age ge 20)
    orderBy(User::id.desc())
}

val userList = userMapper.selectListByQuery(query)

11. Spring Boot ํ™˜๊ฒฝ์„ค์ • (application.yml)

mybatis-flex:
  # 1. SQL ์‹คํ–‰ ๋กœ๊ทธ ์ถœ๋ ฅ (๊ฐœ๋ฐœ ํ™˜๊ฒฝ)
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true

  # 2. ๊ธ€๋กœ๋ฒŒ ๋…ผ๋ฆฌ ์‚ญ์ œ ์„ค์ •
  global-config:
    logic-delete-column: is_deleted
    logic-deleted-value: 1
    logic-normal-value: 0

  # 3. ์ฝ˜์†” ์‹คํ–‰ SQL ํฌ๋งทํŒ… ๋ฐ ์‹คํ–‰ ์‹œ๊ฐ„ ์ถœ๋ ฅ
  logging:
    enable: true
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500