-
[Spring] 게시판 만들기(13) - mapper 및 json 설정Spring/4.3.x - 게시판 만들기 2018. 12. 14. 16:19반응형
SQL문을 관리하는 mapper 파일 및 경로를 설정하고, Java 객체를 JSON으로 응답할 수 있도록 설정하는 작업을 진행하도록 하겠습니다.
1. root-context.xml 수정
mapper에 위치 및 SqlSession을 설정하세요.
root-context.xml
123456789101112131415161718192021222324252627282930<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsdhttp://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/board?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC" /><property name="username" value="board" /><property name="password" value="password1!" /></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:mybatis-config.xml" /><property name="mapperLocations" value="classpath:mappers/**/*.xml"></property></bean><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache"><constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/></bean></beans>cs 2. mapper.xml 추가
src/main/resources 위치에 mappers 폴더를 생성한 후 boardMapper.xml 파일을 추가하세요. parameterType, resultType에 선언한 BoardForm, BoardDto 클래스는 다음 게시글에 추가하도록 하겠습니다.
mapper.xml
12345678910111213141516171819202122232425<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.spring.board.boardMapper"><select id="getBoardList" parameterType="com.spring.board.form.BoardForm" resultType="com.spring.board.dto.BoardDto">SELECT BOARD_SEQ, BOARD_RE_REF, BOARD_RE_LEV, BOARD_RE_SEQ, BOARD_WRITER, BOARD_SUBJECT, BOARD_CONTENT, BOARD_HITS, DEL_YN, INS_USER_ID, INS_DATE, UPD_USER_ID, UPD_DATEFROM BOARD.TB_BOARD</select></mapper>cs 3. pom.xml에 의존성 추가
json과 관련된 의존성을 추가하세요.
pom.xml
123456789101112131415161718<!-- Jackson --><jackson.version>2.9.5</jackson.version><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>${jackson.version}</version></dependency>cs 4. servlet-context.xml 수정
컨트롤러에서 넘겨주는 데이터를 messageConverter로 사용하기 위한 MappingJackson2HttpMessageConverter 추가
servlet-context.xml
12345678910111213141516171819202122232425262728293031323334353637<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:beans="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --><!-- Enables the Spring MVC @Controller programming model --><annotation-driven /><!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the $/resources directory --><resources mapping="/resources/**" location="/resources/" /><!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --><beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><beans:property name="prefix" value="/WEB-INF/views/" /><beans:property name="suffix" value=".jsp" /></beans:bean><context:component-scan base-package="com.spring.board" /><mvc:interceptors><mvc:interceptor><mvc:mapping path="/**"/><beans:bean id="loggerInterceptor" class="com.spring.board.common.LoggerInterceptor"></beans:bean></mvc:interceptor></mvc:interceptors><beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /></beans:beans>cs 소스 코드는 Github Repository - https://github.com/tychejin1218/board_v1 (branch : section13) 를 참조하세요.
Github에서 프로젝트 가져오기 - https://tychejin.tistory.com/33반응형'Spring > 4.3.x - 게시판 만들기' 카테고리의 다른 글
[Spring] 게시판 만들기(15) - 게시글 등록(INSERT), 수정(UPDATE), 삭제(DELETE), 상세 조회(SELECT) (28) 2018.12.17 [Spring] 게시판 만들기(14) - 게시글 조회(SELECT) (28) 2018.12.17 [Spring] 게시판 만들기(12) - Mybatis 연동 (9) 2018.12.14 [Spring] 게시판 만들기(11) - 인터셉터(Interceptor) 설정 (5) 2018.12.14 [Spring] 게시판 만들기(10) - log4j->logback으로 변경 (7) 2018.12.13