[WEB] SQL + MyBatis

HyeJi9908·2022년 11월 27일
0

[WEB] 기초

목록 보기
7/8

SQL

테이블 2개일 때

(참고 예제)

GROUP BY는 조건(WHERE) 후 컬럼 그룹화 구문

group by 는 내장함수 sum,max,count 일때 쓰임.
위의 예제에서 만약 성적테이블에 과목컬럼에 수학과목도 각각 있었다면 같은 과목별 점수를 합산해야할 때 group by 과목 이 추가됨

참고

INNER JOIN(교집합)

LEFT JOIN(차집합)

tb_mbr에서 사용자의 기본주소 칼럼이 따로 없음 + tb_adr에서 해당주소의 기본주소 사용여부 칼럼이 있음
=> left join써서 userController에서 addressService전역변수해서 addressVO받아내던가 그냥 addressController에서처리하던가
(이때 select 에는 무조건 쓰인 테이블들이 모두 입력되어야함. B.adr_cn 만 입려했더니 null값 받아옴)

참고

WEHER 절 서브쿼리


MyBatis의 동적 쿼리

  • namespace는 별칭을 임의로 정하는 파일에서 정하거나 혹은 그대로 ...Mapper.java의 파일 경로임
    ex)

  • <select id=... 의 id는 controller나 dao에서 메소드 호출시 사용하는 메소드명

  • resultType은 mybatis-config.xml에서 지정한 alias의 이름?

  • 파라미터 타입은 쿼리문에서 쓰이는 테이블을 정의한 vo? Integer이런것도 가능함

참고


평과과제 2:게시판 검색 조회 쿼리

     (/src/main/resources/egovframework/sqlmap/intte/Intte_SQL_mysql.xml)


OR


<?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" >
<mapper namespace="EgovMainDAO">

    <select id="selectIntteClsList" parameterType="IntteVO" resultType="EgovMap">
        SELECT A.intte_seq,
               A.cls_seq,
               A.cls_nm
        FROM tb_intte_cls A
            INNER JOIN tb_intte B ON A.intte_seq = B.intte_seq AND B.use_yn = 'Y'
        WHERE A.intte_seq = #{intteSeq}
            AND A.cls_seq = #{clsSeq}
    </select>

    <select id="selectIntteList" resultType="EgovMap">
        SELECT intte_seq,
               intte_nm
        FROM tb_intte
        WHERE use_yn = 'Y'
        
    </select>

    <select id="selectIntteMbr" parameterType="IntteMbrVO" resultType="EgovMap">
        SELECT *
        FROM tb_intte_mbr A
            INNER JOIN tb_intte B ON A.intte_seq = B.intte_seq AND B.use_yn = 'Y'
        WHERE A.intte_seq = #{intteSeq}
            AND A.intte_mbr_id = #{intteMbrId}
    </select>
    
    <select id="selectList" resultType="ListVO">
        SELECT cls_nm, subj_nm, cls_st_date, cls_fns_date, CASE WHEN use_yn = 'Y' THEN '사용' WHEN use_yn = 'N' THEN '미사용' ELSE NULL END AS use_yn, cret_dt
                
        FROM tb_intte_cls
        WHERE intte_seq=#{intteSeq}
        
        
        
        <if test="searchCnd==1">
          AND  cls_nm LIKE CONCAT ('%', #{searchWrd},'%')
        </if>
        
        <if test="searchCnd == 2">
          AND  subj_nm LIKE CONCAT ('%', #{searchWrd},'%')
        
        </if>
        
        <if test="searchCnd == 0">
          AND (
               cls_nm LIKE CONCAT ('%', #{searchWrd},'%')
               OR
               subj_nm LIKE CONCAT ('%', #{searchWrd},'%')
              )
        </if>
    
    </select>

</mapper>

0개의 댓글