ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [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 

    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
    <?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.xsd
            http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://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&amp;useSSL=false&amp;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

    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
    <?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_DATE 
            FROM BOARD.TB_BOARD
        
        </select>
        
    </mapper>
    cs

     

    3. pom.xml에 의존성 추가

    json과 관련된 의존성을 추가하세요.

     

    pom.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <!-- 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 

    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
    <?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.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://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

     

     

    반응형

    댓글

Designed by Tistory.