Search

typeAliases

typeAliases

ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๋ณ„์นญ์„ ์ •์˜ํ•˜์—ฌ XML์—์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

typeAliases ์„ค์ • ๋ฐฉ๋ฒ•

mybatis ์—์„œ Java ํด๋ž˜์Šค ์ „์ฒด์ด๋ฆ„(ํŒจํ‚ค์ง€๋ช….ํด๋ž˜์Šค๋ช…)์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜ ๋ฐฉ๋ฒ• ์ค‘์— ํ•˜๋‚˜๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
mybatis-config.xml
โ€ข
application.properties
โ€ข
@Alias ์–ด๋…ธํ…Œ์ด์…˜

mybatis-config.xml

โ€ข
์„ค์ • ํŒŒ์ผ ์œ„์น˜
โ—ฆ
~/src/main/resources
โ–ช
mybatis-config.xml
โ€ข
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- ์„ค์ • --> <settings> <!-- ์–ธ๋”์Šค์ฝ”์–ด ์ผ€์ด์Šค์ธ ์ปฌ๋Ÿผ์„ ์นด๋ฉœ ์ผ€์ด์Šค๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์„ค์ • --> <!-- board_no - boardNo --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- ํƒ€์ž… ๋ณ„์นญ ์„ค์ • --> <typeAliases> <!-- โญ typeAlias : ๋ณ„์นญ ์„ค์ • - alias : ๋ณ„์นญ - type : ๋ณ„์นญ์„ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค ๊ฒฝ๋กœ --> <typeAlias alias="User" type="com.aloha.mybatis.dto.User"/> <!-- โญ package : ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ ์ง€์ • - name : ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ --> <package name="com.aloha.mybatis.dto"/> </typeAliases> </configuration>
XML
๋ณต์‚ฌ
โ€ข
application.properties
# Mybatis ์„ค์ • # Mybatis ์„ค์ • ๊ฒฝ๋กœ : ~/resources/mybatis-config.xml mybatis.config-location=classpath:mybatis-config.xml # Mybatis ๋งคํผ ํŒŒ์ผ ๊ฒฝ๋กœ : ~/๋ฉ”์ธํŒจํ‚ค์ง€/mapper/**Mapper.xml mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
Java
๋ณต์‚ฌ
ํƒ€์ž… ๋ณ„์นญ package ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•˜๋ฉด, ํ•ด๋‹น ํŒจํ‚ค์ง€ ์•„๋ž˜์˜ ๋ชจ๋“  ํด๋ž˜์Šค๋“ค์„ ํด๋ž˜์Šค ์ด๋ฆ„๋งŒ์œผ๋กœ type ์„ ์ง€์ •ํ• ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
typeAliases - package ์„ค์ •
<resultMap id="๋งตid" type="ํŒจํ‚ค์ง€๋ช….ํด๋ž˜์Šค๋ช…"> </resultMap>
XML
๋ณต์‚ฌ
โ€ข
typeAliases - package ์„ค์ •
<!-- <resultMap id="๋งตid" type="ํŒจํ‚ค์ง€๋ช….ํด๋ž˜์Šค๋ช…"> --> <resultMap id="๋งตid" type="ํด๋ž˜์Šค๋ช…"> </resultMap>
XML
๋ณต์‚ฌ
mybatis-config.xml ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ์„ค์ •ํŒŒ์ผ์„ ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ ,
application.properties ์˜ ์†์„ฑ์œผ๋กœ ๋ฐ”๋กœ ์ ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
# Mybatis ์„ค์ • # ์ปฌ๋Ÿผ๋ช…์„ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์ž๋™๋งคํ•‘ : board_no โžก boardNo mybatis.configuration.map-underscore-to-camel-case=true # resultType ์†์„ฑ์—์„œ dto ๊ฐ์ฒด (ํŒจํ‚ค์ง€.ํด๋ž˜์Šค๋ช…) โžก (ํด๋ž˜์Šค๋ช…) mybatis.type-aliases-package=com.aloha.mybatis.dto # Mybatis ๋งคํผ ํŒŒ์ผ ๊ฒฝ๋กœ : ~/๋ฉ”์ธํŒจํ‚ค์ง€/mapper/**Mapper.xml mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
Java
๋ณต์‚ฌ

@Alias ์–ด๋…ธํ…Œ์ด์…˜

mybatis-config.xml ๋˜๋Š” application properties ์— MyBatis๊ฐ€ ์ง€์ •๋œ ํŒจํ‚ค์ง€ ๋‚ด์˜ ํด๋ž˜์Šค์— ๋Œ€ํ•ด ํƒ€์ž… ๋ณ„์นญ์„ ์ž๋™์œผ๋กœ ์Šค์บ”ํ•˜๋„๋ก ์„ค์ •ํ•ด์•ผ @Alias ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋งคํ•‘ํ•  ๊ฐ ๊ฐ์ฒด์—์„œ, @Alias ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋ณ„์นญ์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•
package com.aloha.mybaits.dto; ... @Data @Alias("Users") public class Users { private Long no; private String username; private String password; private String name; private String email; private Date createdAt; private Date updatedAt; private int enabled; }
Java
๋ณต์‚ฌ
โ€ข
@Alias("Users") ์„ค์ •
<!-- โญ typeAliases ์„ค์ •ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ - ํŒจํ‚ค์ง€๋ช….ํด๋ž˜์Šค๋ช… --> <select id="select" resultType="com.aloha.mybatis.dto.Users"> SELECT * FROM user WHERE username = #{username} </select>
XML
๋ณต์‚ฌ
โ€ข
@Alias("Users") ์„ค์ •
<!-- โญ typeAliases ์„ค์ •ํ•œ ๊ฒฝ์šฐ - ๋ณ„์นญ (Users) --> <select id="select" resultType="Users"> SELECT * FROM user WHERE username = #{username} </select>
XML
๋ณต์‚ฌ