일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- vsCode
- centos
- MAC
- 센토스
- jest
- TensorFlow
- 맥
- avds
- PYTHON
- xcode
- localserver
- 개발
- unittest
- 리눅스
- ReactNative
- androidstudio
- webpack
- react
- linux
- IOS
- 네트워크
- qunit
- MachineLearning
- node
- VirtualBox
- Android
- build
- picker
- Chrome
- Today
- Total
로메오의 블로그
[Spring Boot] Spring Boot - My Batis 연동 - Oracle 본문
Spring Boot / Oracle / Mybatis 차례
pom.xml
Oracle 연동을 위해 필요한 라이브러리를 추가합니다.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gaeyou</groupId>
<artifactId>firstProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>firstProject</name>
<description>Demo project for Spring Boot</description>
<!-- OJDBC 드라이버 레포지토리 -->
<repositories>
<repository>
<id>oracle</id>
<url>http://maven.jahia.org/maven2</url>
</repository>
</repositories>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<!-- Spring boot JDBC Library -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- Connection pool Library -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- Oracle JDBC Driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.2</version>
</dependency>
<!-- MyBatis Library -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- Spring MyBatis 연동 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
<packaging>war</packaging>
</project>
Maven > Update Project
Force Update of Snapshots를 체크하고 Maven을 업데이트 합니다.
application.properties
# Database
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/xe
spring.datasource.username=dev_user
spring.datasource.password=1234
# server.port=80
# server.error.whitelabel.enabled=false
#### jsp 사용할 경우
# spring.mvc.view.prefix=/WEB-INF/views/
# spring.mvc.view.suffix=.jsp
# server.jsp-servlet.init-parameters.development=true
#### thymeleaf 사용할 경우
# spring.thymeleaf.cache=false
Oracle DB 접속 정보를 입력합니다.
PersonModel.java
com/gaeyou/firstproject/model/PersonModel.java
model은 Oracle에서 받아오는 컬럼과 동일한 데이터를 선언합니다.
package com.gaeyou.firstproject.model;
import lombok.Data;
@Data
public class PersonModel {
private int idx;
private String name;
private String email;
private String passwd;
private String content;
}
PersonDao.java
com/gaeyou/firstproject/dao/PersonDao.java
dao는 spring과 mapper를 연결해줍니다.
package com.gaeyou.firstproject.dao;
import com.gaeyou.firstproject.model.PersonModel;
public interface PersonDao {
PersonModel getPerson();
}
DataSourceConfig.java
com/gaeyou/firstproject/config/DataSourceConfig.java
application.properties에 선언되어 있는 spring.datasource.* 의 값으로 DataSource를 build 합니다.
package com.gaeyou.firstproject.config;
import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DataSourceConfig {
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
DataAccessConfig.java
com/gaeyou/firstproject/config/DataAccessConfig.java
myBatis로 SqlSessionFactoryBean을 생성합니다.
package com.gaeyou.firstproject.config;
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;
@Configuration
@MapperScan(basePackages = "com.gaeyou.firstproject.dao")
public class DataAccessConfig {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/*.xml")
);
return sessionFactory.getObject();
}
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
classpath:mybatis/mapper/*.xml
위 경로는 mapper xml 경로입니다.
main.xml
src/main/resources/mybatis/mapper/main.xml
query를 정의합니다.
<?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.gaeyou.firstproject.dao.PersonDao">
<select id="getPerson" resultType="com.gaeyou.firstproject.model.PersonModel">
SELECT
IDX
, NAME
, EMAIL
, PASSWD
, CONTENT
FROM BOARD
ORDER BY IDX DESC
</select>
</mapper>
PersonService.java
com/gaeyou/firstproject/service/PersonService.java
service interface를 정의합니다.
package com.gaeyou.firstproject.service;
import com.gaeyou.firstproject.model.PersonModel;
public interface PersonService {
PersonModel getPerson();
}
PersonServiceImpl.java
com/gaeyou/firstproject/service/imp/PersonServiceImpl.java
service 구현체입니다.
package com.gaeyou.firstproject.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gaeyou.firstproject.dao.PersonDao;
import com.gaeyou.firstproject.model.PersonModel;
import com.gaeyou.firstproject.service.PersonService;
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonDao personDao;
@Override
public PersonModel getPerson() {
PersonModel person = personDao.getPerson();
return person;
}
}
PersonController.java
com/gaeyou/firstproject/controller/PersonController.java
view와 model을 연결하는 route를 구현합니다.
package com.gaeyou.firstproject.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gaeyou.firstproject.model.PersonModel;
import com.gaeyou.firstproject.service.PersonService;
@CrossOrigin
@RestController
@RequestMapping("/persons")
public class PersonController {
@Autowired
PersonService personService;
@RequestMapping("/all")
public PersonModel getAll() {
return personService.getPerson();
}
}
서버구동
길고 길었던 Spring boot 프로젝트의 한 사이클이 완전히 끝났습니다.
Oracle에서 데이터를 가지고 오는것만 해봤는데
데이터 넣고 수정하는 응용편은 별도로 다루지 않겠습니다.
빠이~
(댓글도 좀 ㅋㅋ)
'Backend > Spring' 카테고리의 다른 글
[Spring boot] mybatis SQL 로그 찍기 - log4jdbc (0) | 2019.10.12 |
---|---|
[Spring Boot] 프로젝트 구조 정리하기 (0) | 2019.09.21 |
[Eclipse] Debug shell 사용하기 (0) | 2019.08.28 |
[Spring Boot] lombok 설정하기 - Mac / Windows (0) | 2019.08.24 |
[Spring Boot] Logback 설정하기 (2) | 2019.08.23 |