Spring Boot 시작하기
- #6. MariaDb + Mybatis 연동
이번장에서는 Database 연동에 대해 진행하겠습니다.
mariaDB는 미리 설치가 되어져 있어야됩니다.
pom.xml 에 아래 내용 추가.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | < 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 정보 입력
1 2 3 4 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <p> 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; } } </p> |
DbController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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
1 2 3 4 5 6 | package com.example.dao; public interface DbMapper { /* DB Select */ public String getDual() throws Exception; } |
Db.xml
src/main/resources/com/exmaple/dao/Db.xml 로 생성
1 2 3 4 5 6 7 8 9 10 | <? 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 의 설치 및 연동은 따로 블로깅하도록 하겠습니다.
'SPRING BOOT' 카테고리의 다른 글
Spring Boot 시작하기 - #8. Srping Boot JSP view 설정하기 (0) | 2016.08.24 |
---|---|
Spring Boot 시작하기 - #7. Mybatis Multi DataSource 운용 (4) | 2016.08.12 |
Spring Boot 시작하기 - #5_3. Eclipse Srping Boot 웹 페이지 설정 (0) | 2016.08.11 |
Spring Boot 시작하기 - #5_2. 이클립스 Srping boot 웹 프로젝트 생성 / 가동 (3) | 2016.08.08 |
Spring Boot 시작하기 - #5_1. 이클립스 Spring Boot 시작하기 (0) | 2016.08.08 |