프로그래밍/전자정부프레임워크(eGov)

데이터베이스에서 특정값 조회 후 뷰에 뿌리기 - (2)

Jay Tech 2017. 2. 27. 15:05
반응형

저번 시간에는 SQL설정 까지 보았다.


이번에는 Mapper파일을 보자.


@Mapper("welcomeWebMapper")

public interface WelcomeWebMapper {

List<Map> selectPieChartList() throws Exception;

// test

List<Map> selectListOne(Map<String, Object> paramMap) throws Exception;

List<Map> selectListTwo(CountryVO countryVO) throws Exception;

List<Map> selectListThree(String str) throws Exception;

}




이렇게 인터페이스로 맵퍼를 정의한다. 저번 포스팅에서 맵핑 설정을 해 놓았으므로 이제 자동으로 맵핑이 될 것이다.


맵퍼전은 서비스이므로 서비스단을 보자.


public interface WelcomeWebService {


List<Map> selectPieChartList() throws Exception;

// test


List<Map> selectListOne(Map<String, Object> paramMap) throws Exception;

List<Map> selectListTwo(CountryVO countryVO) throws Exception;

List<Map> selectListThree(String str) throws Exception;

}



똑같다. 이 인터페이스를 상속받은 서비스 임플을 만들어야 한다.


@Service("welcomeWebService")

public class WelcomeWebServiceImpl extends EgovAbstractServiceImpl implements WelcomeWebService{


@Resource(name="welcomeWebMapper")

private WelcomeWebMapper welcomeWebMapper;



@Override

public List<Map> selectPieChartList() throws Exception {

return welcomeWebMapper.selectPieChartList();

}


@Override

public List<Map> selectListOne(Map<String, Object> paramMap)

throws Exception {

return welcomeWebMapper.selectListOne(paramMap);

}


@Override

public List<Map> selectListTwo(CountryVO countryVO) throws Exception {

return welcomeWebMapper.selectListTwo(countryVO);

}


@Override

public List<Map> selectListThree(String str) throws Exception {

return welcomeWebMapper.selectListThree(str);

}

}



맵퍼파일을 @Resource로 주입을 해준다.

그리고 아까 서비스에서 정의한 함수들을 구현을 해준다. 맵퍼쪽에서 받아온 데이터를 호출한쪽으로 리턴한 것으로 끝낸다. 컨트롤러에 전달해야 하기 떄문이다.



대망의 컨트롤러 이다.




@Controller

public class WelcomeWebController {


@Resource(name = "welcomeWebService")

private WelcomeWebService welcomeWebService;




@RequestMapping(value = "test.do")

public String initTest(HttpServletRequest request, ModelMap model)

throws Exception {


// 3가지 방법 테스트하는 것임


String country = request.getParameter("country");

Map<String, Object> paramMap = new HashMap<String, Object>();


paramMap.put("country", country);


CountryVO countryVO = new CountryVO();

countryVO.setCountryName(country);


List<Map> list1 = welcomeWebService.selectListOne(paramMap);

List<Map> list2 = welcomeWebService.selectListTwo(countryVO);

List<Map> list3 = welcomeWebService.selectListThree(country);

System.out.println("list1:"+list1);

System.out.println("list2:"+list2);

System.out.println("list3:"+list3);


model.addAttribute("tableList1", list1);

model.addAttribute("tableList2", list2);

model.addAttribute("tableList3", list3);


return "test/test.tiles";

}

}



서비스 주입을 해준다. 그리고 @RequestMapping은 url맵핑이므로 뷰에서 설정한 url을 적어준다. 필자는 그냥 test.do로 했다.


List<Map> list1 = welcomeWebService.seletListOne(paramMap)은 맵을 던지는 방식이고

두 번째는 VO, 세번재는 스트링이다.


String country = request.getParameter("country"); 이것은 jsp쪽에서 던진 값이다.


jsp쪽 폼태그 안에

<input type="hidden" id="country" name="country" value="러시아" /> 이런식으로 값을 던진다.. name의 값을 자바쪽에서 getParameter로 찾는 것이다. 


model.addAttribute("tableList1", list1);

model.addAttribute("tableList2", list2);

model.addAttribute("tableList3", list3);


return "test/test.tiles";


이부분은 jsp로 넘길 것들을 설정해 주는 것이다. 함수의 매개변수에 ModelMap이 있는데 이것이 바로 뷰로 보낼때 쓰는 것이다. key와 value 형식으로 넘기게 된다.


그렇다면 이제 뒷단은 끝났다.


앞단의 jsp쪽에서 받는 것은 처음 글의 표 나오는 html에서 보여줬는데 다시 보겠다.



<div class="content table-responsive table-full-width">

                        <table class="table table-hover table-striped">

                            <thead>

                                <th>ID</th>

                            <th>Name</th>

                            <th>Salary</th>

                            <th>Country</th>

                            <th>City</th>

                            </thead>

                            <tbody>

                            <c:forEach items="${tableList3}" var="tableList3" varStatus="status">

                            <tr>

                              <td><c:out value="${tableList3.id}"/></td>

                              <td><c:out value="${tableList3.userName}"/></td>

                              <td><c:out value="${tableList3.age}"/></td>

                              <td><c:out value="${tableList3.country}"/></td>

                              <td><c:out value="${tableList3.etc}"/></td>

                              </tr>

                            </c:forEach>

                            </tbody>

                        </table>

                   </div>


이런식으로 3 덩어리를 만들면 되겠다.


jstl을 보면 items=$표시는 아까 컨트롤러에서 넘긴 키 값이다. var은 jstl내에서 쓰일 변수값을 정의 하는것인데 그냥 편의상 같이 했다.


varStatus는 jstl상태 태그 속성이다. 조금더 쉽게 제어하기 위해서 쓰이는 것인데 여기서는 쓰이지 않았다. 그냥 참고하자면,


${status.current}    현재 아이템 


${status.index}    0부터의 순서 


${status.count}    1부터의 순서


${status.first}    현재 루프가 처음인지 반환


${status.last}    현재 루프가 마지막인지 반환


${status.begin}    시작값 


${status.end}    끝값 


${status.step}    증가값 



값을 찍어 줄때는 이런식으로 찍는다. <td><c:out value="${tableList3.id}"/></td>


var로 정의한 변수를 이용하여 값을 꺼내온다. 디비에서 받아올 당시 맵으로 받았다. 그러므로 이런식으로 꺼내야 한다. 만일 컨트롤러에서 단일 값만 넘긴경우는 그냥 하나의 변수만 쓰는 식이 되겠다.



결과 화면



이런식으로 테이블이 만들어 질 것이다. 비단 테이블만이 아니라 다른 표현 그래프 또는 텍스트 등 원하는 ui를  쓰면 되겠다. 중요한것은 데이터베이스에서 어떻게 값을 가져와서 화면까지 전달하냐는 것이다.


질문 있으시면 댓글 남겨주세요~

반응형