헤르메스 LIFE

[개발환경] Spring + Maven + MyBatis + PostgreSQL 본문

Spring Framework

[개발환경] Spring + Maven + MyBatis + PostgreSQL

헤르메스의날개 2022. 11. 20. 23:18
728x90

sample-spring-webapp.zip
0.04MB

개발환경

Eclipse Java EE IDE for Web Developers ( Eclipse IDE Neon 3 Packages x64 )
 
Spring 3.2.14
Maven 3.8.6
MyBastis 3.1.1
PostgreSQL 14.1
Tomcat 8.5.81
JDK 1.8.0_202

DBCP + MyBatis 를 이용한 PostgreSQL 접속 방법

context-common.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:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- annotation enabled -->
    <context:annotation-config />

    <context:component-scan base-package="spring.simple.test">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

</beans>

context-datasource.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:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/springboot" />
        <property name="username" value="hermeswing" />
        <property name="password" value="pass" />
    </bean>
</beans>

context-mapper.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:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

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

        <property name="mapperLocations">
            <list>
                <value>classpath*:spring/simple/test/sample/**/dao/*Mapper.xml</value>
            </list>
        </property>
    </bean>

    <!-- MyBatis SqlSessionTemplate -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="spring.simple.test.sample" />
    </bean>
</beans>

context-transaction.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:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

    <tx:annotation-driven proxy-target-class="true" />

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
</beans>

mybatis-config-base.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>

    <settings>
        <setting name="cacheEnabled" value="true" />
        <setting name="lazyLoadingEnabled" value="false" />
        <setting name="multipleResultSetsEnabled" value="true" />
        <setting name="useColumnLabel" value="true" />
        <setting name="useGeneratedKeys" value="false" />
        <setting name="defaultExecutorType" value="SIMPLE" />
        <setting name="defaultStatementTimeout" value="25000" />
    </settings>

    <typeHandlers>
        <!-- java.sql.Timestamp 를 java.util.Date 형으로 반환 -->
        <typeHandler javaType="java.sql.Timestamp" handler="org.apache.ibatis.type.DateTypeHandler" />
        <typeHandler javaType="java.sql.Time" handler="org.apache.ibatis.type.DateTypeHandler" />
        <typeHandler javaType="java.sql.Date" handler="org.apache.ibatis.type.DateTypeHandler" />
    </typeHandlers>

</configuration>

http://localhost:9090/sample-spring-webapp/sample/list

아래의 환경을 참조해서 셋팅만 변경해서 테스트한 내용입니다.

소스는 아래 개발환경을 기초로 Import 해서 테스트해 보시면 실행되실 거라 생각됩니다.


https://hermeslog.tistory.com/629

 

[개발환경] Spring + Maven + MyBatis + HSQL Simple Web Sample

갑자기 Spring 3.x 환경으로 개발환경을 구축하려니, 고생스럽네요. 덕분에 이것 저것 급조해서 간단하게 만들어봤습니다. 현재는 Eclipse plugin의 지원이 원할치 않습니다. neon3 버전 이전버전은 Marke

hermeslog.tistory.com

https://hermeslog.tistory.com/541

 

[SpringBoot] PostgreSQL 연결하기

IDE : IntelliJ JDK : OpenJDK 11 Framework : Spring Boot 2.5.2 Database : PostgreSQL 최신버전 ( 라이센스도 소스공개의무도 없음 ) 첨부파일이 MySqlRunner 로 되어있는데.. MySQL 접속테스트 중 소스만 바뀐거라.. ㅎㅎ Do

hermeslog.tistory.com

개발환경 다운로드

https://mega.nz/file/MakC0CDK#tCCYVOg13Cq4xccC2SlNviCuAcxJVF--oQGFaS0Q3RA

 

704.91 MB file on MEGA

 

mega.nz

 

728x90