ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring 게시판](3) 스프링에 오라클&마이바티스 연결
    Practice 2020. 12. 3. 03:05

     

     

    아직까지 설정이 안끝났다. 이제 스프링과 오라클, 마이바티스를 연결하려고 한다.

     

    예전에 프로젝트 할 때는 기본 설정을 다른 팀원이 해 직접 스프링 설정을 마무리하는 경우가 없었다.

    이번에는 스스로 구글링을 통해 정보를 찾아보며 스프링 설정을 마무리 하려고 한다.

     

     

    1. log4jdbc.log4j2.properties파일 생성

     

    log4jdbc의 정체를 몰라서 알아보았다. 검색해보니 로그를 생성하고 쿼리를 정렬해주는 역할인 듯 싶다.

    log4jdbc에 연결하기 위해서 해당 파일이 필요하다.

     

     

    참고: tsop.tistory.com/30

     

    09. 스프링에서 myBatis사용하여 mySQL db연동

    스프링 프레임워크에서 myBatis사용하여 mySQL db연동하는 방법 결론적으로 아래와 같이 파일구성이 되야한다. 예제파일----- -------- 그럼 차근차근 진행해본다. 1. src/main/resources에 파일 생성 1) myb.

    tsop.tistory.com

     

    2. root-context.xml 수정

     

    src/main/WEB-INF/spring/ 경로의 root-context.xml을 수정한다.

    먼저 NameSpaces에서 beans / context / jdbc / mybatis-spring을 선택한다.

    예전엔 직접 Spring 파일들을 구해 경로에 집어넣는 방법을 택했는데, STS를 통해 생성하니 이런 기능도 제공하는구나 싶었다.

     

    root-context.xml의 NameSpaces

    이후 root-context.xml의 소스를 바꾼다. 

    아래의 소스를 삽입했다. 오라클 username과 password를 삽입하면 된다.

    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
    <?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: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">
     
     <!-- Root Context: defines shared resources visible to all other web components -->
     
     <!-- 오라클 접속 -->
        <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
            <property value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" name="driverClassName"/>
            <property value="jdbc:log4jdbc:oracle:thin:@localhost:1521/XE" name="url"/>
            <property value="유저이름" name="username"/>
            <property value="" name="password"/>
        </bean>
       
        <!-- 마이바티스 연동 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
             <property name="dataSource" ref="dataSource"></property>
             <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
             <property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml"/>
        </bean>
        <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
            <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
        </bean>
       
    </beans>
    cs

     

    3. mybatis-config.xml / testMapper.xml 파일을 생성

     

    src/main/resource 폴더에 mybatis-config.xml를 생성한다. 해당 파일에는 아래 소스를 삽입한다.

    1
    2
    3
    4
    5
    6
    7
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
      PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
      
    </configuration>
    cs

     

    이후에는 해당 폴더에 mappers 폴더를 생성한 후 testMapper.xml 파일을 생성한다.

    이후 해당 파일에 아래 소스를 삽입한다.

    1
    2
    3
    4
    5
    6
    7
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.test.mappers.testMapper">
       
    </mapper>
    cs

     

    4. ojdbc6.jar 파일 삽입

     

    oracleext 폴더에서 ojdbc.jar 파일을

     

    C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext
    C:\Program Files\Java\jre1.8.0_171\lib\ext

     

    아래 루트에 복사-붙여넣기 한다.

     

     

    이번 스프링에 오라클/마이바티스 연결은 아래 블로그를 참조했다.

     

    kuzuro.blogspot.com/2018/04/blog-post_7.html

     

    스프링에 오라클과 마이바티스 연동

    개발자, 웹개발, PC앱개발, Java, C#, HTML/CSS, JavaScript, Spring, ASP, .NET

    kuzuro.blogspot.com

     

    의존 주입이 많아 매번 헷갈리는 연결이다.

    특히 각 DBMS마다 연결 방식이 다르고, 블로그 마다 주입하는 라이브러리도 다르기에

    여러번 시도해서 익숙해 질 필요가 있다.

     

    중간에 maven으로 update project를 안해줘서 경로를 읽지 못하고 pom.xml / root-context.xml의 일부 구문이 에러가 났다. ㅠㅠ 어디에도 maven update project 하란 말이 없어서 고생하다가 겨우 찾았다.

     

    혹시 두 파일에서 의문을 알 수 없는 구문에러가 나는 경우에는

    maven update project를 해보는 방법도 있겠다.

    댓글

Designed by Tistory.