by-nc-sa     개발자, DBA가 함께 만들어가는 구루비 지식창고!

7. iBatis를 이용한 SELECT 예제 - AbstractCommandController




7. iBatis를 이용한 SELECT 예제 - AbstractCommandController

  • deptno를 파라미터로 받아서 emp테이블의 데이터를 SELECT 하는 예제이다.
  • EmployeeListController.java, EmployeeBO.java, EmployeeDAO.java, Emp.java, Emp.xml 파일을 생성한다.

7.1 Model 클래스 생성

  • 사원관리(emp) 테이블의 모델 클래스를 생성한다.
  • 이 모델객체는 Controller, BO, DAO 에서 사용된다.
Emp.java
package net.gurubee.web.employee.model;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Emp {

    private int empno;
    private int mgr;
    private int sal;
    private int deptno;

    private String ename;
    private String job;

    public int getEmpno() {
        return empno;
    }

    public void setEmpno(int empno) {
        this.empno = empno;
    }

    public int getMgr() {
        return mgr;
    }

    public void setMgr(int mgr) {
        this.mgr = mgr;
    }

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }

    // multi line style
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
}
 

7.2 Controller 서블릿 생성

  • AbstractCommandController 를 상속하여 Controller를 구현한다.
  • AbstractCommandController를 사용하면 Client 파라미터를 쉽게 관리 할 수 있다.
EmployeeListController.java
package net.gurubee.web.employee.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.gurubee.web.employee.bo.EmployeeBO;
import net.gurubee.web.employee.model.Emp;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

public class EmployeeListController extends AbstractCommandController {

    private static final Log LOG = LogFactory.getLog(EmployeeListController.class);
    private EmployeeBO employeeBO;

    public void setEmployeeBO(EmployeeBO employeeBO) {
        this.employeeBO = employeeBO;
    }

    @Override
    protected ModelAndView handle(HttpServletRequest req, HttpServletResponse rep, Object command,
            BindException bindException) throws Exception {

        ModelAndView mv = new ModelAndView();
        Emp emp = (Emp) command;
        LOG.debug(" ### emp : " + emp);

        List<Emp> list = employeeBO.getEmployeeList(emp.getDeptno());

        mv.setViewName("emp/list");
        mv.addObject("list", list);

        return mv;
    }
}

7.3 BO 클래스 생성

  • POJO로 BO클래스를 생성한다.
EmployeeBO.java
package net.gurubee.web.employee.bo;

import java.util.List;

import net.gurubee.web.employee.dao.EmployeeDAO;
import net.gurubee.web.employee.model.Emp;

public class EmployeeBO {

    private EmployeeDAO employeeDAO;

    public List<Emp> getEmployeeList(int deptno) {
        return employeeDAO.selectEmpList(deptno);
    }

    public void setEmployeeDAO(EmployeeDAO employeeDAO) {
        this.employeeDAO = employeeDAO;
    }
}
  • bean을 등록한다.
applicationContext-bo.xml
<bean id="employeeBO" class="net.gurubee.web.employee.bo.EmployeeBO" >
  <property name="employeeDAO" ref="employeeDAO" />
</bean>

7.4 DAO 클래스 생성

  • SqlMapClientDaoSupport 클래스를 extends 하여 DAO를 생성한다.
EmployeeDAO.java
package net.gurubee.web.employee.dao;

import java.util.List;

import net.gurubee.web.employee.model.Emp;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class EmployeeDAO extends SqlMapClientDaoSupport {
    private static final String NAMESPACE = "emp.";

    @SuppressWarnings("unchecked")
    public List<Emp> selectEmpList(int deptno) {
        return (List<Emp>) getSqlMapClientTemplate().queryForList(NAMESPACE + "selectEmpList", deptno);
    }
}
  • bean을 등록한다.
applicationContext-dao.xml
 <bean id="employeeDAO"
      class="net.gurubee.web.employee.dao.EmployeeDAO" />

7.5 iBatis 작성

  • /sqlmapx/Emp.xml 파일에 SELECT SQL 문장을 작성한다.
  • sql-map-config.xml 파일에 Emp.xml을 등록하였는지 확인한다.
Emp.xml
 <?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="emp">

  	<typeAlias alias="emp" type="net.gurubee.web.employee.model.Emp"/>

 	<select id="selectEmpList" resultClass="emp" parameterClass="int">
		SELECT empno, ename, job, sal, IFNULL(mgr,0) mgr
		FROM  emp  
		<isGreaterThan   compareValue="0">
		WHERE deptno = #deptno#
		</isGreaterThan>
 	</select> 	
 	
</sqlMap>

5.2 mvc-dispatcher-servlet.xml 설정

  • SimpleUrlHandlerMapping HandlerMapping을 사용하여 bean을 설정한다.
mvc-dispatcher-servlet.xml
<bean id="handler"  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
        <prop key="/emp/list.do">employeeList</prop>                  
    </props>
  </property>
</bean>

<bean id="employeeList" class="net.gurubee.web.employee.controller.EmployeeListController">
   <property name="employeeBO" ref="employeeBO" />
   <!--  Controller의 command를 설정한다. -->
   <property name="commandClass" value="net.gurubee.web.employee.model.Emp"/>
   <property name="commandName"  value="emp"/>
</bean>

5.3 jsp 생성

  • /WEB-INF/pages/emp/list.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" %>

<body>
    <form name="form" method="post" action="">
        <table width="50%" border="0">
            <tr>
                <td align="right">
                    <input type="button" value="등록" onclick="goInsertForm();"/>
                </td>
            </tr>
        </table>
        <table width="50%" border="1">
            <tr>
                <th>empno</th>
                <th>ename</th>
                <th>job</th>
                <th>sal</th>
                <th>mgr</th>
                <th>deptno</th>
            </tr>
            <c:forEach var="list" items="${list}" varStatus="c">
                <tr>
                    <td>${list.empno}</td>
                    <td>${list.ename}</td>
                    <td>${list.job}</td>
                    <td>${list.sal}</td>
                    <td>${list.mgr}</td>
                    <td>${list.deptno}</td>
                </tr>
            </c:forEach>
        </table>
    </form>
</body>

5.4 테스트

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<fmt:formatNumber value="${list.sal}" type="number" />

문서정보

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.