김민쏭
REPOSITORY
김민쏭
전체 방문자
오늘
어제
  • 분류 전체보기 (69)
    • 원앙둥지 (17)
      • 2022 홋카이도 (6)
      • 2024 후쿠오카 (3)
    • 프로그래밍 (51)
      • 정리노트 (11)
      • Spring (5)
      • Javascript (7)
      • Database (7)
      • 알고리즘 (11)
      • 좋은글_갈무리 (3)
      • 기타 (3)

인기 글

최근 글

최근 댓글

hELLO · Designed By 정상우.
김민쏭

REPOSITORY

개발환경 구축 - 03. log4jdbc, MyBatis 연동 테스트 (끝)
프로그래밍/Spring

개발환경 구축 - 03. log4jdbc, MyBatis 연동 테스트 (끝)

2019. 3. 13. 01:00

1. log4jdbc Property



src/main/resources 에 log4jdbc.log4j2.properties 생성


log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator

▲ log4jdbc.log4j2.properties 파일 내용


2. DB(오라클) 연동 및 MyBatis



root-context.xml 의 Namespace 에서 'beans, context, jdbc, mvc, mybatis-spring' 체크 후 Ctrl + S


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
<?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="oracle.jdbc.driver.OracleDriver" name="driverClassName" />
        <property value="jdbc:oracle:thin:@localhost:1521:orcl" name="url" />
        <property value="DB유저명" name="username" />
        <property value="DB패스워드" 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>
Colored by Color Scripter
cs




src/main/resources에 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>
Colored by Color Scripter
cs


코드 입력



이어서 src/main/resources 에 mappers 폴더 생성하고 testMappers.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>
Colored by Color Scripter
cs

testMappers.xml에 코드를 작성한다.


(위 사진처럼 폴더를 생성했는데 폴더 아이콘이 아니라, 패키지 아이콘으로 나올시)



프로젝트 오른쪽 클릭 -> Properties -> Java Build Path -> Source 에서

YS/src/main/resources의 Excluded: (None) 부분을 Edit해준다.



아래 Exclusion patterns 부분에 Add로 ** 를 추가해준다.



적용



3. JUnit을 이용한 MyBatis 연동테스트


src/test/java 내에 있는 기본 패키지 내에 MyTest.java 생성


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.love.ys;
 
import java.sql.Connection;
 
import javax.inject.Inject;
import javax.sql.DataSource;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"file:src/main/webapp/WEB-INF/spring/**/root-context.xml"})
public class MyTest {
 
    @Inject
    private DataSource ds;
       
    @Inject
    private SqlSessionFactory sqlFactory;
 
    @Test
    public void test() throws Exception{
 
        try(Connection conn = ds.getConnection()){
            System.out.println(conn);
        } catch(Exception e){
            e.printStackTrace();
        }
    }
   
  
    @Test
    public void factoryTest() {
        System.out.println(sqlFactory);
      
    }
  
    @Test
    public void sessionTest() throws Exception{
      
        try(SqlSession session = sqlFactory.openSession()) {
            System.out.println(session);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}
Colored by Color Scripter
cs


코드 입력 후 프로젝트 오른쪽 클릭 -> Run As -> JUnit Test


이렇게 JUnit 창에서 초록색 불이 들어오면 MyBatis 연동 완료.


--


YS.zip


저작자표시 (새창열림)

'프로그래밍 > Spring' 카테고리의 다른 글

VS Code로 Java, SpringBoot 개발환경 세팅(2/2 - SpringBoot)  (1) 2020.05.28
VS Code로 Java, SpringBoot 개발환경 세팅 (1/2 - Java)  (0) 2020.05.28
개발환경 구축 - 02. 오라클, MyBatis dependency 추가  (1) 2019.03.13
개발환경 구축 - 01. 최초 기본세팅  (0) 2019.03.13
    '프로그래밍/Spring' 카테고리의 다른 글
    • VS Code로 Java, SpringBoot 개발환경 세팅(2/2 - SpringBoot)
    • VS Code로 Java, SpringBoot 개발환경 세팅 (1/2 - Java)
    • 개발환경 구축 - 02. 오라클, MyBatis dependency 추가
    • 개발환경 구축 - 01. 최초 기본세팅
    김민쏭
    김민쏭
    예예,,,저장소임더,,,

    티스토리툴바