Mybatis
λ§μ΄λ°ν°μ€λ κ°λ°μκ° μ§μ ν SQL, μ μ₯νλ‘μμ κ·Έλ¦¬κ³ λͺκ°μ§ κ³ κΈ λ§€νμ μ§μνλ νΌμμ€ν΄μ€ νλ μμν¬μ΄λ€.
Mybatis 곡μ μ¬μ΄νΈ
νΌμμ€ν΄μ€ νλ μμν¬
νΌμμ€ν΄μ€ νλ μμν¬λ λ°μ΄ν°λ² μ΄μ€μμ μνΈμμ©μ λ¨μννκ³ μΆμννλ μννΈμ¨μ΄ λꡬμ
λλ€. μ΄ λꡬλ₯Ό μ¬μ©νλ©΄ κ°λ°μλ SQL 쿼리λ₯Ό μ§μ μμ±νλ λμ κ°μ²΄ μ§ν₯μ μΈ λ°©μμΌλ‘ λ°μ΄ν°λ² μ΄μ€μ μ κ·Όν μ μμ΅λλ€. Mybatisλ κ°λ°μκ° μ§μ ν SQL, μ μ₯νλ‘μμ , κ·Έλ¦¬κ³ λͺ κ°μ§ κ³ κΈ λ§€νμ μ§μνλ νΌμμ€ν΄μ€ νλ μμν¬μ
λλ€.
SQL Mapper
Mybatisλ SQL Mapper ννλ‘ λμν©λλ€. κ°λ°μλ SQL λ¬Έμ₯μ΄λ μ μ₯νλ‘μμ λ₯Ό μ§μ μμ±νκ³ , Mybatisλ μ΄λ₯Ό λ°μ΄ν°λ² μ΄μ€μ μ λ¬νκ³ κ²°κ³Όλ₯Ό 맀ννμ¬ λ°νν©λλ€. μ΄λ¬ν λ°©μμ ν΅ν΄ κ°λ°μλ μ§μ μ μΈ SQL μμ±μ λν μ μ°μ±κ³Ό μ μ΄λ ₯μ κ°μ§λ©΄μλ, κ°μ²΄ μ§ν₯μ μΈ λ°©μμΌλ‘ λ°μ΄ν°λ² μ΄μ€μ μνΈμμ©ν μ μμ΅λλ€.
Mybatis vs JPA
Mybatisμ JPAλ λͺ¨λ νΌμμ€ν΄μ€ νλ μμν¬μ΄μ§λ§, λ€λ₯Έ μ κ·Ό λ°©μκ³Ό μ² νμ κ°μ§κ³ μμ΅λλ€. JPAλ κ°μ²΄-κ΄κ³ 맀νμ μ€μ¬μΌλ‘ ν ORM(Object-Relational Mapping) νλ μμν¬μ΄λ©°, κ°λ°μκ° κ°μ²΄λ₯Ό ν΅ν΄ λ°μ΄ν°λ² μ΄μ€μ μνΈμμ©ν©λλ€. λ°λ©΄ Mybatisλ SQL Mapperλ‘μ μ§μ μ μΈ SQL μμ±κ³Ό 맀νμ μ€μ¬μΌλ‘ ν νλ μμν¬μ
λλ€. μ΄λ€ νλ μμν¬λ₯Ό μ νν μ§λ κ°λ°μμ μ νΈλμ νλ‘μ νΈ μꡬμ¬νμ λ°λΌ κ²°μ λ©λλ€.
Spring Framework μ Mybatis μ°λ
Mybatisλ₯Ό Spring Frameworkμ μ°λνλ λ°©λ²μ λ€μκ³Ό κ°μ΅λλ€:
1.
Mybatis μμ‘΄μ± μ€μ
2.
Mybatis μ€μ λ° μ€μ νμΌ μμ±
3.
SqlSessionFactoryBeanκ³Ό SqlSessionTemplate λΉ λ±λ‘
4.
Mapper μΈν°νμ΄μ€ ν¨ν€μ§ μ€μΊ κ²½λ‘ μ€μ
5.
Mapper.xml 맀ν νμΌ μμ±
6.
Mapper μΈν°νμ΄μ€ μμ±
7.
λΉμ¦λμ€ λ‘μ§ κ΅¬ν
Mybatis μμ‘΄μ± μ€μ
β’
pom.xml
β¦
mybatis
β¦
mybatis-spring
β¦
spring-jdbc
β¦
mysql-connector-java
β’
<!-- mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
XML
볡μ¬
<!-- mysql λλΌμ΄λ² -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
</dependency>
<!-- Spring JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
XML
볡μ¬
β’
lombok
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
XML
볡μ¬
Mybatis μ€μ λ° μ€μ νμΌ
Mybatis λ€μ μ€νμ΄μ€
β’
root-context.xml
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
XML
볡μ¬
Mybatis μ€ν€λ§ μ€μ
β’
root-context.xml
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
XML
볡μ¬
SqlSessionFactoryBean, SqlSessionTemplate λΉ λ±λ‘
β’
root-context.xml
<!-- DriverManagerDataSource λΉ λ±λ‘ -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/joeun"></property>
<property name="username" value="joeun"></property>
<property name="password" value="123456"></property>
</bean>
<!-- SqlSessionFactoryBean λΉ λ±λ‘ -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
</bean>
<!-- MyBatis SqlSessionTemplate λΉ λ±λ‘ -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
XML
볡μ¬
Mapper μΈν°νμ΄μ€ ν¨ν€μ§ μ€μΊ κ²½λ‘ μ€μ
β’
root-context.xml
<!-- Mapper μΈν°νμ΄μ€ ν¨ν€μ§ κ²½λ‘ μ€μ -->
<mybatis-spring:scan base-package="com.aloha.spring.mapper" />
XML
볡μ¬
Mybatis μ€μ νμΌ
β’
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>
<!-- μΈλμ€μ½μ΄ μΌμ΄μ€μΈ 컬λΌμ μΉ΄λ© μΌμ΄μ€λ‘ λ³ννλ μ€μ -->
<!-- no - no -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- νμ
λ³μΉ μ€μ -->
<typeAliases>
<!-- ν¨ν€μ§λΆν°μ μ 체경λ‘λ₯Ό κ°λ μλ° νμ
μ μΆμ½ν΄μ λ³μΉμΌλ‘ μ§μ -->
<!--
<typeAlias alias="Board" type="com.aloha.spring.dto.Board"/>
<typeAlias alias="User" type="com.aloha.spring.dto.User"/>
<typeAlias alias="Comment" type="com.aloha.spring.dto.Comment"/>
-->
<!-- ν
μ΄λΈκ³Ό 맀νν DTOκ° μλ ν¨ν€μ§ κ²½λ‘ μ§μ -->
<package name="com.aloha.spring.dto"/>
</typeAliases>
</configuration>
XML
볡μ¬
Mapper.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">
<!-- namespace="λ§€νΌ μΈν°νμ΄μ€ κ²½λ‘" -->
<mapper namespace="com.aloha.spring.mapper.BoardMapper">
</mapper>
HTML
볡μ¬
β’
BoardMapper.xml
β¦
ν¨ν€μ§ κ΅¬μ± : μ΅μμ ν¨ν€μ§(top level package)μ mapper ν¨ν€μ§μ μΌμΉ
β¦
~/java
βͺ
com.aloha.spring.mapper
β¦
~/resources
βͺ
com.aloha.spring.mapper
β’
BoardMapper.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">
<!-- namespace="λ§€νΌ μΈν°νμ΄μ€ κ²½λ‘" -->
<mapper namespace="com.aloha.spring.mapper.BoardMapper">
<!-- sql νκ·Έ μμ± -->
<!-- κ²μκΈ λͺ©λ‘ -->
<!-- id : μ°κ²°ν λ§€νΌ μΈν°νμ΄μ€μ λ©μλλͺ
-->
<!-- resultType : μ‘°ν κ²°κ³Όλ₯Ό 맀νν ν΄λμ€ νμ
(ν¨ν€μ§κ²½λ‘+ν΄λμ€λͺ
) -->
<select id="list" resultType="com.aloha.spring.dto.Board">
SELECT no 'no'
,title
,writer
,content
,reg_date 'regDate'
,upd_date 'updDate'
,views
FROM board
</select>
<!-- κ²μκΈ μ‘°ν -->
<!-- 맀ν νλΌλ―Έν° : #{νλΌλ―Έν°λͺ
} -->
<!-- #{} νλΌλ―Έν° κ°μκ° 1κ°λΌλ©΄, μ΄λ¦κ³Ό 무κ΄νκ² λ§€νλλ€. -->
<select id="select" resultType="com.aloha.spring.dto.Board">
SELECT no 'no'
,title
,writer
,content
,reg_date 'regDate'
,upd_date 'updDate'
,views
FROM board
WHERE no = #{no}
</select>
<!-- κ²μκΈ λ±λ‘ -->
<!-- Mapper μΈν°νμ΄μ€μ λκ²¨μ€ νλΌλ―Έν°κ° μ¬λ¬ κ°λΌλ©΄ κ°μ²΄λ‘ 맀νν μ μλ€. -->
<!-- 맀κ°λ³μλ‘ Board ν΄λμ€λ₯Ό μ§μ νλ©΄ λ³μλ€μ΄ μλμΌλ‘ #{λ³μ} νλΌλ―Έν°μ 맀νλλ€. -->
<!-- #{index} : νλΌλ―Έν°λ₯Ό μμλλ‘ index λ‘ λ§€ννμ¬ μ¬μ©ν μλ μλ€. -->
<insert id="insert">
INSERT INTO board ( title, writer, content )
VALUES ( #{title}, #{writer}, #{content} )
<!-- (indexλ‘ λ§€ννλ κ²½μ°) VALUES ( #{0}, #{1}, #{2} ) -->
</insert>
<!-- κ²μκΈ μμ -->
<update id="update">
UPDATE board
SET title = #{title}
,writer = #{writer}
,content = #{content}
,upd_date = now()
WHERE no = #{no}
</update>
<!-- κ²μκΈ μμ -->
<delete id="delete">
DELETE FROM board
WHERE no = #{no}
</delete>
</mapper>
XML
볡μ¬
Mapper μΈν°νμ΄μ€ μμ±
β’
BoardMapper.java
@Mapper μ΄λ
Έν
μ΄μ
μ μ μνλ€.
λ©μλ λͺ
μ Mapper.xml 맀ν νμΌμ 쿼리νκ·Έ id κ°κ³Ό μΌμΉν΄μΌνλ€.
@Mapper
public interface BoardMapper {
// λ©μλ λͺ
μ Mapper.xml 맀ν νμΌμ SQL νκ·Έ id κ°κ³Ό μΌμΉν΄μΌνλ€.
public List<Board> list() throws Exception;
public Integer insert(Board board) throws Exception;
public Board select(Integer no) throws Exception;
public Integer update(Board board) throws Exception;
public Integer delete(Integer no) throws Exception;
}
Java
볡μ¬
table
CREATE TABLE `board` (
`no` int NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`writer` varchar(100) NOT NULL,
`content` text,
`reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`upd_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`views` int NOT NULL DEFAULT '0',
PRIMARY KEY (`no`)
) COMMENT='κ²μν';
SQL
볡μ¬
β’
μν λ°μ΄ν°
INSERT INTO `board` (`title`, `writer`, `content`, `views`)
VALUES
('첫 λ²μ§Έ κΈ', 'μμ±μ1', '첫 λ²μ§Έ κΈμ λ΄μ©μ
λλ€.', 5),
('λ λ²μ§Έ κΈ', 'μμ±μ2', 'λ λ²μ§Έ κΈμ λ΄μ©μ
λλ€.', 12),
('μΈ λ²μ§Έ κΈ', 'μμ±μ3', 'μΈ λ²μ§Έ κΈμ λ΄μ©μ
λλ€.', 8),
('λ€ λ²μ§Έ κΈ', 'μμ±μ4', 'λ€ λ²μ§Έ κΈμ λ΄μ©μ
λλ€.', 20),
('λ€μ― λ²μ§Έ κΈ', 'μμ±μ5', 'λ€μ― λ²μ§Έ κΈμ λ΄μ©μ
λλ€.', 7),
('μ¬μ― λ²μ§Έ κΈ', 'μμ±μ6', 'μ¬μ― λ²μ§Έ κΈμ λ΄μ©μ
λλ€.', 15),
('μΌκ³± λ²μ§Έ κΈ', 'μμ±μ7', 'μΌκ³± λ²μ§Έ κΈμ λ΄μ©μ
λλ€.', 3),
('μ¬λ λ²μ§Έ κΈ', 'μμ±μ8', 'μ¬λ λ²μ§Έ κΈμ λ΄μ©μ
λλ€.', 9),
('μν λ²μ§Έ κΈ', 'μμ±μ9', 'μν λ²μ§Έ κΈμ λ΄μ©μ
λλ€.', 22),
('μ΄ λ²μ§Έ κΈ', 'μμ±μ10', 'μ΄ λ²μ§Έ κΈμ λ΄μ©μ
λλ€.', 0);
SQL
볡μ¬
λΉμ¦λμ€ λ‘μ§
β’
Board.java
β’
BoardService.java
β’
BoardServiceImpl.java
Board.java
@Data
public class Board {
private int no;
private String title;
private String writer;
private String content;
private Date regDate;
private Date updDate;
private int views;
public Board() {
}
public Board(String title, String writer, String content) {
this.title = title;
this.writer = writer;
this.content = content;
}
}
Java
볡μ¬
BoardService.java
public interface BoardService {
// κ²μκΈ λͺ©λ‘
public List<Board> list() throws Exception;
// κ²μκΈ μ‘°ν
public Board select(int no) throws Exception;;
// κ²μκΈ λ±λ‘
public Integer insert(Board board) throws Exception;
// κ²μκΈ μμ
public Integer update(Board board) throws Exception;
// κ²μκΈ μμ
public Integer delete(int no) throws Exception;
}
Java
볡μ¬
BoardServiceImpl.java
@Service // λΉμ¦λμ€ λ‘μ§μ μ²λ¦¬νλ μλΉμ€ ν΄λμ€λ‘ λΉ λ±λ‘
public class BoardServiceImpl implements BoardService {
private static final Logger logger = LoggerFactory.getLogger(BoardServiceImpl.class);
@Autowired
private BoardMapper boardMapper;
@Override
public List<Board> list() throws Exception {
List<Board> boardList = boardMapper.list();
return boardList;
}
@Override
public Board select(int no) throws Exception {
Board board = boardMapper.select(no);
return board;
}
@Override
public Integer insert(Board board) throws Exception {
int result = boardMapper.insert(board);
return result;
}
@Override
public Integer update(Board board) throws Exception {
int result = boardMapper.update(board);
return result;
}
@Override
public Integer delete(int no) throws Exception {
int result = boardMapper.delete(no);
return result;
}
}
Java
볡μ¬
컨νΈλ‘€λ¬
BoardController.java
package com.aloha.spring.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.aloha.spring.dto.Board;
import com.aloha.spring.service.BoardService;
@Controller // 컨νΈλ‘€λ¬λ‘ λΉ λ±λ‘
@RequestMapping("/board") // ν΄λμ€ λ 벨 κ²½λ‘ /board λ‘ μ§μ
public class BoardController {
private static final Logger logger
= LoggerFactory.getLogger(BoardController.class);
@Autowired // μμ‘΄μ± μλ μ£Όμ
private BoardService boardSerivce;
/**
* κ²μκΈ λͺ©λ‘
* @param model
* @return
* @throws Exception
*/
// @RequestMapping(value = "/list", method = RequestMethod.GET)
@GetMapping("/list") // Spring 4.3 λ²μ μ¬μ© κ°λ₯
public String list(Model model) throws Exception {
List<Board> boardList = boardSerivce.list();
model.addAttribute("boardList", boardList);
return "board/list"; // board/list.jsp νλ©΄ μλ΅
}
/**
* κ²μκΈ μ‘°ν
* @param model
* @param no
* @return
* @throws Exception
*/
@GetMapping("/read")
public String read(Model model, int no) throws Exception {
Board board = boardSerivce.select(no);
model.addAttribute("board", board);
return "board/read";
}
// κ²μκΈ λ±λ‘ - /board/insert - [GET]
@GetMapping("/insert")
public String insert() throws Exception {
return "board/insert";
}
// κ²μκΈ λ±λ‘ μ²λ¦¬ - /board/insert - [POST]
@PostMapping("/insert")
public String insertPro(Board board) throws Exception {
int result = boardSerivce.insert(board);
logger.info("κ²μκΈ λ±λ‘ μμ² - result : " + result);
return "redirect:/board/list";
}
// κ²μκΈ μμ - /board/update - [GET]
@GetMapping("/update")
public String update(Model model, int no) throws Exception {
Board board = boardSerivce.select(no);
model.addAttribute("board", board);
return "board/update";
}
// κ²μκΈ μμ μ²λ¦¬ - /board/update - [POST]
@PostMapping("/update")
public String updatePro(Board board) throws Exception {
int result = boardSerivce.update(board);
logger.info("κ²μκΈ μμ μμ² - result : " + result);
return "redirect:/board/list";
}
// κ²μκΈ μμ μ²λ¦¬ - /board/delete - [POST]
@PostMapping("/delete")
public String deletePro(int no) throws Exception {
int result = boardSerivce.delete(no);
logger.info("κ²μκΈ μμ μμ² - result : " + result);
return "redirect:/board/list";
}
}
Java
볡μ¬
λ·°
board
β’
list.jsp
β’
read.jsp
β’
insert.jsp
β’
update.jsp
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>κ²μκΈ λͺ©λ‘</title>
</head>
<body>
<h1>κ²μκΈ λͺ©λ‘</h1>
<a href="${ pageContext.request.contextPath }/board/insert">κΈμ°κΈ°</a>
<table border="1">
<tr>
<th>λ²νΈ</th>
<th>μ λͺ©</th>
<th>μμ±μ</th>
<th>λ±λ‘μΌμ</th>
<th>μμ μΌμ</th>
<th>μ‘°νμ</th>
</tr>
<c:forEach items="${boardList}" var="board">
<tr>
<td align="center">${board.no}</td>
<td width="300">
<a href="${ pageContext.request.contextPath }/board/read?no=${board.no}">
${board.title}
</a>
</td>
<td align="center" width="120">${board.writer}</td>
<%-- <td align="center">${board.regDate}</td> --%>
<td align="center">
<fmt:formatDate value="${board.regDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
</td>
<%-- <td align="center">${board.updDate}</td> --%>
<td align="center">
<fmt:formatDate value="${board.updDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
</td>
<td align="center">${board.views}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
HTML
볡μ¬
read.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>κ²μκΈ μ‘°ν</title>
</head>
<body>
<h1>κ²μκΈ μ‘°ν</h1>
<form action="${ pageContext.request.contextPath }/board/delete" method="POST">
<input type="hidden" name="no" value="${board.no}" />
<table>
<tr>
<td><label for="title">μ λͺ©</label></td>
<td><input type="text" name="title" value="${board.title}" /></td>
</tr>
<tr>
<td><label for="writer">μμ±μ</label></td>
<td><input type="text" name="writer" value="${board.writer}" /></td>
</tr>
<tr>
<td><label for="content">λ΄μ©</label></td>
<td>
<textarea rows="5" cols="20" name="content" >${board.content}</textarea>
</td>
</tr>
</table>
<div>
<a href="${ pageContext.request.contextPath }/board/update?no=${board.no}">μμ </a>
<input type="submit" value="μμ " />
</div>
</form>
</body>
</html>
HTML
볡μ¬
insert.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>κ²μκΈ μ°κΈ°</title>
</head>
<body>
<h1>κ²μκΈ μ°κΈ°</h1>
<form action="${ pageContext.request.contextPath }/board/insert" method="POST">
<table>
<tr>
<td><label for="title">μ λͺ©</label></td>
<td><input type="text" name="title" /></td>
</tr>
<tr>
<td><label for="writer">μμ±μ</label></td>
<td><input type="text" name="writer" /></td>
</tr>
<tr>
<td><label for="content">λ΄μ©</label></td>
<td>
<textarea rows="5" cols="20" name="content" ></textarea>
</td>
</tr>
</table>
<div>
<input type="submit" value="λ±λ‘" />
</div>
</form>
</body>
</html>
HTML
볡μ¬
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>κ²μκΈ μμ </title>
</head>
<body>
<h1>κ²μκΈ μμ </h1>
<form action="${ pageContext.request.contextPath }/board/update" method="POST">
<input type="hidden" name="no" value="${board.no}" />
<table>
<tr>
<td><label for="title">μ λͺ©</label></td>
<td><input type="text" name="title" value="${board.title}" /></td>
</tr>
<tr>
<td><label for="writer">μμ±μ</label></td>
<td><input type="text" name="writer" value="${board.writer}" /></td>
</tr>
<tr>
<td><label for="content">λ΄μ©</label></td>
<td>
<textarea rows="5" cols="20" name="content" >${board.content}</textarea>
</td>
</tr>
</table>
<input type="submit" value="μμ " />
</form>
<form action="${ pageContext.request.contextPath }/board/delete" method="POST">
<input type="hidden" name="no" value="${board.no}" />
<input type="submit" value="μμ " />
</form>
</body>
</html>
HTML
볡μ¬
Mybatis νλΌλ―Έν° 맀ν λ°©λ²
κ°μ²΄ 맀ν
β’
Mapper μΈν°νμ΄μ€ μμ± μ 맀κ°λ³μλ‘ DTO κ°μ²΄ μ¬μ©: λ§€νΌ μΈν°νμ΄μ€μ λ©μλμ 맀κ°λ³μλ‘ DTO κ°μ²΄λ₯Ό μ¬μ©νμ¬ ν΄λΉ DTO κ°μ²΄μ νλ κ°μ 쿼리μ 맀νν μ μμ΅λλ€.
public interface BoardMapper {
public List<Board> list(Board board);
}
Java
볡μ¬
β’
Mapper XMLμμ #{} μ¬μ©νμ¬ νλ κ° λ§€ν: Mapper XMLμμ #{}λ₯Ό μ¬μ©νμ¬ DTO κ°μ²΄μ νλ κ°μ 맀νν μ μμ΅λλ€.
<select id="list" resultType="com.aloha.spring.dto.Board">
SELECT *
FROM board
WHERE title = #{title} AND writer = #{writer}
</select>
XML
볡μ¬
@Param μ΄λ Έν μ΄μ μ¬μ©
β’
Mapper μΈν°νμ΄μ€ μμ± μ @Param μ΄λ
Έν
μ΄μ
μ¬μ©: λ§€νΌ μΈν°νμ΄μ€μ λ©μλμ @Param μ΄λ
Έν
μ΄μ
μ μ¬μ©νμ¬ μ¬λ¬ κ°μ νλΌλ―Έν°λ₯Ό 쿼리μ 맀νν μ μμ΅λλ€.
public interface BoardMapper {
public int insert(@Param("title") String title
, @Param("writer") String writer
, @Param("content") String content);
}
Java
볡μ¬
β’
Mapper XMLμμ νλΌλ―Έν° λͺ
μΌλ‘ 맀ν: Mapper XMLμμ νλΌλ―Έν° λͺ
μ μ¬μ©νμ¬ λ§€κ°λ³μλ₯Ό 쿼리μ 맀νν μ μμ΅λλ€.
<insert id="insert">
INSERT INTO board ( title, writer, content )
VALUES ( #{title}, #{writer}, #{content} )
</insert>
XML
볡μ¬
Map 컬λ μ 맀ν
β’
Mapper μΈν°νμ΄μ€ μμ± μ Map 컬λ μ
μ¬μ©: λ§€νΌ μΈν°νμ΄μ€μ λ©μλμ Map 컬λ μ
μ μ¬μ©νμ¬ μ¬λ¬ κ°μ νλΌλ―Έν°λ₯Ό 쿼리μ 맀νν μ μμ΅λλ€.
public interface BoardMapper {
public int update(Map<String,String> map);
}
Java
볡μ¬
map
{
no : β100β,
title : βμ λͺ©β,
writer : βμμ±μβ,
content : βλ΄μ©β,
}
β’
Mapper XMLμμ νλΌλ―Έν° λͺ
μΌλ‘ 맀ν: Mapper XMLμμ νλΌλ―Έν° λͺ
μ μ¬μ©νμ¬ λ§€κ°λ³μλ₯Ό 쿼리μ 맀νν μ μμ΅λλ€.
<update id="update">
UPDATE board
SET title = #{title}
,writer = #{writer}
,content = #{content}
,upd_date = now()
WHERE no = #{no}
</update>
XML
볡μ¬
Mybatis SQL
select λ¬Έ
MyBatisμ SELECT λ¬Έμ λ°μ΄ν°λ² μ΄μ€μμ λ°μ΄ν°λ₯Ό κ²μνλ λ° μ¬μ©λ©λλ€. μ§μ λ 쑰건μ κΈ°λ°νμ¬ λ¨μΌ λλ λ€μ€ νμ κ²μνλ λ° μ¬μ©ν μ μμ΅λλ€. SELECT λ¬Έμ κ²°κ³Όλ Java κ°μ²΄ λλ Java κ°μ²΄ 컬λ μ
μ 맀νλ μ μμ΅λλ€.
<select id="select" resultType="com.aloha.spring.dto.Board">
SELECT *
FROM board
WHERE no = #{no}
</select>
XML
볡μ¬
μμ μ½λμμ select λ¬Έμ no λ§€κ° λ³μλ₯Ό κΈ°λ°μΌλ‘ board ν
μ΄λΈμμ λ¨μΌ νμ κ²μν©λλ€. κ²°κ³Όλ Board ν΄λμ€μ 맀νλ©λλ€.
insert λ¬Έ
MyBatisμ INSERT λ¬Έμ λ°μ΄ν°λ² μ΄μ€μ μλ‘μ΄ λ μ½λλ₯Ό μ½μ
νλ λ° μ¬μ©λ©λλ€. λ¨μΌ ν λλ ν λ²μ μ¬λ¬ νμ μ½μ
νλ λ° μ¬μ©ν μ μμ΅λλ€. μ½μ
ν κ°μ λ§€κ° λ³μλ‘ μ 곡νκ±°λ Java κ°μ²΄λ‘λΆν° 맀νν μ μμ΅λλ€.
<insert id="insert">
INSERT INTO board (title, writer, content)
VALUES (#{title}, #{writer}, #{content})
</insert>
XML
볡μ¬
μμ μ½λμμ insert λ¬Έμ title, writer, λ° content λ§€κ° λ³μμμ μ 곡λ κ°μΌλ‘ board ν
μ΄λΈμ μλ‘μ΄ νμ μ½μ
ν©λλ€.
update λ¬Έ
MyBatisμ UPDATE λ¬Έμ λ°μ΄ν°λ² μ΄μ€μ κΈ°μ‘΄ λ μ½λλ₯Ό μμ νλ λ° μ¬μ©λ©λλ€. μ§μ λ 쑰건μ κΈ°λ°νμ¬ νλ μ΄μμ μ΄μ μμ νλ λ° μ¬μ©ν μ μμ΅λλ€. μμ ν κ°μ λ§€κ° λ³μλ‘ μ 곡νκ±°λ Java κ°μ²΄λ‘λΆν° 맀νν μ μμ΅λλ€.
<update id="update">
UPDATE board
SET title = #{title},
writer = #{writer},
content = #{content},
upd_date = now()
WHERE no = #{no}
</update>
XML
볡μ¬
μμ μ½λμμ update λ¬Έμ no λ§€κ° λ³μλ₯Ό κΈ°λ°μΌλ‘ board ν
μ΄λΈμ title, writer, content, λ° upd_date μ΄μ μμ ν©λλ€.
delete λ¬Έ
MyBatisμ DELETE λ¬Έμ λ°μ΄ν°λ² μ΄μ€μμ κΈ°μ‘΄ λ μ½λλ₯Ό μ κ±°νλ λ° μ¬μ©λ©λλ€. μ§μ λ 쑰건μ κΈ°λ°νμ¬ νλ μ΄μμ νμ μμ νλ λ° μ¬μ©ν μ μμ΅λλ€.
<delete id="delete">
DELETE FROM board
WHERE no = #{no}
</delete>
XML
볡μ¬
selectKey λ¬Έ
MyBatisμ SELECTKEY λ¬Έμ INSERT λ¬Έμμ μμ±λ ν€ κ°μ κ²μνλ λ° μ¬μ©λ©λλ€. μ£Όλ‘ λ°μ΄ν°λ² μ΄μ€μ μ λ μ½λλ₯Ό μ½μ
ν ν μλμΌλ‘ μμ±λ κΈ°λ³Έ ν€ κ°μ κ²μνλ λ° μ¬μ©λ©λλ€.
<insert id="insert">
<selectKey resultType="java.lang.Integer" keyProperty="parentNo" order="BEFORE">
SELECT MAX( ${pk} ) FROM ${parentTable}
</selectKey>
INSERT INTO file (parent_table, parent_no, file_name, file_path)
VALUES (#{parentTable}, #{parentNo}, #{fileName}, #{filePath})
</insert>
XML
볡μ¬
μμ μ½λμμ selectKey λ¬Έμ λ§μ§λ§μΌλ‘ μ½μ
λ ID κ°μ κ²μνκ³ Java κ°μ²΄μ no μμ±μ 맀νν©λλ€. μ΄λ₯Ό ν΅ν΄ μ λ μ½λλ₯Ό μ½μ
ν ν μμ±λ ν€ κ°μ μ‘μΈμ€ν μ μμ΅λλ€.
${ } vs #{ }
β’
${ } : λμ΄μ¨ 맀κ°λ³μμ κ°μ, κ·Έλλ‘ μΆλ ₯
β’
#{ } : λμ΄μ¨ 맀κ°λ³μμ κ°μ, μλ νμ
λ³ννμ¬ μΆλ ₯
ν¨μ & νλ‘μμ νΈμΆ
λ§μ΄λ°ν°μ€μμ ν¨μλ νλ‘μμ λ₯Ό νΈμΆνλ λ°©λ²μ λν΄ μλ €λλ¦¬κ² μ΅λλ€.
ν¨μ νΈμΆ: ν¨μλ₯Ό νΈμΆνλ €λ©΄ SELECT λ¬Έμ μ¬μ©νμ¬ ν¨μλ₯Ό μ€ννκ³ κ²°κ³Όλ₯Ό λ°νν΄μΌ ν©λλ€. μλ μμ λ₯Ό μ°Έκ³ νμΈμ.
<select id="callFunction" resultType="java.lang.Integer" statementType="CALLABLE">
{ #{result, mode=OUT, jdbcType=INTEGER} = call functionName(#{param1, jdbcType=INTEGER}, #{param2, jdbcType=INTEGER}) }
</select>
XML
볡μ¬
μμ μμ μμλ callFunctionμ΄λΌλ SELECT λ¬Έμ μ¬μ©νμ¬ functionNameμ΄λΌλ ν¨μλ₯Ό νΈμΆνκ³ κ²°κ³Ό κ°μ λ°νν©λλ€. param1κ³Ό param2λ ν¨μμ μ λ¬λλ 맀κ°λ³μμ΄λ©°, resultλ ν¨μμ κ²°κ³Ό κ°μ μ μ₯νλ λ³μμ
λλ€.
1.
νλ‘μμ νΈμΆ: νλ‘μμ λ₯Ό νΈμΆνλ €λ©΄ INSERT, UPDATE λλ DELETE λ¬Έμ μ¬μ©νμ¬ νλ‘μμ λ₯Ό μ€νν΄μΌ ν©λλ€. μλ μμ λ₯Ό μ°Έκ³ νμΈμ.
<insert id="callProcedure" statementType="CALLABLE">
{ call procedureName(#{param1, jdbcType=INTEGER}, #{param2, jdbcType=VARCHAR}) }
</insert>
XML
볡μ¬
μμ μμ μμλ callProcedureμ΄λΌλ INSERT λ¬Έμ μ¬μ©νμ¬ procedureNameμ΄λΌλ νλ‘μμ λ₯Ό νΈμΆν©λλ€. param1κ³Ό param2λ νλ‘μμ μ μ λ¬λλ 맀κ°λ³μμ
λλ€.
μμ μμ μ½λλ₯Ό μ°Έκ³ νμ¬ λ§μ΄λ°ν°μ€μμ ν¨μλ νλ‘μμ λ₯Ό νΈμΆνλ λ°©λ²μ μ μ©ν΄λ³΄μΈμ.
MyBatis λμ SQL
MyBatisμμ λμ SQLμ μμ±νλ λ°©λ²μλ μ¬λ¬ κ°μ§κ° μμ΅λλ€. λ€μμ μΌλ°μ μΌλ‘ μ¬μ©λλ λμ SQL λ¬Έλ²μ λͺ κ°μ§ μμμ
λλ€.
IF λ¬Έ
<if test="condition">
<!-- μ‘°κ±΄μ΄ trueμΌ λ μ€νλλ SQL ꡬ문 -->
</if>
XML
볡μ¬
choose λ¬Έ
β’
chooese
β¦
when
β¦
otherwise
<choose>
<when test="condition1">
<!-- condition1μ΄ trueμΌ λ μ€νλλ SQL ꡬ문 -->
</when>
<when test="condition2">
<!-- condition2κ° trueμΌ λ μ€νλλ SQL ꡬ문 -->
</when>
<otherwise>
<!-- μμ λͺ¨λ μ‘°κ±΄μ΄ falseμΌ λ μ€νλλ SQL ꡬ문 -->
</otherwise>
</choose>
XML
볡μ¬
foreach λ¬Έ
<foreach collection="list" item="item" separator=",">
<!-- listμ λͺ¨λ μμμ λν΄ μ€νλλ SQL ꡬ문 -->
</foreach>
XML
볡μ¬
TRIM λ¬Έ
<!-- prefixμ suffixμλ ν΄λΉ ꡬ문μ λλ¬μΈλ μ λμ¬μ μ λ―Έμ¬κ° μ¬ μ μμΌλ©°, μ λμ¬ λλ μ λ―Έμ¬κ° νμνμ§ μμ κ²½μ° μλ΅ν μ μμ΅λλ€. -->
<trim prefix="prefix" suffix="suffix" prefixOverrides="prefixOverrides" suffixOverrides="suffixOverrides">
<!-- prefixOverridesμ suffixOverridesλ μλ΅ κ°λ₯ν μμλ‘, SQL ꡬ문μ μ λμ¬ λλ μ λ―Έμ¬λ₯Ό μ κ±°ν©λλ€. -->
<!-- SQL ꡬ문 -->
</trim>
XML
볡μ¬
set λ¬Έ
λμ SQLμ μμ±νκΈ° μν΄ MyBatisμμ μ 곡νλ set λ¬Έλ²μ μ¬μ©ν μ μμ΅λλ€. set λ¬Έλ²μ UPDATE 쿼리μμ νμν νλλ§ λμ μΌλ‘ μ
λ°μ΄νΈν μ μλ μ μ©ν κΈ°λ₯μ
λλ€. λ€μμ set λ¬Έλ²μ μμμ
λλ€.
<update id="update">
UPDATE board
<set>
<if test="title != null">
title = #{title},
</if>
<if test="writer != null">
writer = #{writer},
</if>
<if test="content != null">
content = #{content},
</if>
</set>
WHERE no = #{no}
</update>
XML
볡μ¬
μμ μμμμλ title, writer, content νλκ° nullμ΄ μλ κ²½μ°μλ§ ν΄λΉ νλλ₯Ό μ
λ°μ΄νΈνλλ‘ μ€μ λμ΄ μμ΅λλ€. μ΄λ₯Ό ν΅ν΄ νμν νλλ§ λμ μΌλ‘ μ
λ°μ΄νΈν μ μμ΅λλ€.