2017년 7월 3일 월요일

Spring Boot Mybatis 연습


소스:

https://github.com/julyinsung/springboot/tree/master/boot_mvc_mybatis

환경:

Type: gradle
Packaging: Jar
Java Version: 1.8,
Boot Version: 1.5.4
MyBatis, MySQL, Thymeleaf, Web


참조:

https://github.com/sivaprasadreddy/springboot-tutorials/tree/master/springboot-mybatis-demo
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
http://sivalabs.in/2016/03/springboot-working-with-mybatis/
http://www.donnert.net/82
http://jsonobject.tistory.com/225

개요:

Mybatis를 설정한다.

설명:

프로젝트 구성은 그림과 같이 controller, domain, mapper, service 그리고 mapper XML파일은  resources 밑에 위치한다.


application.properties에 설정하는 것만으로 DB 및 Mybatis 연동이 끝난다.
  1. # ----------------------------------------------------------------------------------------------------
  2. # DataSource Config
  3. # 설정 참조 http://jsonobject.tistory.com/225
  4. # ----------------------------------------------------------------------------------------------------
  5. spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
  6. #spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
  7. spring.datasource.url=jdbc:mysql://localhost:3306
  8. spring.datasource.username=username
  9. spring.datasource.password=password
  10. spring.datasource.connectionProperties=useSSL=false;characterEncoding=UTF-8
  11. spring.datasource.initSQL=SELECT 1
  12.  
  13.  
  14. # ----------------------------------------------------------------------------------------------------
  15. # Mybatis Config: http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
  16. # ----------------------------------------------------------------------------------------------------
  17. mybatis.type-aliases-package=mysample.boot.mybatis.domain
  18. mybatis.mapper-locations=classpath*:/mapper/**/*.xml
mybatis.mapper-locations 는 mapper XML 위치를 설정하는 config로 위 구성과 같이 resources밑에 둔다. spring.datasource.driverClassName 를 지정하지 않으면 기본값이 자동 지정된다.

마지막으로 위 구성과 같이 @Mapper 어노테이션을 사용하는 UserMapper.java 인터페이스와 query를 작성하는 XML 파일을 생성하면 된다.

댓글 없음:

댓글 쓰기