개발_기초 정보

[mybatis-spring] mapper 세팅하기

메타듀 2023. 2. 21. 11:03
728x90
반응형
  1. build.gradle 추가
  2. DataSource 를 세팅 -> HikariCP
  3. MyBatis 세팅 -> SqlSessionFactory
  4. Mapper 인터페이스
  5. Mapper 인터페이스 설정이 필요.
  6. Mapper에 SQL을 개발.
  7. Test 만들어 테스트하기

build.gradle - dependencies 안에

 //스프링 코어 https://mvnrepository.com/artifact/org.springframework/spring-core
    implementation 'org.springframework:spring-core:5.3.18'
    implementation 'org.springframework:spring-test:5.3.18'
    implementation 'org.springframework:spring-context:5.3.18'
    implementation 'org.springframework:spring-jdbc:5.3.18'
    implementation 'org.springframework:spring-tx:5.3.18'

    //롬복 https://mvnrepository.com/artifact/org.projectlombok/lombok
    compileOnly 'org.projectlombok:lombok:1.18.22'
    annotationProcessor 'org.projectlombok:lombok:1.18.22'
    testCompileOnly 'org.projectlombok:lombok:1.18.22'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'

    //log4j
    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.2'
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.2'
    implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.17.2'

    // 마리아DB https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client
    implementation 'org.mariadb.jdbc:mariadb-java-client:3.0.4'

    // 히카리cp https://mvnrepository.com/artifact/com.zaxxer/HikariCP
    implementation 'com.zaxxer:HikariCP:5.0.1'

    // 마이바티즈 https://mvnrepository.com/artifact/org.mybatis/mybatis
    implementation 'org.mybatis:mybatis:3.5.9'

    // 마이바티즈 스프링 https://mvnrepository.com/artifact/org.mybatis/mybatis-spring
    implementation 'org.mybatis:mybatis-spring:2.0.7'

java.org.com.practice.mapper.NowMapper //인터페이스 만들기

public interface NowMapper {

    String getTime();
    String getDate();
}

resources.mappers.NowMapper.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="org.com.practice.mapper.NowMapper">

</mapper>

webapp.WEB-INF.root-Context.xml

<?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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://mybatis.org/schema/mybatis-spring
       http://mybatis.org/schema/mybatis-spring.xsd">

    <context:component-scan base-package="org.com.practice.store"/>

    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="driverClassName"
                  value="org.mariadb.jdbc.Driver"></property>
        <property name="jdbcUrl"
                  value="jdbc:mariadb://127.0.0.1:3306/webdb"></property>
        <property name="username" value="webuser"></property>
        <property name="password" value="webuser"></property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:/mappers/**/*.xml"></property>
    </bean>

    <!-- HikariCP configuration -->
    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
          destroy-method="close">
        <constructor-arg ref="hikariConfig" />
    </bean>

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

</beans>

resources.log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration status="INFO">

    <Appenders>
        <!-- 콜솔 -->
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout charset="UTF-8" pattern="%d{hh:mm:ss} %5p [%c] %m%n"/>
        </Console>
    </Appenders>

    <loggers>
        <logger name="org.springframework" level="INFO" additivity="false">
            <appender-ref ref="console" />
        </logger>

        <logger name="org.com" level="INFO" additivity="false">
            <appender-ref ref="console" />
        </logger>

        <logger name="org.com.practice.mapper" level="TRACE" additivity="false">
            <appender-ref ref="console" />
        </logger>

        <root level="INFO" additivity="false">
            <AppenderRef ref="console"/>
        </root>

    </loggers>

</configuration>

resources.mappers 안에 mapper 안에 추가

 <select id="getTime" resultType="String">  
        select DATE_FORMAT(now(), '%H:%i:%s')
 </select>
 <select id="getDate" resultType="String">
        select DATE_FORMAT(now(), '%Y-%m-%d')
 </select>

test 만들기
test.java.org.com.practice.mapper.NowMapperTests

@Log4j2
@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations ="file:src/main/webapp/WEB-INF/root-context.xml")
public class NowMapperTests {

    @Autowired(required = false)
    private NowMapper nowMapper;

    @Test
    public void testNow(){
        log.info("---------------------------------");
        log.info(nowMapper.getClass().getName());
        log.info(nowMapper.getTime());
        log.info(nowMapper.getDate());
        log.info("---------------------------------");
    }
}

실행해보기! 성공!

반응형