ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SPRING] Spring + MariaDB + Mybatis 연결하기!
    Spring/Spring Framework 2020. 5. 22. 10:02

    Spring + MariaDB + Mybatis를 연결해보자!

    1. 설치 환경

    • Spring: 3.1.1.release
    • OS: Window10 pro

    2. pom.xml 수정

    아래는 Spring과 MariaDB & Mybatis 연결을 위해 필요한 라이브러리 목록이다.

    1. mybatis

    2. mybatis-spring

    3. spring-jdbc

    4. mariadb-java-client

    위 목록을 얻기 위해서  https://mvnrepository.com/  에서 각각의 라이브러리를 검색해 원하는 버전으로 코드를 복사하자!

     

    Maven Repository: Search/Browse/Explore

    CryptoTool Carina Core Last Release on May 21, 2020

    mvnrepository.com

    이제 복사한 각각의 라이브러리 코드를 pom.xml 에 붙여넣어주면 된다.

    	<dependencies>
    		.
          	  	.
          	  	.
            
    		<!-- Mybatis -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis</artifactId>
    			<version>3.5.4</version>
    		</dependency>
    
    		<!-- Mybatis-spring -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis-spring</artifactId>
    			<version>2.0.4</version>
    		</dependency>
    
    		<!-- Spring-jdbc -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    			<version>3.1.1.RELEASE</version>
    		</dependency>
    
    		<!-- Mariadb-java-client -->
    		<dependency>
    			<groupId>org.mariadb.jdbc</groupId>
    			<artifactId>mariadb-java-client</artifactId>
    			<version>2.6.0</version>
    		</dependency>
            
            	.
            	.
            	.
            </dependencies>

     

    3. root-context.xml 수정

    이제 root-context.xml 파일에다가 어떤 DB를 사용할건지! 계정과 비밀번호는 뭔지! 설정한다.

     

    일단 root-context.xml파일 하단의 Namespaces 를 클릭한다.

    그 다음, 밑의 사진에 체크되어있는 항목들을 체크해주면 된다.

     

     

    이제 다시 Namespaces espaces 말고~ Source로 돌아가서

    DB 설정에 맞게 수정해준다.

    <?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"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
    		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    	
    	<!-- Root Context: defines shared resources visible to all other web components -->
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="org.mariadb.jdbc.Driver" />
    		<property name="url" value="jdbc:mariadb://127.0.0.1:3306/DB이름" />
    		<property name="username" value="계정이름" />
    		<property name="password" value="계정비밀번호" />
    	</bean>
    	
    	<bean id="SqlSessionFectory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="configLocation" value="classpath:/mybatis-config.xml" />
    		<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="SqlSessionFectory" />
    	</bean>
    </beans>
    

     

    ** 본인의 환경에 맞게 수정해줘야하는 부분 **

    DB이름, 계정이름, 계정비밀번호 (사용하고자하는 DB에 맞게 수정!)

    configLocation값, mapperLocations값 (본인의 프로젝트 구조에 맞게 경로 수정!)

     

    ** SqlSessionFectory Bean의 경로들은 따로 만들어줘야함(바로 밑에서 함!) **

     

    4. mapper folder 생성

    1) resources 폴더에 새로운 mappers 폴더 생성

    2) mappers 폴더 안에 mapper.xml 파일 생성

     

    위의 폴더 구조는 root-context.xml 에서 SqlSessionFectory bean 의 경로다!

    사실 저 경로에 맞게 bean경로를 써준거지만..ㅎ

     

    ex) memberMapper.xml 파일

    <?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="memberMapper">
    	
    </mapper>

     

    5. 연결 확인! - 간단한 회원가입 기능 구현

    이제 연결을 위한 설정은 끝났다!

    진짜로 DB값을 가져오는지 확인만 남았다ㅠㅠ

    확인을 위해서 회원가입 기능을 예로 들었는데 view는 생략하겠다ㅎㅎㅎ

    (회원가입이 아니더라도 간단한 select 하나만 해봐도 된다)

     

    1) 기본 DB 구조

    Member Table

    2) VO

    @Data
    public class MemberVO {
    	private String employeeNum;
    	private String userName;
    	private String userEmail;
    	private String userPwd;
    	private Date applyDate;
    	private Date acceptDate;
    	private char status;
    }

    ~~ Controller, Service 는 생략 ~~

     

    3) DAO

    @Repository("memberDAO")
    public class MemberDAO {
    	@Autowired
    	protected SqlSessionTemplate sqlSession; // !!연결연결!!
    	
    	public int insertMember(MemberVO member) {
    		return sqlSession.insert("memberMapper.insertMember", member);
    	}
    }

     

    4) mapper

    <insert id="insertMember" parameterType="member">
    		insert into member
    		values(#{employeeNum}, #{userPwd}, #{userName}, #{userEmail}, default, null, default)
    </insert>

     

    5) 확인

     

    기존 Member table의 데이터

    회원가입 후 Memmber table의 데이터

     

    2번 째 데이터가 insert 된 것을 확인 할 수 있다!

     

     

    이렇게 String + MariaDB + Mybatis 연결 완료~~

    댓글

Designed by Tistory.