Dev/Spring

[ Spring ] 인터페이스를 이용하는 mybatis 연결

surimi🍥 2021. 3. 7. 15:01
반응형

# 구조도

# Maven pom.xml dependency 설정

# 서블릿 및 JSTL

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
    
     <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

# json 형식 데이터를 JS에서 Controller로 보낼때 사용되는 dependency

  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
    </dependency>

# spring JDBC 사용과 oracle DB로 연결하기 위한 dependency

    <!-- DB 연결 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>com.oracle.ojdbc</groupId>
      <artifactId>ojdbc8</artifactId>
      <version>19.3.0.0</version>
    </dependency>

# basicDataSource (DataBase Connection Pool)

    <!--dataSource 추가 org.apache.commons.dbcp2.BasicDataSource -->
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-dbcp2</artifactId>
      <version>2.8.0</version>
    </dependency>

 

# Mybatis 사용을 위한 dependency

    <!-- Mybatis -->
    <!--https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>

# Lombok

    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.16</version>
      <scope>provided</scope>
    </dependency>

 

# applicationContext 설정

# applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans https://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://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--특정 패키지 안의 클래스들을 스캔하고 빈 인스턴스를 생성한다. 아래와 같은 정확한 어노테이션이 존재해야지 빈을 생성할수있다.
          @Component @Repository @Service @Controller-->
    <context:component-scan base-package="com.board"/>

    <!--  <context:component-scan> 이 있으므로 생략 가능-->
    <context:annotation-config />
    <mvc:annotation-driven />

    <!-- Oracle -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
        <property name="username" value="***"/>
        <property name="password" value="***"/>

        <!-- default 설정 -->
        <property name="initialSize" value="0"/>
        <property name="maxTotal" value="8"/>
        <property name="maxIdle" value="8"/>
        <property name="minIdle" value="0"/>
        <property name="maxWaitMillis" value="-1"/>
    </bean>

    <!-- 트랜잭션 설정(Namespaces에 tx 추가) -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    <tx:annotation-driven transaction-manager="transactionManager" />-->
    
    <!--맵핑 인터페이스 구현  -->
    <mybatis-spring:scan base-package="com.board.mapper"/>

    <bean id="sqlSessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:/mybatis-config.xml"/>
        <property name="mapperLocations">
            <array>
                <value>classpath:com.board.mapper/*.xml</value>
            </array>
        </property>
    </bean>

</beans>

 

xml 문서 가장 위 <beans> 태그 안에 

xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" 
xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd"

 

이 내용이 추가되어야 mybatis 사용이 가능하다.

 

<mybatis-spring:scan base-package="com.board.mapper"/>

mybatis의 mapper.xml과 연동시킬 인터페이스 파일의 패키지 경로를 지정한다.

 

 

    <bean id="sqlSessionFactory"
          class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:/mybatis-config.xml"/>
        <property name="mapperLocations">
            <array>
                <value>classpath:com.board.mapper/*.xml</value>
            </array>
        </property>
    </bean>

# sqlSessionFactory

    DB 연결과 ConnectionPool 정보를 입력해 놓은 bean인 "dataSource"

    "mybatis-config.xml"의 mybatis 설정과

    "com.board.mapper/*.xml" 경로의 .xml 파일을 연결시켜준다. (/*.xml은 해당 경로의 모든 xml파일을 의미)

 


# 적용

 

# interface 파일

public interface IndexService {

    public int initiateServer();
}

# IndexMapper.xml

<?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.board.mapper.IndexMapper">

	<update id="initiateServer">
		update server set start_count = start_count + 1 where SERVER_NAME = 'simple'
	</update>

</mapper>

mapper namespace="com.board.mapper.IndexMapper" 로 interface 파일을 지정하고

select update delete 등의 query 문을 쓸 때 id="initiateServer"로 메소드명을 입력해주면 연동이 된다.

 

이제 자바에서 해당 인터페이스메소드를 실행시키면 쿼리가 동작하게 된다.

 

반응형