interface Mapper

woomΒ·2023λ…„ 2μ›” 7일
0

Framework

λͺ©λ‘ 보기
5/20
post-thumbnail

🌼 interface Mapper

  • mybatis ν”„λ ˆμž„μ›Œν¬λŠ” μΈν„°νŽ˜μ΄μŠ€λ§Œ μ΄μš©ν•˜μ—¬ 맀퍼 μ„€μ • κ°€λŠ₯

    • 맀핑 정보에 λŒ€ν•œ μž¬μ‚¬μš© λΆˆκ°€λŠ₯ν•˜μ—¬ μœ μ§€λ³΄μˆ˜μ˜ νš¨μœ¨μ„± κ°μ†Œ β†’ xml맀퍼와 λ°”μΈλ”©ν•΄μ„œ μ‚¬μš©ν•˜λŠ” 이유
  • μΆ”μƒλ©”μ†Œλ“œμ— 맀퍼 μ–΄λ…Έν…Œμ΄μ…˜(Mapper Annotation)을 μ‚¬μš©ν•˜μ—¬ SQL λͺ…λ Ή 등둝


πŸ“• DTO class

package xyz.itwill.dto;

public class MyHewon {
	private String id;
	private String name;
	private String phone;
	private String email;
	private int state;
	
	public MyHewon() {
		// TODO Auto-generated constructor stub
	}

	public MyHewon(String id, String name, String phone, String email, int state) {
		super();
		this.id = id;
		this.name = name;
		this.phone = phone;
		this.email = email;
		this.state = state;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}
}





πŸ“™ interface 맀퍼

  • μΆ”μƒλ©”μ†Œλ“œμ— λ“±λ‘λœ SELECT λͺ…령은 기본적으둜 검색결과λ₯Ό μžλ™ λ§€ν•‘μ²˜λ¦¬ν•˜μ—¬ 제곡

    • 검색결과λ₯Ό μžλ™ λ§€ν•‘ν•˜κΈ° μœ„ν•΄μ„œλŠ” κ²€μƒ‰ν–‰μ˜ 컬럼λͺ…κ³Ό Java 클래슀의 ν•„λ“œλͺ…이 λ°˜λ“œμ‹œ 동일
    • κ²€μƒ‰ν–‰μ˜ 컬럼λͺ…κ³Ό Java 클래슀의 ν•„λ“œλͺ…이 ν•˜λ‚˜λ„ 같지 μ•ŠμœΌλ©΄ Java 객체 λŒ€μ‹  NULL 제곡
  • @Results : κ²€μƒ‰ν–‰μ˜ μ»¬λŸΌκ°’μ„ Java 객체의 ν•„λ“œκ°’μœΌλ‘œ μ €μž₯λ˜λ„λ‘ 맀핑 μ²˜λ¦¬ν•˜λŠ” 정보λ₯Ό μ œκ³΅ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜

    • XML 기반의 맀퍼 νŒŒμΌμ—μ„œ resultMap μ—˜λ¦¬λ¨ΌνŠΈμ™€ μœ μ‚¬ν•œ κΈ°λŠ₯을 제곡
    • 맀핑 정보에 λŒ€ν•œ μž¬μ‚¬μš© λΆˆκ°€λŠ₯ (μœ μ§€λ³΄μˆ˜μ˜ νš¨μœ¨μ„± κ°μ†Œ)
    • value 속성 : 맀핑정보λ₯Ό μ œκ³΅ν•˜λŠ” @Result μ–΄λ…Έν…Œμ΄μ…˜ 배열을 μ†μ„±κ°’μœΌλ‘œ μ„€μ • (λ‹€λ₯Έ 속성이 μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯)
    • @Results(value = { }) or @Results({ })
  • @Result : κ²€μƒ‰ν–‰μ˜ μ»¬λŸΌκ°’μ΄ Java 객체 ν•„λ“œμ— μ €μž₯λ˜λ„λ‘ μ„€μ •ν•˜λŠ” μ—˜λ¦¬λ¨ΌνŠΈ

    • XML 기반의 맀퍼 νŒŒμΌμ—μ„œ id(result) μ—˜λ¦¬λ¨ΌνŠΈμ™€ μœ μ‚¬ν•œ κΈ°λŠ₯을 제곡
    • column 속성 : κ²€μƒ‰ν–‰μ˜ 컬럼λͺ…을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • property 속성 : κ²€μƒ‰ν–‰μ˜ μ»¬λŸΌκ°’μ΄ μ €μž₯될 Java 클래슀의 ν•„λ“œλͺ…을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • @Result(column = "hewon_id", property = "id")
  • @SelectProvider : SQL Builder 클래슀의 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ SELECT λͺ…령을 λ°˜ν™˜λ°›μ•„ λ“±λ‘ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜

    • 동적 SQL κΈ°λŠ₯을 μ‚¬μš©ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
    • SQL Builder 클래슀 : SQL 객체λ₯Ό μƒμ„±ν•˜μ—¬ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•΄ SQL λͺ…령을 μž‘μ„±ν•˜κ³  객체에 μ €μž₯된 SQL λͺ…령을 λ°˜ν™˜ν•˜λŠ” κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” 클래슀
    • type 속성 : SQL Builder κ΄€λ ¨ 클래슀(Class 객체 - Clazz)λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • method 속성 : SQL 객체λ₯Ό μƒμ„±ν•˜μ—¬ SQL λͺ…령을 μž‘μ„±ν•΄ λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œμ˜ 이름을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
    • @SelectProvider(type = MyHewonProvider.class, method = "selectDynamicName")
  • @Select μ–΄λ…Έν…Œμ΄μ…˜μ˜ value μ†μ„±κ°’μœΌλ‘œ script μ—˜λ¦¬λ¨ΌνŠΈλ₯Ό μ‚¬μš©ν•˜λ©΄ SQL λͺ…λ Ή μž‘μ„±μ‹œ 동적 SQL κ΄€λ ¨ μ—˜λ¦¬λ¨ΌνŠΈ μ‚¬μš© κ°€λŠ₯
    • value μ†μ„±κ°’μœΌλ‘œ 배열을 μ„€μ •ν•˜μ—¬ SQL λͺ…령을 순차적으둜 μž‘μ„± κ°€λŠ₯

package xyz.itwill.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import xyz.itwill.dto.MyHewon;

//mybatis ν”„λ ˆμž„μ›Œν¬λŠ” μΈν„°νŽ˜μ΄μŠ€λ§Œ μ΄μš©ν•˜μ—¬ 맀퍼 μ„€μ • κ°€λŠ₯
// => μΆ”μƒλ©”μ†Œλ“œμ— 맀퍼 μ–΄λ…Έν…Œμ΄μ…˜(Mapper Annotation)을 μ‚¬μš©ν•˜μ—¬ SQL λͺ…λ Ή 등둝
public interface MyHewonInterfaceMapper {
	//μΆ”μƒλ©”μ†Œλ“œμ— λ“±λ‘λœ SELECT λͺ…령은 기본적으둜 검색결과λ₯Ό μžλ™ λ§€ν•‘μ²˜λ¦¬ν•˜μ—¬ 제곡
	// => 검색결과λ₯Ό μžλ™ λ§€ν•‘ν•˜κΈ° μœ„ν•΄μ„œλŠ” κ²€μƒ‰ν–‰μ˜ 컬럼λͺ…κ³Ό Java 클래슀의 ν•„λ“œλͺ…이 λ°˜λ“œμ‹œ 동일
	// => κ²€μƒ‰ν–‰μ˜ 컬럼λͺ…κ³Ό Java 클래슀의 ν•„λ“œλͺ…이 ν•˜λ‚˜λ„ 같지 μ•ŠμœΌλ©΄ Java 객체 λŒ€μ‹  NULL 제곡
	//@Results : κ²€μƒ‰ν–‰μ˜ μ»¬λŸΌκ°’μ„ Java 객체의 ν•„λ“œκ°’μœΌλ‘œ μ €μž₯λ˜λ„λ‘ 맀핑 μ²˜λ¦¬ν•˜λŠ” 정보λ₯Ό μ œκ³΅ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜
	// => XML 기반의 맀퍼 νŒŒμΌμ—μ„œ resultMap μ—˜λ¦¬λ¨ΌνŠΈμ™€ μœ μ‚¬ν•œ κΈ°λŠ₯을 제곡
	// => 맀핑 정보에 λŒ€ν•œ μž¬μ‚¬μš© λΆˆκ°€λŠ₯ - μœ μ§€λ³΄μˆ˜μ˜ νš¨μœ¨μ„± κ°μ†Œ
	//value 속성 : 맀핑정보λ₯Ό μ œκ³΅ν•˜λŠ” @Result μ–΄λ…Έν…Œμ΄μ…˜ 배열을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
	// => λ‹€λ₯Έ 속성이 μ—†λŠ” 경우 μ†μ„±κ°’λ§Œ μ„€μ • κ°€λŠ₯
	@Results(value = {
		//@Result : κ²€μƒ‰ν–‰μ˜ μ»¬λŸΌκ°’μ΄ Java 객체 ν•„λ“œμ— μ €μž₯λ˜λ„λ‘ μ„€μ •ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜
		// => XML 기반의 맀퍼 νŒŒμΌμ—μ„œ id(result) μ—˜λ¦¬λ¨ΌνŠΈμ™€ μœ μ‚¬ν•œ κΈ°λŠ₯을 제곡
		//column 속성 : κ²€μƒ‰ν–‰μ˜ 컬럼λͺ…을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
		//property 속성 : κ²€μƒ‰ν–‰μ˜ μ»¬λŸΌκ°’μ΄ μ €μž₯될 Java 클래슀의 ν•„λ“œλͺ…을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
		@Result(column = "hewon_id", property = "id")
		,@Result(column = "hewon_name", property = "name")
		,@Result(column = "hewon_phone", property = "phone")
		,@Result(column = "hewon_email", property = "email")
		,@Result(column = "hewon_state", property = "state")
	})
	@Select(value = "select * from myhewon order by hewon_id")
	List<MyHewon> selectHewonList();

	@Results({
		@Result(column = "hewon_id", property = "id")
		,@Result(column = "hewon_name", property = "name")
		,@Result(column = "hewon_phone", property = "phone")
		,@Result(column = "hewon_email", property = "email")
		,@Result(column = "hewon_state", property = "state")
	})
	@Select("select * from myhewon where hewon_name=#{name} order by hewon_id")
	List<MyHewon> selectNameHewonList(String name);
	
	/*
	@Results({
		@Result(column = "hewon_id", property = "id")
		,@Result(column = "hewon_name", property = "name")
		,@Result(column = "hewon_phone", property = "phone")
		,@Result(column = "hewon_email", property = "email")
		,@Result(column = "hewon_state", property = "state")
	})
	//@SelectProvider : SQL Builder 클래슀의 λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ SELECT λͺ…령을 λ°˜ν™˜λ°›μ•„ λ“±λ‘ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜
	// => 동적 SQL κΈ°λŠ₯을 μ‚¬μš©ν•˜κΈ° μœ„ν•œ μ–΄λ…Έν…Œμ΄μ…˜
	//SQL Builder 클래슀 : SQL 객체λ₯Ό μƒμ„±ν•˜μ—¬ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•΄ SQL λͺ…령을 μž‘μ„±ν•˜κ³  객체에 μ €μž₯된 
	//SQL λͺ…령을 λ°˜ν™˜ν•˜λŠ” κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” 클래슀
	//type 속성 : SQL Builder κ΄€λ ¨ 클래슀(Class 객체 - Clazz)λ₯Ό μ†μ„±κ°’μœΌλ‘œ μ„€μ •
	//method 속성 : SQL 객체λ₯Ό μƒμ„±ν•˜μ—¬ SQL λͺ…령을 μž‘μ„±ν•΄ λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œμ˜ 이름을 μ†μ„±κ°’μœΌλ‘œ μ„€μ •
	@SelectProvider(type = MyHewonProvider.class, method = "selectDynamicName")
	List<MyHewon> selectDynamicHewonList(String name);
	*/
	
	@Results({
		@Result(column = "hewon_id", property = "id")
		,@Result(column = "hewon_name", property = "name")
		,@Result(column = "hewon_phone", property = "phone")
		,@Result(column = "hewon_email", property = "email")
		,@Result(column = "hewon_state", property = "state")
	})
	//@Select μ–΄λ…Έν…Œμ΄μ…˜μ˜ value μ†μ„±κ°’μœΌλ‘œ script μ—˜λ¦¬λ¨ΌνŠΈλ₯Ό μ‚¬μš©ν•˜λ©΄ SQL λͺ…λ Ή μž‘μ„±μ‹œ 동적 
	//SQL κ΄€λ ¨ μ—˜λ¦¬λ¨ΌνŠΈ μ‚¬μš© κ°€λŠ₯
	// => value μ†μ„±κ°’μœΌλ‘œ 배열을 μ„€μ •ν•˜μ—¬ SQL λͺ…령을 순차적으둜 μž‘μ„± κ°€λŠ₯ 
	@Select({"<script>select * from myhewon"
			," <if test=\"name!=null and name!=''\">where hewon_name=#{name}</if>"
			," order by hewon_id</script>"})
	List<MyHewon> selectDynamicHewonList(String name);
}
      
      
      
      
      

πŸ“Œ SQL Builder 클래슀

  • Provider 클래슀(SQL Builder 클래슀) : SQL λͺ…령을 λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œκ°€ μ„ μ–Έλœ 클래슀

    • Java λͺ…λ ΉμœΌλ‘œ 동적 SQL κΈ°λŠ₯이 κ΅¬ν˜„λœ SQL λͺ…령을 λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œ
  • SQL 객체 : SQL λͺ…λ Ή μž‘μ„±μ— ν•„μš”ν•œ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ SQL λͺ…령을 μ €μž₯ν•˜κΈ° μœ„ν•œ 객체

    • SQL.toString() : SQL 객체에 μ €μž₯된 SQL λͺ…령을 λ¬Έμžμ—΄(String 객체)둜 λ³€ν™˜ν•˜μ—¬ λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œ

package xyz.itwill.mapper;

import org.apache.ibatis.jdbc.SQL;

public class MyHewonProvider {
	public String selectDynamicName(String name) {
		return new SQL() {{
			SELECT("*");
			FROM("myhewon");
			if(name!=null && !name.equals("")) {
				WHERE("hewon_name=#{name}");
			}
			ORDER_BY("hewon_id");
		}}.toString();
	}
}





πŸ“’ DAO class

  1. selectHewonList() : 전체 νšŒμ› 정보 λͺ©λ‘ 좜λ ₯

  2. selectNameHewonList(String name) : 이름을 κ²€μƒ‰ν•˜μ—¬ ν•΄λ‹Ή νšŒμ›μ •λ³΄ 좜λ ₯

  3. selectNameHewonList(String name) : SQL Builder 클래슀λ₯Ό μ‚¬μš©ν•˜μ—¬ νšŒμ›μ •λ³΄ 검색

  4. selectDynamicHewonList(String name) : @Select μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•œ SQL λͺ…령을 μž‘μ„±ν•˜μ—¬ νšŒμ›μ •λ³΄ 검색


package xyz.itwill.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import xyz.itwill.dto.MyHewon;
import xyz.itwill.mapper.MyHewonInterfaceMapper;

public class MyHewonInterfaceDAO extends AbstractSession {
	private static MyHewonInterfaceDAO _dao;
	
	private MyHewonInterfaceDAO() {
		// TODO Auto-generated constructor stub
	}
	
	static {
		_dao=new MyHewonInterfaceDAO();
	}
	
	public static MyHewonInterfaceDAO getDAO() {
		return _dao;
	}
	
	public List<MyHewon> selectHewonList() {
		SqlSession sqlSession=getSqlSessionFactory().openSession(true);
		try {
			return sqlSession.getMapper(MyHewonInterfaceMapper.class).selectHewonList();
		} finally {
			sqlSession.close();
		}
	}
	
	public List<MyHewon> selectNameHewonList(String name) {
		SqlSession sqlSession=getSqlSessionFactory().openSession(true);
		try {
			return sqlSession.getMapper(MyHewonInterfaceMapper.class).selectNameHewonList(name);
		} finally {
			sqlSession.close();
		}
	}
	
	public List<MyHewon> selectDynamicHewonList(String name) {
		SqlSession sqlSession=getSqlSessionFactory().openSession(true);
		try {
			return sqlSession.getMapper(MyHewonInterfaceMapper.class).selectDynamicHewonList(name);
		} finally {
			sqlSession.close();
		}
	}
}





🐣 νšŒμ›μ •λ³΄ 검색

  1. selectHewonList() : 전체 νšŒμ› 정보 λͺ©λ‘ 좜λ ₯

  2. selectNameHewonList(String name) : 이름을 κ²€μƒ‰ν•˜μ—¬ ν•΄λ‹Ή νšŒμ›μ •λ³΄ 좜λ ₯

  3. selectNameHewonList(String name) : SQL Builder 클래슀λ₯Ό μ‚¬μš©ν•˜μ—¬ νšŒμ›μ •λ³΄ 검색

  4. selectDynamicHewonList(String name) : @Select μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•œ SQL λͺ…령을 μž‘μ„±ν•˜μ—¬ νšŒμ›μ •λ³΄ 검색

<%@page import="xyz.itwill.dao.MyHewonInterfaceDAO"%>
<%@page import="xyz.itwill.dto.MyHewon"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("utf-8");
	String name=request.getParameter("name");
	
	/*
	List<MyHewon> hewonList=null;
	if(name==null || name.equals("")) {
		hewonList=MyHewonInterfaceDAO.getDAO().selectHewonList();
	} else {
		hewonList=MyHewonInterfaceDAO.getDAO().selectNameHewonList(name);
	}
	*/
	
	List<MyHewon> hewonList=MyHewonInterfaceDAO.getDAO().selectDynamicHewonList(name);
%>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MYBATIS</title>
<style type="text/css">
table {
	border: 1px solid black;
	border-collapse: collapse;
}
td {
	border: 1px solid black;
	text-align: center;
	padding: 3px;
}
.id { width: 150px; }
.name { width: 150px; }
.phone { width: 200px; }
.email { width: 200px; }
.state { width: 100px; }
</style>
</head>
<body>
	<h1>νšŒμ›λͺ©λ‘</h1>
	<hr>
	<table>
		<tr>
			<td class="id">아이디</td>
			<td class="name">이름</td>
			<td class="phone">μ „ν™”λ²ˆν˜Έ</td>
			<td class="email">이메일</td>
			<td class="state">κ³΅κ°œλ²”μœ„</td>
		</tr>	
		<% if(hewonList.isEmpty()) { %>
		<tr>
			<td colspan="5">κ²€μƒ‰λœ νšŒμ›μ •λ³΄κ°€ μ—†μŠ΅λ‹ˆλ‹€.</td>
		</tr>
		<% } else { %>
			<% for(MyHewon hewon:hewonList) { %>
			<tr>
				<td><%=hewon.getId() %></td>
				<td><%=hewon.getName() %></td>
				<td><%=hewon.getPhone() %></td>
				<td><%=hewon.getEmail() %></td>
				<td><%=hewon.getState() %></td>
			</tr>
			<% } %>
		<% } %>
	</table>
	<br>
	
	<form method="post">
		이름 : <input type="text" name="name">
		<button type="submit">검색</button>
	</form>
</body>
</html>





profile
Study Log πŸ“‚

0개의 λŒ“κΈ€