헤르메스 LIFE

[Spring] DB Connection 본문

Spring Framework

[Spring] DB Connection

헤르메스의날개 2012. 2. 15. 15:05
728x90

web.xml

 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:/spring/applicationContext-*.xml</param-value>
 </context-param>
 <!-- Processes application requests -->
 <servlet>
 <servlet-name>rest</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
 /WEB-INF/spring/config/servlet-*.xml
 </param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>

applicationContext-resource.xml

<context:property-placeholder location="classpath:config/applicationConfig.xml"/>



applicationConfig.xml

    <!-- ======================================== -->
    <!--          MYSQL JDBC Configuration        -->
    <!-- ======================================== -->
    <!-- mysql
    <entry key="mysql.jdbc.driverClassName">com.mysql.jdbc.Driver</entry>
     -->
     <!-- log4sql - mysql driver --> 
    <entry key="mysql.jdbc.driverClassName">core.log.jdbc.driver.MysqlDriver</entry>
    <entry key="mysql.jdbc.url">jdbc:mysql://localhost:3309/springdb</entry>
    <entry key="mysql.jdbc.username">scott</entry>
    <entry key="mysql.jdbc.password">tiger</entry>

applicationContext-datasource.xml

    <!-- MYSQL DB POOL datasources -->
    <bean id="defaultDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${mysql.jdbc.driverClassName}" />
        <property name="url" value="${mysql.jdbc.url}" />
        <property name="username" value="${mysql.jdbc.username}" />
        <property name="password" value="${mysql.jdbc.password}" />
        <property name="initialSize" value="10"/>
        <property name="maxActive" value="30"/>
 <property name="maxIdle" value="15" />
 <property name="minIdle" value="15" />
 <property name="testOnBorrow" value="false" />
 <property name="validationQuery" value="select 1" />
 <property name="timeBetweenEvictionRunsMillis" value="10000" />
 <property name="testWhileIdle" value="true" />  
 <property name="numTestsPerEvictionRun" value="3" />        
 <property name="minEvictableIdleTimeMillis" value="-1" />
    </bean>

    <!-- define the SqlSessionFactory -->
    <bean id="defaultSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="defaultDataSource" />
        <property name="typeAliasesPackage" value="com.framework" />
        <property name="configLocation" value="classpath:config/mybatisConfig.xml"/>
        <property name="mapperLocations" value="classpath*:com/ framework /**/sql/*.xml" />
    </bean>
    
    <bean id="defaultSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg ref="defaultSqlSessionFactory"/>
    </bean>

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

    <!-- transaction manager, use JtaTransactionManager for global tx -->
    <bean id="defaultTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="defaultDataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="defaultTransactionManager"/>



mybatisConfig.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" /><!-- default = true -->
        <setting name="useGeneratedKeys" value="true" /><!-- default = false -->
        <setting name="defaultExecutorType" value="REUSE" /><!-- SIMPLE/REUSE/BATCH default = SIMPLE -->
        <setting name="lazyLoadingEnabled" value="false" /><!-- default = true -->
        <setting name="aggressiveLazyLoading" value="true" /><!-- default = true -->
        <setting name="defaultStatementTimeout" value="25000" /><!-- Not Set(null) -->
        <setting name="useColumnLabel" value="true" /><!-- default = true --> 
        <setting name="autoMappingBehavior" value="PARTIAL" /><!-- NONE/PARTIAL/FULL default = PARTIAL -->
        <setting name="multipleResultSetsEnabled" value="true" /><!-- default = true -->        
        <setting name="safeRowBoundsEnabled" value="true" /><!-- default = true -->
        <setting name="mapUnderscoreToCamelCase" value="false" /><!-- default = false -->        
            
    </settings>
</configuration>

https://hermeslog.tistory.com/207

 

[MyBatis] mybatis config xml 예제

출처 : https://mybatis.org/mybatis-3/ko/configuration.html MyBatis – 마이바티스 3 | 매퍼 설정 매퍼 설정 마이바티스 XML 설정파일은 다양한 설정과 프로퍼티를 가진다. 문서의 구조는 다음과 같다.: configuration

hermeslog.tistory.com

 

728x90