국비지원학원/MyBatis

114일차-Dynamic Query조회

초코맛 2019. 4. 19. 03:33
반응형

select car_img, maker,model, car_year, price, car_option, to_char(hiredate,'yyyy-mm-dd hh24:mi') hiredate
from
(select car_img, maker,model, car_year, price, car_option, hiredate,row_number() over(order by hiredate desc) r_num
    from
(select  cmo.car_img,cma.maker,cmo.model,cmo.car_year,cmo.price,cmo.car_option,cmo.hiredate
    from car_maker cma, car_model cmo
    where cmo.model=cma.model))
where r_num between 1 and 10
 

*Dynamic Query
  • 쿼리문이 조건에 따라 다르게 생성되는 것
    • 게시판에 리스트 
    • select count(*) cnt
    • from 테이블명
    • if(키워드가 존재하니?){
    • where 컬럼명 like '%'검색단어'%'
    • }//이런것들도 다 Dinamic
  • MyBatis 에서는 다이나믹 쿼리를 OGNL로 생성할 수 있다.
  • <where> - where 절을 동적으로 붙여준다.
    • select count(*) cnt
    • from 테이블명
    • <where>
    • 컬럼명 like '%'검색단어'%'
    • </where>
  • <if> -단일조건 줄때(맞을때만) <c:if안쓰고
  • <choose>, <when>, <otherwise> - 연관된 조건
  • <set> - update에서 set을 동적으로 붙일 때
  • <forEach> - List로 입력된 값을 반복 시킬 때
    • or=in(포함되는)
  • 쿼리 모든 부분에 사용할 수 있다.
  • 값이 입력될 때에는 반드시 파라메터 타입이 vo이어야 하고(parameterType="VO", vo에 존재하는 getter가 있을 경우에만 사용할 수 있다.(VO아님 ㄴㄴ)
  • if)
    • <if test="getter 관계연산자(크다작다는 기호Error-&gt; / &lt; 로사용) 비교값" > 
      • 조건에 맞을때 들어갈 쿼리
    • </if>
    • //c : if와 같다 getter는 소문자?
  • where)
    • <where> -속성이 존재하지 않고 where을 동적으로 붙여야 하는 곳에서 사용한다. 
  • 부서(10,20,30,40)에 따른 사원정보 조회 ) 
  • <select.... parameterType="DeptVO">  // <=getDeptno(){  }
  • select 사원번호...
  • from 테이블명
  • <where>
  • <if test="deptno &gt; 9 and deptno &lt; 41">  //이때의 deptno는 getDeptno...
  • <if test="deptno  &gt; 10 and deptno &lt; 20">
    • deptno=10;
  • </if>
  • <if test="deptno  &gt; 20 and deptno &lt; 30">
    • deptno=20;
  • </if>
  • ....//반복
  • </if>
  • </where>
<<Getter가 없다고 Error>>
parameterType가 int면 getter가 없다고 Error가 난다

 

<<<쓰고 끝난 부분
//join_subquery.jsp
<%@page import="kr.co.sist.exam.domain.Car"%>
<%@page import="java.util.List"%>
<%@page import="kr.co.sist.exam.service.MyBatisService1"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
     MyBatisService1 mbs =new MyBatisService1();
     List<Car> carList=mbs.joinSubquery();
     pageContext.setAttribute("carList", carList);
%>
<table style="border-top: 2px solid #333; border-bottom: 1px  solid #333; ">
<tr>
     <th width="80" style="border-bottom: 1px solid  #333;">이미지</th>
     <th width="400" style="border-bottom: 1px solid  #333;">차량정보</th>
     <th width="150" style="border-bottom: 1px solid  #333;">등록일</th>
</tr>
<c:if test="${empty carList}">
<tr>
     <td colspan="3" style="text-align: center">등록된 차량이  존재하지 않습니다.</td>
</tr>
</c:if>
<c:forEach var="car" items="${carList}">
<tr>
     <td><img  src="http://localhost:8080/mybatis_prj/day0409/images/${car.img}" style="width:80px; height: 50px"/></td>
     <td>
           <div>
           <c:out value="${car.maker}"/><c:out  value="${car.model}"/>
           </div>
           <div>
                <c:out value="${car.carYear}"/>년식 <c:out  value="${car.price}"/>
           </div>
           <div>
                <c:out value="${car.carOption}"/>
           </div>
     </td>
     <td>
           <c:out value="${car.hiredate}"/>
     </td>
</tr>
</c:forEach>
</table>
//dynamic_table.jsp
<%@page import="kr.co.sist.exam.domain.Demp"%>
<%@page import="java.util.List"%>
<%@page import="kr.co.sist.exam.vo.TnameVO"%>
<%@page import="kr.co.sist.exam.service.MyBatisService1"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<form action="main.jsp" method="get">
     <input type="hidden" name="page"  value="day0409/dynamic_table"/>
     <input type="radio" name="tname" value="cp_emp2"  checked="checked"/>한국본사
     <input type="radio" name="tname" value="emp"/>미국지사
     <input type="submit" value="조회" class="btn"/>
</form>
<%
     String tname=request.getParameter("tname");
     if(tname !=null){
           MyBatisService1 mbs=new MyBatisService1();
           List<Demp> list=mbs.dynamicTable(new TnameVO(tname));
           pageContext.setAttribute("empList", list);
     }//end if
%>
<c:if test="${not empty param.tname}">
<table>
     <tr>
           <th colspan="4">"<c:out value="${param.tname  eq'emp'?'미국지사':'한국본사'}"/>"의 사원정보 조회 결과</th>
     </tr>
     <tr>
           <th width="80">사원번호</th>
           <th width="120">사원명</th>
           <th width="80">연봉</th>
           <th width="150">입사일</th>
     </tr>
     
     <c:if test="${empty empList}">
     <tr>
           <td colspan="4" style="text-align: center">
                사원이 존재하지 않습니다.
           </td>
     </tr>
     </c:if>
     <c:forEach var="emp" items="${empList}">
     <tr align="center">
           <td><c:out value="${emp.empno}"/></td>
           <td><c:out value="${emp.ename}"/></td>
           <td><c:out value="${emp.sal}"/></td>
           <td><c:out value="${emp.hiredate}"/></td>
     </tr>
     </c:forEach>
</table>
</c:if>
//Demp.java
package kr.co.sist.exam.domain;
public class Demp {
     private int empno,sal;
     private String ename,hiredate;
     
     public int getEmpno() {
           return empno;
     }
     public void setEmpno(int empno) {
           this.empno = empno;
     }
     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 getHiredate() {
           return hiredate;
     }
     public void setHiredate(String hiredate) {
           this.hiredate = hiredate;
     }
}
//DeptnoVO.java
package kr.co.sist.exam.vo;
public class DeptnoVO {
     private int deptno;
     public DeptnoVO(int deptno) {
           this.deptno = deptno;
     }
     public int getDeptno() {
           return deptno;
     }
     
}

 

 


//dynamic_if.jsp
<%@page import="kr.co.sist.exam.domain.Dynamicif"%>
<%@page import="kr.co.sist.exam.vo.DeptnoVO"%>
<%@page import="kr.co.sist.exam.service.MyBatisService1"%>
<%@page import="kr.co.sist.exam.domain.Emp"%>
<%@page import="java.util.List"%>
<%@page import="kr.co.sist.exam.dao.MyBatisDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<%
     MyBatisService1 mbs=new MyBatisService1();
     String deptno=request.getParameter("deptno");
     
     if(deptno!=null){
           List<Dynamicif> list=mbs.dynamicIf(new  DeptnoVO(Integer.parseInt(deptno)));
           pageContext.setAttribute("empList", list);
     }//end if
%>
<form name="frm" action="main.jsp">
<input type="hidden" name="page" value="day0409/dynamic_if"/>
<label>부서번호 입력</label>
<input type="text" name="deptno" class="inputBox"/>
<input type="submit" value="사원 조회", class="btn"/>
*10~30번 부서의 사원을 조회. 없는 부서번호가 입력되면 모든  부서사원을 조회
</form>
<div>
<c:if test="${not empty param.deptno}">
     <div>
           ${param.deptno}번 부서 사원 조회 결과
     </div>
     <table border="1">
           <tr>
                <th width="40">번호</th>
                <th width="80">사원번호</th>
                <th width="120">사원명</th>
                <th width="80">직무</th>
                <th width="80">부서번호</th>
                <th width="150">입사일</th>
           </tr>
           <c:if test="${empty empList}">
           <tr>
                <td colspan="6" align="center">
                     입력하신 부서는 부서가 존재하지 않거나  사원이 없습니다.
                </td>
           </tr>
           </c:if>
           <c:forEach var="emp" items="${empList}">
           <c:set var="i" value="${i+1}"/>
           <tr>
                <td><c:out value="${i}"/></td>
                <td><c:out value="${emp.empno}"/></td>
                <td><c:out value="${emp.ename}"/></td>
                <td><c:out value="${emp.job}"/></td>
                <td><c:out value="${emp.deptno}"/></td>
                <td><c:out value="${emp.hiredate}"/></td>
           </tr>
           </c:forEach>
     </table>
</c:if>
</div>
//Dynamicif.java
package kr.co.sist.exam.domain;
public class Dynamicif {
     private int empno,deptno;
     private String ename,job,hiredate;
     
     public int getEmpno() {
           return empno;
     }
     public void setEmpno(int empno) {
           this.empno = empno;
     }
     public int getDeptno() {
           return deptno;
     }
     public void setDeptno(int deptno) {
           this.deptno = deptno;
     }
     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 String getHiredate() {
           return hiredate;
     }
     public void setHiredate(String hiredate) {
           this.hiredate = hiredate;
     }
     
}
//TnameVO.java
package kr.co.sist.exam.vo;
public class TnameVO {
     private String tname;
     public TnameVO(String tname) {
           this.tname = tname;
     }
     public String getTname() {
           return tname;
     }
     
}


반응형

'국비지원학원 > MyBatis' 카테고리의 다른 글

116일차-Procedure사용  (0) 2019.04.20
115일차-페이지 선택유지/dynamicQuery/procedure사용  (0) 2019.04.19
113일차-지도 마무리/조회 마무리  (0) 2019.04.19
112일차-MyBatis  (0) 2019.04.17
111일차-MyBatis  (0) 2019.04.17