Spring Boot Mybatis SQL Snake Case Column and Camel Case Mapping

If the SQL column name is Snake case and the mapping destination java is Camel case, Settings that automatically convert from camel to snake with mybatis.

Conclusion

Method 1

I use this.

application.yml


mybatis:
  configuration:
    map-underscore-to-camel-case: true

Method 2

application.properties


mybatis.configuration.map-underscore-to-camel-case=true

Method 3

application.yml


mybatis:
  config-location: classpath:/mybatis/mybatis-config.xml

mybatis-config.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

Reference implementation

Todo.java


class Todo {
    private Integer id;
    private String statusNm; //Status in this field_I want to enter the value of nm
}

TodoListMapper.java


@Mapper
public interface TodoListMapper {
    /**
     *Search the list.
     * @return TODO list
     */
    List<Todo> selectList();
}

TodoListMapper.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hellospring.mapper.TodoListMapper">
  <select id="selectList" resultType="com.hellospring.domain.Todo">
    SELECT
      id
     ,status_nm
    FROM
      todo
  </select>
</mapper>

Recommended Posts

Spring Boot Mybatis SQL Snake Case Column and Camel Case Mapping
Spring Boot Servlet mapping
Spring profile function, and Spring Boot application.properties
I got stuck using snake case for variable name in Spring Boot
[Java] Mutual conversion of notations such as camel case and snake case [Jackson]
Implementation method for multi-data source with Spring boot (Mybatis and Spring Data JPA)
HTTPS with Spring Boot and Let's Encrypt
Add spring boot and gradle to eclipse