1. 개요
DB 컬럼명에 _ (Underscore)가 포함되어 있을 때 이를 자동으로 카멜 표기법의 DTO와 매핑해주기 위해 설정을 추가한다.
예: USER_NAME 컬럼
-> getUserName();
2. mapUnderscoreToCamelCase 설정
DB 컬럼명에 _ (Underscore)가 포함되어 있을 때 이를 자동으로 카멜 표기법의 DTO와 매핑해준다. 만약 이 설정이 없으면 _ (Undersocre)가 포함된 컬럼의 값은 null로 리턴된다.
3. Spring Boot에서 mapUnderscoreToCamelCase 설정
3.1. resource 하위에 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>
3.2. SessionFactory
@Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/**/*.xml")); Resource myBatisConfig = new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"); sessionFactory.setConfigLocation(myBatisConfig); return sessionFactory.getObject(); }
원래 코드에 다음 부분이 추가된 것이다.
Resource myBatisConfig = new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"); sessionFactory.setConfigLocation(myBatisConfig);
사실 구글링 결과로는 따로 config 파일 빼서 처리하지 않고 아래를 추가해도 해도 된다고 하는데 뭘 잘못했는지 안됐다.
Properties properties = new Properties(); properties.put("mapUnderscoreToCamelCase", true); sessionFactory.setConfigurationProperties(properties);