[JAVA] How to dynamically change the column name acquired by MyBatis

How to dynamically change the column name acquired by MyBatis

In order to dynamically pass the condition value of where clause in MyBatis, I think that it is common to treat it as a prepared statement using "#" as shown below.

SQL definition file.xml


<select id="getHogeValue" resultType="java.util.Map">
select * 
form hoge_table
where foo_1 = #{foovalue}
</select>

This section describes how to dynamically change the column to be acquired. In other words, it is useful when the column specified in the select clause is indefinite.

environment

Java:JDK1.7 MyBatis:mybatis-3.2.5.jar

Implementation method

Instead of passing it as a prepared statement value Use "$" to pass as a string.

Below is an example.

Data access class.java


    //Define the dynamic part of the column name in List
    List<Integer> columnIdList = Arrays.asList(1,2,3,4);
    
    //Set List of column name dynamic part as Map
    Map params = new HashMap();
    params.put("column_id_list", columnIdList);
    
    //FOO from Hoge table_1, FOO_2, FOO_3, FOO_4, BAR_1, BAR_2, BAR_3, BAR_Get 4
    List<Map> hogeValueLsit = hogeTableMapper.getHogeValue(params);
    
    //Check the acquired contents
    for (Map elm : hogeValueLsit) {
      int idx = 0;
      for (Integer columnId : columnIdList) {
        
        String foo = (String)elm.get("FOO_"+columnId);
        System.out.println("FOO_" + idx + "The second is" + foo + "is.");
        
        String bar = (String)elm.get("BAR_"+columnId);
        System.out.println("BAR_" + idx + "The second is" + bar + "is.");
        
      }
    }

SQL definition file.xml


    <select id="getHogeValue" resultType="java.util.Map">
    select 
      <foreach item="column_id" collection="column_id_list">
         foo_${column_id}
        ,bar_${column_id}
      </foreach>
    from hoge_table 
    </select>

important point

Note that the column name is "uppercase" when retrieving on the Java side Even if the SQL definition file side uses lowercase "foo_" The Map key on the Java side can only be obtained with "FOO_" in uppercase.

that's all.

Recommended Posts

How to dynamically change the column name acquired by MyBatis
[Ruby on Rails] How to change the column name
[Rails] How to change the column name of the table
How to change the file name with Xcode (Refactor Rename)
[Swift] How to dynamically change the height of the toolbar on the keyboard
How to change app name in rails
How to change the timezone on Ubuntu
Ransack sort_link How to change the color!
[Rails] How to log in with a name by adding a devise name column
[Rails] How to create a table, add a column, and change the column type
How to reference a column when overriding the column name method in ActiveRecord
http: // localhost: How to change the port number
How to change the action with multiple submit buttons
How to return to the previous screen by Swipe operation
How to remove the underline displayed by Rails link_to
[Java] How to extract the file name from the path
How to move to the details screen by clicking the image
Change the database (MySQL) primary key to any column.
[Ruby on Rails] Change URL id to column name
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
How to get the class name / method name running in Java
How to connect the strings in the List separated by commas
[IOS] How to get the table name from AWS DynamoDB
Git How to easily return to the state before the change (before commit)
How to change the setting value of Springboot Hikari CP
[Rails] How to display the list of posts by category
How to change the contents of the jar file without decompressing
How to set the IP address and host name of CentOS8
[Rails] How to introduce kaminari with Slim and change the design
How to display products by category on the same list screen
[Rails] How to change the page title of the browser for each page
Use JUL to record the SQL execution log issued by MyBatis
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[chown] How to change the owner of a file or directory
[For beginners] How to get the Ruby delayed railway line name
[Rails 5] How to display the password change screen when using devise
How to use the link_to method
How to use the include? method
Dynamically switch the database to connect to
How to find the average angle
How to use the wrapper class
How to change kube-proxy to ipvs mode.
How to use MyBatis Mapper annotation
How to name variables in Java
How to add the delete function
How to change from HTML to Haml
[Swing] How to display an arbitrary name on the menu bar on Mac
Add the date to the GC statistics acquired by gcutil and output it.
How to change the maximum and maximum number of POST data in Spark
How to change the value of a variable at a breakpoint in intelliJ
How to make the schema of the URL generated by Rails URL helper https
[swift5] How to change the color of TabBar or the color of item of TabBar with code
How to prevent duplicate processing by addEventListener
[Java] How to use the File class
How to delete the wrong migration file
[Java] How to use the hasNext function
How to name variables 7 selections of discomfort
How to put out the error bundling
[Java] How to use the HashMap class
How to delete the migration file NO FILE
[Rails] How to use the map method