How to use Java enums (Enum) in MyBatis Mapper XML

Introduction

In MyBatis Mapper XML Although there is code on the net that references a constant (static final), I didn't see much code that references enums, so I wrote it.

Installation

build.gradle


    compile 'org.mybatis:mybatis:3.5.2'

Enumeration sample

SampleEnums.java


package com.example;

public class SampleEnums {

    public enum Color {
        RED("1"), GREEN("2"), BLUE("3");

        private final String code;

        private Color(final String code) {
            this.code = code;
        }

        public String getCode() {
            return this.code;
        }
    }
}

Mapper XML sample (set code)

<select id="findByColorIsBlue" resultType="SampleDto">
    SELECT *
      FROM SAMPLE_TABLE
     WHERE COLOR = '${@[email protected]}'

<!--Generated SQL
    SELECT *
      FROM SAMPLE_TABLE
     WHERE COLOR = '2'
-->
</select>

Mapper XML Sample 2 (Set Name)

<select id="findByColorIsBlue" resultType="SampleDto">
    SELECT *
      FROM SAMPLE_TABLE
     WHERE COLOR = '${@com.example.SampleEnums$Color@BLUE}'

<!--Generated SQL
    SELECT *
      FROM SAMPLE_TABLE
     WHERE COLOR = 'BLUE'
-->
</select>

Recommended Posts

How to use Java enums (Enum) in MyBatis Mapper XML
How to use MyBatis Mapper annotation
How to use classes in Java?
How to use Java enum type
[Java] How to substitute Model Mapper in Jackson
[Java] How to use Map
[Java] How to use Map
[Rails] How to use enum
How to use java Optional
[Rails] How to use enum
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
Notes on how to use regular expressions in Java
How to store an object in PostgreSQL as JSON with MyBatis (Mapper XML)
How to use Lombok in Spring
How to use Java HttpClient (Get)
How to use JSON data in WebSocket communication (Java, JavaScript)
How to pass an object to Mapper in MyBatis without arguments
How to use Java HttpClient (Post)
How to learn JAVA in 7 days
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
How to call and use API in Java (Spring Boot)
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to name variables in Java
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to concatenate strings in java
How to implement Kalman filter in Java
[Java] How to use the File class
How to use custom helpers in rails
[Java] How to use the hasNext function
How to use named volume in docker-compose.yml
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
ResultType behaves strangely in MyBatis Mapper XML
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
How to use Java classes, definitions, import
How to embed Janus Graph in Java
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
How to get the date in java
[Processing × Java] How to use the function
How to use environment variables in RubyOnRails
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] How to use the Calendar class
Understand in 5 minutes !! How to use Docker