본문 바로가기
개발/개인

사용자 정보조회 - 정렬 (MYBATIS $,IF문) , ON조인구문 활용

by areumtb 2016. 11. 15.

... mybatis if문을 사용하면 되는것이였다.



그리드에서 헤더 클릭시 넘어오는 값을 controller에서 받아 매퍼로 정렬값을 인자로 보내서 출력


- controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
    //사원정보 jqgrid, json으로 변형한 데이터
    @RequestMapping(value = "/gridView.do", method = RequestMethod.POST) 
    @ResponseBody   //json으로 변환
    public   HashMap<String, Object> gridView(EmployeeVO vo, Model model,
            @RequestParam(value = "page", required = false, defaultValue = "1"int page,
            @RequestParam(value = "rows", required = false, defaultValue = "20"int rows,
            @RequestParam(value = "total", required = false, defaultValue = "20"int total,
            @RequestParam(value = "records", required = false, defaultValue = "20"int records,
            @RequestParam(value = "sord", required=false, defaultValue="sort"String sord,
          @RequestParam(value = "sidx", required=falseString sidx) {
            
        
        
        System.out.println("page :"+page+", rows :"+rows+", total :"+total+", sord :"+sord+", sidx :"+sidx);
        //page :1, rows :5, total :20, sord :asc, sidx:name
        
        System.out.println("사원정보 jqgrid");
        List<EmployeeVO> empVo = null;
        //empVo = employeeServiceimp.AllEmployeeList();
        HashMap<String, Object> order = new HashMap<String, Object>();
        order.put("SORD", sord); // 정렬값
        order.put("SIDX", sidx); //정렬기준
        
        empVo = employeeServiceimp.AllEmployeeList(order);
        
        HashMap<String, Object> resMap = new HashMap<String, Object>();
        total = employeeServiceimp.totalCount(rows);
        records = employeeServiceimp.totalRecords();
        
        resMap.put("page", page);  
        resMap.put("total", total); // 총페이지
        resMap.put("records", records); // 총레코드
        resMap.put("rows", empVo);
 
        
        return resMap;
    }
 
cs

 

-serviceimp

1
2
3
4
5
6
7
8
9
10
public List<EmployeeVO> AllEmployeeList(Map<String, Object> order) {
        System.out.println("AllEmployeeList2 "+order);
        
        EcoLibraryMapper mapper = sqlsesstion.getMapper(EcoLibraryMapper.class);
        List<EmployeeVO> evo = new ArrayList<>();
        evo = mapper.AllEmployeeList(order);
        System.out.println("AllEmployeeList2 evo "+evo);
 
        return evo;
    }
cs


-mapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
    <select id="AllEmployeeList" resultMap="EmployeeMapper"parameterType="map">
        select 
        UCODE,DCODE,ENAME,NAME,BIRTHDAY,PHONE,EXNUM,EMAIL,ADDRESS,BIREDAY,
        NOTE,DEPARTMENT,BIREDAY,
        DECODE(POSITION,'W_PO1','사원','W_PO2','선임','책임','관리자'
        ,'W_PO4','과장','W_PO5','차장','W_PO6','팀장','W_PO7','이사')AS POSITION,
        DECODE(PRESENT,'W_PE1','정상회원','W_PE2','대출정지','W_PE3','관리자')AS PRESENT
        from EMPLOYEE order by ${SIDX}
        <if test="SORD == ('asc')">ASC</if> 
        <if test="SORD == ('desc')">DESC</if> 
        
    </select>
cs


// 처음엔 

 from EMPLOYEE order by #{SIDX}
        <if test="#{SORD}.EQULALS('asc')">ASC</if> 
        <if test="#{SORD}.EQULALS('desc')">DESC</if> 

이렇게 했다가 값이 그냥 출력했던대로 나와서 오키에 글을올려보니 #->$로 바꾸라서해서 바꿨지만 에러발생

그래서 MYBATIS #,$ 를 검색 + MYBATIS IF문을 검색함
값 매핑 뒤에는 #이 아닌 $로 매핑을 해줘야 하나보다
그리고 IF문등의 쿼리를 쓸때는 해당 쿼리명만 써줘여 하나봄


     from EMPLOYEE order by ${SIDX}
        <if test="SORD == ('asc')">ASC</if> 
        <if test="SORD == ('desc')">DESC</if>






※주의!!! ※

mubatis는 파라미터에 대소문자를 구분한다.  그래서 파라미터값이 대문자인데 xml에서 소문자로 매핑하면

에러가 발생한다. 



댓글