Spring Boot 시작하기

- #6. MariaDb + Mybatis 연동


이번장에서는 Database 연동에 대해 진행하겠습니다.

mariaDB는 미리 설치가 되어져 있어야됩니다.



pom.xml 에 아래 내용 추가. 

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
			
		<dependency>
			<groupId>org.mariadb.jdbc</groupId>
			<artifactId>mariadb-java-client</artifactId>
			<version>1.3.2</version>
		</dependency>
		
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.3.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.3</version>
		</dependency>

# dependency 추가 후 이클립스에서 alt + F5를 실행하면 자동으로 jar파일들을 추가합니다.  ( 프로젝트 우클릭 -> maven -> Update Project.. )


application.properties내에 아래 내용 추가
db계정 / id , pw는 설치하신 MariaDB 정보 입력

spring.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/test
spring.datasource.username=test
spring.datasource.password=test


DatabaseConfig.java

package com.example; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @MapperScan(basePackages="com.example.dao") @EnableTransactionManagement public class DatabaseConfig { @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath:com/example/dao/*.xml")); return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception { final SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory); return sqlSessionTemplate; } }


DbController.java

package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.service.DbService;

@RestController
public class DbController {
    @Autowired
    DbService dbService;
    
    @RequestMapping("/")
    public @ResponseBody String root_test() throws Exception{   
        return "Hello World";
    }

    @RequestMapping("/now")
    public @ResponseBody String now() throws Exception{
        return dbService.getDual();
    }
}


DbService.java

package com.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.DbMapper;

@Service
public class DbService {

	@Autowired
	DbMapper dbMapper;

	/* select dual */
    public String getDual() throws Exception{
        return dbMapper.getDual();
    }

}


DbMapper.java

package com.example.dao;

public interface DbMapper {
    /* DB Select  */
	public String getDual() throws Exception;
}


Db.xml

src/main/resources/com/exmaple/dao/Db.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="com.example.dao.DbMapper">

   	<!-- /* select dual */ -->
	<select id="getDual" resultType="java.lang.String">
		SELECT NOW() FROM DUAL
	</select>

</mapper>


결과화면


http://localhost/now

현재 시각이 출력되는 것을 볼 수 있습니다.



총 수정 / 추가파일은 아래그림과 같습니다.



# maraiDB 의 설치 및 연동은 따로 블로깅하도록 하겠습니다.

+ Recent posts