본문 바로가기
DataBase/MariaDB

[MariaDB] Spring + MariaDB 연동하기

by 김뚱 2018. 12. 14.

[현재환경]

spring : 4.2

DB : mariadb 10.3


현재 package 구조는 다음과 같다.




1. pom.xml 라이브러리 추가하기


https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client/2.0.1












spring 실행 > pom.xml > dependency 추가

MariaDB connector에 맞는 log4jdbc 추가




2. root-context.xml bean 설정하기


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="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.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/shcema/jdbc/spring-jdbc.xsd">
    
    <!-- Root Context: defines shared resources visible to all other web components -->
    
    <!-- Scans within the base package of the application for @Component classes to configure as beans -->  
    <context:component-scan base-package="com.pjt.*"></context:component-scan>
    
    <context:property-placeholder location="classpath:application.properties"/>

    <!-- MariaDB -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="org.mariadb.jdbc.Driver"/>
        <property name="url" value="jdbc:mariadb://localhost:3306/test"></property>
        <property name="username" value="root"/>
        <property name="password" value="test"/>
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis/config.xml"/>
        <property name="mapperLocations" value="classpath:mybatis/sql/*.xml"></property>
    </bean>
    
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
    
</beans>



3. mybatis 관련 config 파일 및 쿼리가 작성될 파일 생성하기




4. DB연동 확인


DB에 테스트 테이블 작성한 후 확인한다.

728x90
반응형

'DataBase > MariaDB' 카테고리의 다른 글

[MariaDB] MariaDB Connector 설치하기  (0) 2018.12.14
[MariaDB] MariaDB 10.3 설치  (0) 2018.12.14

댓글