mybatis νλ μμν¬λ μΈν°νμ΄μ€λ§ μ΄μ©νμ¬ λ§€νΌ μ€μ κ°λ₯
μΆμλ©μλμ λ§€νΌ μ΄λ Έν μ΄μ (Mapper Annotation)μ μ¬μ©νμ¬ SQL λͺ λ Ή λ±λ‘
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;
}
}
μΆμλ©μλμ λ±λ‘λ SELECT λͺ λ Ήμ κΈ°λ³Έμ μΌλ‘ κ²μκ²°κ³Όλ₯Ό μλ 맀νμ²λ¦¬νμ¬ μ 곡
@Results : κ²μνμ 컬λΌκ°μ Java κ°μ²΄μ νλκ°μΌλ‘ μ μ₯λλλ‘ λ§€ν μ²λ¦¬νλ μ 보λ₯Ό μ 곡νλ μ΄λ Έν μ΄μ
@Results(value = { })
or @Results({ })
@Result : κ²μνμ 컬λΌκ°μ΄ Java κ°μ²΄ νλμ μ μ₯λλλ‘ μ€μ νλ μ리먼νΈ
@Result(column = "hewon_id", property = "id")
@SelectProvider : SQL Builder ν΄λμ€μ λ©μλλ₯Ό νΈμΆνμ¬ SELECT λͺ λ Ήμ λ°νλ°μ λ±λ‘νλ μ΄λ Έν μ΄μ
@SelectProvider(type = MyHewonProvider.class, method = "selectDynamicName")
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);
}
Provider ν΄λμ€(SQL Builder ν΄λμ€) : SQL λͺ λ Ήμ λ°ννλ λ©μλκ° μ μΈλ ν΄λμ€
SQL κ°μ²΄ : SQL λͺ λ Ή μμ±μ νμν λ©μλλ₯Ό νΈμΆνμ¬ SQL λͺ λ Ήμ μ μ₯νκΈ° μν κ°μ²΄
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();
}
}
selectHewonList() : μ 체 νμ μ 보 λͺ©λ‘ μΆλ ₯
selectNameHewonList(String name) : μ΄λ¦μ κ²μνμ¬ ν΄λΉ νμμ 보 μΆλ ₯
selectNameHewonList(String name) : SQL Builder ν΄λμ€λ₯Ό μ¬μ©νμ¬ νμμ 보 κ²μ
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();
}
}
}
selectHewonList() : μ 체 νμ μ 보 λͺ©λ‘ μΆλ ₯
selectNameHewonList(String name) : μ΄λ¦μ κ²μνμ¬ ν΄λΉ νμμ 보 μΆλ ₯
selectNameHewonList(String name) : SQL Builder ν΄λμ€λ₯Ό μ¬μ©νμ¬ νμμ 보 κ²μ
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>