ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring postgresql mybatis jdbc 기본 연결테스트
    Spring/Spring 기본 지식 2020. 3. 23. 10:51
    반응형

    기본 구조

     

     

    Junit5 사용하여 연결 테스트

    @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/PostgreSQL-context.xml"})
    public class PostgreSQLConnectionTest {
    	@Autowired
    	private DataSource ds;
    
    	@Test
    	public void testConnection() throws Exception {
    
    		try (Connection con = ds.getConnection()) {
    
    			System.out.println("\nDBConnection 있냐? : " + con + "\n");
    
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    }

     

    @ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/**/PostgreSQL-context.xml"})
    public class PostgreSQLMybatisTest {
    	@Autowired
    	private SqlSessionFactory sqlFactory;
    
    	@Test
    	public void testFactory() {
    		System.out.println("\nSqlSessionFactory 생성되냐? : " + sqlFactory);
    	}
    
    	@Test
    	public void testSession() throws Exception {
    
    		try (SqlSession session = sqlFactory.openSession()) {
    
    			System.out.println("\nSqlSessionFactory 생성이되면 sqlSesion도 생성되겟지 : " + session  + "\n");
    
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }

     

    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: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
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    	<!-- Root Context: defines shared resources visible to all other web components -->
    	<bean
    		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations" value="classpath:/jdbc.properties" />
    		<property name="fileEncoding" value="UTF-8" />
    	</bean>
    
    	<!-- JDBC-PostgreSQL -->
    	<bean id="PostgreSQLDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
    		<property name="driverClassName" value="${jdbc.driverClassName}" />
    		<property name="url" 			 value="${jdbc.postgis.url}" />
    		<property name="username" 		 value="${jdbc.postgis.username}" />
    		<property name="password" 		 value="${jdbc.postgis.password}" />
    		<property name="initialSize" 	 value="${jdbc.postgis.initialSize}" />
    		<property name="maxActive" 		 value="${jdbc.postgis.maxActive}" />
    		<property name="maxIdle" 		 value="${jdbc.postgis.maxIdle}" />
    		<property name="minIdle" 		 value="${jdbc.postgis.minIdle}" />
    		<property name="maxWait" 		 value="${jdbc.postgis.maxWait}" />
    	</bean>
    
     	<!-- mybatis SqlSessionFactoryBean -->
        <bean id="PostgreSQLSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="PostgreSQLDataSource"></property>
            <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
            <property name="mapperLocations" value="classpath:/mybatis/**/*.xml"></property>
        </bean>
        
        <!-- mybatis      -->
        <bean id="PostgreSQLSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
            <constructor-arg name="sqlSessionFactory" ref="PostgreSQLSessionFactory"></constructor-arg>
        </bean>
    
    </beans>
    

     

    pom.xml

    	<!-- Test -->
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.12</version>
    			<scope>test</scope>
    		</dependency>       
    
    		<!-- jacson -->
    		<dependency>
    		    <groupId>com.fasterxml.jackson.core</groupId>
    		    <artifactId>jackson-core</artifactId>
    		    <version>2.10.0</version>
    		</dependency> 
    		
    		 <!-- MySQL -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.11</version>
            </dependency>
            <!-- Srping JDBC -->
    		<dependency>
    		    <groupId>org.springframework</groupId>
    		    <artifactId>spring-jdbc</artifactId>
    		    <version>${org.springframework-version}</version>
    		</dependency>
            <!-- postgre driver -->
    		<dependency>
    			<groupId>org.postgresql</groupId>
    			<artifactId>postgresql</artifactId>
    			<version>9.4-1204-jdbc4</version>
    		</dependency>
    	    <!-- Spring-test -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${org.springframework-version}</version>
            </dependency>
             <!-- MyBatis-Spring -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.0</version>
            </dependency>
            <dependency>
    	        <groupId>org.mybatis</groupId>
    	        <artifactId>mybatis</artifactId>
    	        <version>3.4.5</version>
    	    </dependency>
            <dependency>
    		    <groupId>commons-dbcp</groupId>
    		    <artifactId>commons-dbcp</artifactId>
    		    <version>1.4</version>
    		</dependency>

     

    jdbc.properties

    jdbc.driverClassName=org.postgresql.Driver
    jdbc.postgis.url=jdbc:postgresql://127:0.0.0.1/test
    jdbc.postgis.username=test1234
    jdbc.postgis.password=1234

     

    mybatis-config.xml

    <?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>
      		<typeAliases>
      		 	<typeAlias type="kr.co.test.DTO.ParameterDTO" alias="parameterDTO"/>
            </typeAliases>
        </configuration>
    

     

    반응형

    'Spring > Spring 기본 지식' 카테고리의 다른 글

    Spring AOP 설정방법 - @annotation  (0) 2020.03.25
    JDBC 사용 - 커넥션 풀  (0) 2020.03.23
    서블릿 컨테이너와 스프링 컨테이너  (0) 2020.03.19
    Spring Bean Life Cycle  (0) 2020.03.15
    DI 방법 3가지  (0) 2020.03.12
Designed by Tistory.