목표
mariadb & mybatis 연동

 

1. pom.xml

...

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
          
    </dependencies>

...

 

2. application.yml

spring :    
    devtools :
        livereload :
            enalbed : true
    thymeleaf:
        cache : false       
    datasource:
        hikari :
            maximum-pool-size : 4
        url : jdbc:mariadb://localhost:3306/bamdule
        username : root
        password : 12345678
        
        
mybatis :
    type-aliases-package : com.bamdule.model
    mapper-locations : mapper/xml/*.xml

 

3. mapper-member.xml

mybatis를 이용해서 테이블 쿼리를 정의하는 XML 파일 생성 

1) src/main/resources/mapper/xml 디렉토리 생성
2) xml 디렉토리에 mapper-member.xml 파일 생성
<!DOCTYPE mapper
	PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.bamdule.db.mapper.MemberMapper">
    
    <select id="selectMember" resultType="map" >
        SELECT *
        FROM member
    </select>
</mapper>

Member 테이블 명세

CREATE TABLE `member` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`email` VARCHAR(100) NOT NULL COLLATE 'utf8_general_ci',
	`PASSWORD` VARCHAR(100) NOT NULL COLLATE 'utf8_general_ci',
	`NAME` VARCHAR(100) NOT NULL COLLATE 'utf8_general_ci',
	PRIMARY KEY (`id`) USING BTREE,
	UNIQUE INDEX `email` (`email`) USING BTREE
);

 

4. Member Mapper 생성  

1) com.bamdule.db.mapper package 생성
2) mapper 안에 MemberMapper Interface 생성
3) Mapper 컴퍼넌트 선언 후 selectMember 메소드 정의
package com.bamdule.db.mapper;

import java.util.Map;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface MemberMapper {

    public Map<String, Object> selectMember();
}

 

5. Member Service 생성

1) com.bamdule.db.service.impl package 생성
2 MemberService와 MemberServiceImpl 클래스 생성
package com.bamdule.db.service;

import java.util.Map;

public interface MemberService {

    Map<String, Object> selectMember();

}
package com.bamdule.db.service.impl;

import com.bamdule.db.mapper.MemberMapper;
import com.bamdule.db.service.MemberService;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memeberMapper;

    @Override
    public Map<String, Object> selectMember() {
        return memeberMapper.selectMember();
    }

}

 

6. HomeController에서 selectMember 결과 콘솔 출력

package com.bamdule.controller;

import com.bamdule.db.service.MemberService;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class HomeController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MemberService memberService;

    @GetMapping("/")
    public String homeView() {
        Map<String, Object> member = memberService.selectMember();
        logger.info("[MYTEST] {}", member);
        return "page/home";
    }

}

 

logback 설정을 하지 않은 분은 아래 링크를 참고하여 설정해주세요.

2020/01/03 - [IT/Spring] - [Spring Boot] Logback 설정 방법

 

7. 결과

[MYTEST] {PASSWORD=123, id=1, email=test, NAME=kim}

+ Recent posts