본문 바로가기

웹개발/스프링 프레임워크

Spring Boot 프로젝트 만들기 - 3편

Spring Boot 프로젝트 만들기 - 3편


MyBatis 와 DB 연결


* MariaDB 설치 방법 : http://bulkywebdeveloper.tistory.com/admin/entry/post/?id=65


1. dependencies 추가

mybatis, mariadb dependencies 추가(mybatis 있으면 추가 안해도됨)



compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0')

compile 'org.mariadb.jdbc:mariadb-java-client:2.0.1'


2. Mybatis, DB 설정

src > main > resources > application.properties


스프링부트의 큰 장점이 application.properties를 이용하여 설정을 쉽게 할 수 있습니다. .을 통하여 힌트도 제공됨



개인 설정에 맞춰서 넣어 주시면 됩니다.


#MyBatis setting

mybatis.mapper-locations=

mybatis.type-aliases-package=


# MariaDB connection information 

spring.datasource.driverClassName=org.mariadb.jdbc.Driver

spring.datasource.url=

spring.datasource.username=

spring.datasource.password=


3. 경로에 맞게 xml파일 생성

* com.bulky.mapper 라고 되어 있는게 하나의 폴더가 아닙니다 com > bulky > mapper > hello.xml 입니다

폴더를 하나씩 생성해서 들어가야 합니다. (com폴더 안에 bulky폴더 안에 mapper폴더 안에 hello.xml)



4. Service, Dao 클래스 생성



이제 순서데로 코드 작성 하도록 하겠습니다.


클라이언트에서 localhost:8080/getNow 접속 -> controller -> service -> dao -> xml(mariaDB 쿼리수행)


5. HelloController.class - getNow 메소드 작성




5. HelloService.class 



function test(){ @Service public class HelloService { @Autowired HelloDao helloDao; public String getNow(){ return helloDao.getNow(); } }

6.HelloDao.class



function test(){ @Component public class HelloDao { @Autowired SqlSession sqlSession; public String getNow(){ return sqlSession.selectOne("getNow"); } }

7.hello.xml



function test(){