Spring - JDBC + MyBatis

^_^·2022년 5월 18일
0

Spring Web

목록 보기
2/4
post-thumbnail
  • build.gradle 설정 추가
//Spring
implementation 'org.springframework:spring-jdbc:5.3.19'

//HikariCP
implementation 'com.zaxxer:HikariCP:5.0.1'

//mariadb
implementation 'org.mariadb.jdbc:mariadb-java-client:3.0.4'

//mybatis
implementation 'org.mybatis:mybatis:3.5.9'

//mybatis-spring
implementation 'org.mybatis:mybatis-spring:2.0.7'
  • IntelliJ - Database 연결
  • test - ConnectionTests 생성
  • ConnectionTests
package connectionTests;

import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.sql.Connection;
import java.sql.DriverManager;

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

    @Test
    public void ConnectionTests() throws Exception{

        Class.forName("org.mariadb.jdbc.Driver");

        Connection connection = DriverManager.getConnection(
                "jdbc:mariadb://localhost:3306/webdb",
                "webuser",
                "webuser");

        Assertions.assertNotNull(connection);

        connection.close();

    }
}

  • root-context.xml 설정 추가
<!-- DB Connection -->
<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>
<!-- HikariCP configuration -->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
      destroy-method="close">
    <constructor-arg ref="hikariConfig" />
</bean>
  • DataSource Connection Test
@Autowired
    private DataSource dataSource;

    @Test
    public void ConnectionTests2(){

        try{
            Connection con = dataSource.getConnection();
            log.info(con);
        }catch (Exception e){

        }
}

  • mapper 패키지 - TimeMapper 클래스 생성

  • TimeMapper
package org.board.mapper;

import org.apache.ibatis.annotations.Select;

public interface TimeMapper {

    @Select("select now()")
    String getTime();

    String getTime2();
}
  • root-context.xml 설정 추가
<!-- MyBatis configuration-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mapperLocations" value="classpath:/meppers/**/*.xml"/>
</bean>

<Mybatis-spirng:scan base-package="org.board.mapper"/>
  • resources - mappers 패키지 - TimeMapper.xml 생성
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.board.mapper.TimeMapper">
<!-- select는 항상 resultType을 반환한다 -->
    <select id="getTime2" resultType="String">
        select now()
    </select>

</mapper>
  • ConnectionTest 추가
@Autowired
private SqlSessionFactory sqlSessionFactory;

@Test
public void ConnectionTest3(){
    try{
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Connection connection = sqlSession.getConnection();
log.info(sqlSession);
log.info(connection);

    }catch (Exception e){
        e.printStackTrace();
    }
}

  • TimeMapperTests 추가
package mapper;

import lombok.extern.log4j.Log4j2;
import org.board.mapper.TimeMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

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

public class TimeMapperTests {

    @Autowired(required = false)
    private TimeMapper timeMapper;

    @Test
    public void Test1(){
log.info(timeMapper.getClass().getName());
log.info(timeMapper.getTime());
    }

    @Test
    public void Test2(){
log.info("getTime2");
log.info(timeMapper.getTime2());
    }
}

0개의 댓글