카테고리 없음

[Spring] Mybatis Sample

shoney9254 2023. 7. 9. 22:55
반응형

컨트롤러

@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RequestMapping(value = "/api/v1")
@RequiredArgsConstructor
public class UserdataController {

    private final UsedataService usedataService;
    private final UserService userService;


    //제공 api : 수정 불가
    @RequestMapping(value = "/user/{user_no}", method = RequestMethod.GET)
    public UserInfoDto getUseInfo(@PathVariable("user_no")String user_no) {
       
        UserInfoDto result = userService.getUserInfo(user_no);
        return result;
    }
   
    //제공 api : 해당 api를 바탕으로 프론트엔드를 구현하시오
    @RequestMapping(value = "/user/{user_no}/usage/summary", method = RequestMethod.GET)
    public UsedataDto getUseData(@PathVariable("user_no")String user_no , @RequestParam("ptype")int ptype ) {
        String start_dt = getStartDate(ptype);
       
        UsedataDto result = usedataService.getUseData(user_no, start_dt);
       
        return result;
    }

    // 작성해야할 api
    @RequestMapping(value = "/user/{user_no}/usage", method = RequestMethod.GET)
    public Map<String, Object> getUsedList(@PathVariable("user_no")String user_no , @RequestParam("ptype")int ptype) {
        String start_dt = getStartDate(ptype);
        Map<String, Object> result = usedataService.getUsedList(user_no, start_dt);
        return result;

    }



    // 날짜 계산을 위한 제공 method
    private String getStartDate(int type){
        Calendar currentCalendar = Calendar.getInstance();
        int nType =  type;      

        switch(nType){
            case 1: // 1주일 전
                currentCalendar.add(Calendar.DATE, -7);
            break;
            case 2: // 1달 전
                currentCalendar.add(Calendar.MONTH, -1);
            break;
            case 3: // 3달 전
                currentCalendar.add(Calendar.MONTH, -3);
            break;
            default:
                currentCalendar.add(Calendar.DATE, -7);            
            break;
        }
        String returnDate = new java.text.SimpleDateFormat("yyyy-MM-dd").format(currentCalendar.getTime());
        return returnDate;
    }


}

매퍼

@Mapper
public interface UsedataMapper {    
   

    UsedataDto getUseData(@Param("user_no")String user_no, @Param("start_dt")String start_dt);

    List<UsedListDto> getUsedList(@Param("user_no")String user_no, @Param("start_dt")String start_dt);

}

 

 

서비스 i/f

package com.lgcns.tct.service;

import org.apache.ibatis.annotations.Param;
import com.lgcns.tct.dto.UsedataDto;
import java.util.*;

public interface UsedataService {

    public UsedataDto getUseData(@Param("user_no")String user_no, @Param("start_dt")String start_dt);

    public Map<String, Object> getUsedList(@Param("user_no")String user_no, @Param("start_dt")String start_dt);
}

 

서비스 impl

@Service
@RequiredArgsConstructor
public class UseddataServiceimpl implements UsedataService {

    @Autowired
    private final UsedataMapper usedataMapper;
   
    @Override
    public UsedataDto getUseData(String user_no, String start_dt){

        UsedataDto result = usedataMapper.getUseData(user_no, start_dt);
        return result;
    }

   
    @Override
    public Map<String, Object> getUsedList(String user_no, String start_dt){
       
        List<UsedListDto> list = usedataMapper.getUsedList(user_no, start_dt);


        Map<String, Object> result = new HashMap<>();
        result.put("list", list);

        return result;
    }
}

 

매퍼xml

    <select id="getUseData" parameterType="String" resultType="com.lgcns.tct.dto.UsedataDto">
        SELECT COUNT(*) AS usage_count
            , SUM(use_time) AS usage_minute
            , SUM(use_distance) AS usage_meter
            , round(SUM(use_distance)/1000*0.232, 1) AS carbon_reduction
        FROM kickboard_svc.t_svc_use
        WHERE 1=1
        AND user_no=#{user_no}
        AND DATE(use_start_dt)>=#{start_dt}
    </select>

        <select id="getUsedList" parameterType="String" resultType="com.lgcns.tct.dto.UsedListDto">
    SELECT svc.USE_NO AS use_no
            , svc.USE_DISTANCE AS use_distance
            , svc.USE_TIME AS use_time
            , date_format(svc.USE_START_DT, '%Y-%m-%d %h:%i') AS use_start_dt
            , date_format(svc.USE_END_DT, '%Y-%m-%d %h:%i') AS use_end_dt
            , date_format(pay.PAY_DATETIME, '%Y-%m-%d %h:%i') AS pay_datetime
            , max(case when  pay.PAYMETHOD_CODE = 'C' then pay.PAY_COST END) AS card_pay
            , max(case when  pay.PAYMETHOD_CODE = 'P' then pay.PAY_COST END) AS point_pay
        FROM kickboard_svc.t_svc_use_pay as pay
            , kickboard_svc.t_svc_use AS svc
        WHERE 1=1
        AND svc.USE_NO =  pay.USE_NO
        AND svc.USER_NO =#{user_no}
            AND DATE(svc.use_start_dt)>=#{start_dt}
        GROUP BY svc.USE_NO
        ORDER BY use_start_dt DESC
    </select>
   

 

 

 

반응형