IT'S DO
article thumbnail
728x90

예를 들면 Mybatis-config.xml을 안쓰면 이런 결과가 나타남.

 

이유는 

# Dto에서 카멜케이스로 만들어서 그럼.

import lombok.Data;

@Data
public class BoardDto {

    private int boardIdx;

    private String title;

    private String contents;

    private int hitCnt;

    private String creatorId;

    private String updaterId;

}

 

물론 여기서 boardIdx를 -> board_idx로 바꾸면 값을 불러와짐. db에서는 주로 다음 문자에선 _바를 붙이는데, 자바 코드에서 까지 _붙여서 사용하면 비효율적임.

 

이럴 때 사용 하는 것이  Mybatis-config.xml이다.

 

# Mybatis-config.xml setting

 

1. apllication.yml

mybatis:
  mapper-locations: classpath:mapper/*.xml
  config-location: classpath:mybatis-config.xml

 

2. resource/mybatis-config.xml

<!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. mapper

<?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.example.dmf.mapper.BoardMapper">
    <select id="selectBoardList" resultType="com.example.dmf.board.dto.BoardDto">

        SELECT
            board_idx,
            title,
            hit_cnt
        FROM
            t_board
        WHERE
            delete_yn = 'N'
        ORDER BY board_idx DESC

  </select>
</mapper>

 

을 해주면 

값이 정상적으로 

잘 불러와짐.

 

 

#  Mybatis-config.xml 팁

 

mapper에서 

<select id="selectBoardList" resultType="com.example.dmf.board.dto.BoardDto">

이렇게 된 것을 좀더 깔끔하게 쓰는법

 

1. mybatis-config.xml

<typeAliases>
    <typeAlias type="com.example.dmf.board.dto.BoardDto" alias="BoardDto"/>
</typeAliases>

넣어주기 

 

2. mapper

<select id="selectBoardList" resultType="BoardDto">

로 변경

 

아래와 같이 같음. (same sample)

profile

IT'S DO

@멋진놈

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!