[springboot] oracle 연결+API

https://github.com/kimxminsu/start-extjs

environment: #springboot #maven #oracle #eclipse(sts) #java

  • spring starter project 생성
  1. File-New-Other-Spring Starter Project

  2. Name: API, Type: Maven, Packaging: Jar, Java Version: 8, Language: Java, Group: com.api, Package: com.api

  3. Dependency(의존성): JDBC API, Oracle Driver, Spring Web


  • spring boot 실행
  1. pom.xml 확인
    (spring web 미선택시 실행하자마자 중단됨)
    참고: https://stackoverflow.com/questions/22380119/why-does-my-spring-boot-app-always-shutdown-immediately-after-starting
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
	</dependency>
**	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>**
	<dependency>
		<groupId>com.oracle.database.jdbc</groupId>
		<artifactId>ojdbc8</artifactId>
		<scope>runtime</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>
  1. 프로젝트 우클릭-Run As-Spring Boot App
  2. localhost:8080 접속
  1. API\src\main\java\com\API\Controller\TestController 생성
package com.db.Controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@RequestMapping(value="/test")
	public Map<String,Object> test() {
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("name", "happy");
		result.put("age", 10);
		return result;
	}
}

@RestController, @RequestMapping : ctrl+space, enter
HashMap : ctrl+shift+o
2. 실행
3. localhost:8080/test 접속

  1. oracle-table 생성

create table test(
    id int primary key,
    name varchar(20),
    email varchar(20)
);

insert into test(id, name, email)
values(1,'happy','[email protected]');

select * from test;
  1. application.properties 수정
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/orcl
spring.datasource.username=system
spring.datasource.password=1234
  1. pom.xml 수정
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-jdbc</artifactId>
	<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
	<groupId>com.oracle.database.jdbc</groupId>
	<artifactId>ojdbc8</artifactId>
	<scope>runtime</scope>
</dependency>
  1. api/controller/TestController, api/dao/TestDao, api/service/TestService
  1. 실행
  2. localhost:8080/getList 접속

좋은 웹페이지 즐겨찾기