JAVA Developer Training
29. Oracle 연동 ( 게시판 만들기 ) 본문
1. 프로젝트 생성
extendsion - spring boot extention pack 설치
파일들 위치 확인
static -> css,js 생성후 복사붙여넣기 하기
해당 css, js파일은 부트스트랩에서 다운받아서 가져왔다.
static - css,js
templates - html 이라 생각하면 편할것
application.properties 서버 가동직전 1회 읽고 실행함
만약 이곳(환경설정) 을 수정하려고 한다면 기존 가동중인 서버를 중지하고 수정후 서버를 재가동 해야한다.
2. pom.xml 설정
<?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.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>boot_20210914</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot_20210914</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<!-- oracle -->
<repositories>
<repository>
<id>oracle</id>
<name>ORACLE JDBC Repository</name>
<url>http://maven.jahia.org/maven2</url>
</repository>
</repositories>
<dependencies>
<!-- oracle-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- jsp에서 사용하는 jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- jsp에서 사용 thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- web 개발용 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- tomcat embedded was -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- 소스 코드 변경시 was 자동 구동 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 단위 테스트 도구 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
repository 와 dependency 추가
3.application.properties 설정
# 이곳은 환경설정 파일, 이파일을 수정했을시 재시작을 해야한다
# 포트 번호
server.port=8080
server.servlet.context-path=/ROOT
# 소스코드 갱신시 자동 실행
spring.devtools.livereload.enabled=true
#
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.jsp
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@1.234.5.158:11521/xe
spring.datasource.username=id318
spring.datasource.password=pw318
#컨트롤러 최초 적용시 update 가 아닌 create-drop
#ex) SEQ_NO를 오라클에 적용시켜 만든후 업데이트로 변경하면 추가될 테이블,seq_no가 변경되는식
spring.jpa.hibernate.ddl-auto=update
4. App.java
package com.example.boot_20210914;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
//@ 추가기능들, 각각 컨트롤러,엔티티,레퍼지토리를 연결해 주는 기능
@SpringBootApplication
@ComponentScan(basePackages = { "com.example.controller" })
@EntityScan(basePackages = { "com.example.entity" })
@EnableJpaRepositories(basePackages = { "com.example.repository" })
public class Boot20210914Application {
public static void main(String[] args) {
SpringApplication.run(Boot20210914Application.class, args);
System.out.println("start server");
}
}
5. BoardController
package com.example.controller;
import java.util.List;
import com.example.entity.Board;
import com.example.repository.BoardRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/board")
public class BoardController {
// 저장소 객체 생성
@Autowired
private BoardRepository boardRepository;
// 127.0.0.1:8080/ROOT => /board/select
@RequestMapping(value = "/select", method = RequestMethod.GET)
public String select(Model model) {
// 정렬안함
List<Board> list = boardRepository.findAll();
// 글번호를 최신순으로
List<Board> list1 = boardRepository.findAll(Sort.by(Sort.Direction.DESC, "no"));
model.addAttribute("list", list);
return "board_select";
}
// 127.0.0.1:8080/ROOT/board/insert
@RequestMapping(value = "/insert", method = RequestMethod.GET)
public String insertGet() {
return "board_insert";
}
// 127.0.0.1:8080/ROOT/board/insert
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public String insertPost(@ModelAttribute Board board) {
System.out.println(board.toString());
boardRepository.save(board);
return "redirect:select";
}
}
6. BoardRepository
package com.example.repository;
import com.example.entity.Board;
import org.springframework.data.jpa.repository.JpaRepository;
//클래스가 아닌 인터페이스
public interface BoardRepository extends JpaRepository<Board, Long> {
}
7. HomeController
package com.example.controller;
import java.util.ArrayList;
import java.util.List;
import com.example.entity.Board;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//컨트롤러 내에는 같은게있으면안된다 구동자체가 안됨
@Controller
public class HomeController {
// http://127.0.0.1:8080/ROOT/
// http://127.0.0.1:8080/ROOT/home
@RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
public String home(Model model) {
// 전달할 값
String title = "제목입니다.";
int num = 120;
// 엔티티 객체
Board board = new Board();
board.setNo(1L);
board.setTitle("가나다");
board.setContent("내용");
Board board1 = new Board();
board1.setNo(1L);
board1.setTitle("가나다");
board1.setContent("내용");
Board board2 = new Board();
board2.setNo(1L);
board2.setTitle("가나다");
board2.setContent("내용");
List<Board> list = new ArrayList<>();
list.add(board);
list.add(board1);
list.add(board2);
// jsp로 전달
model.addAttribute("li", list);
model.addAttribute("ti", title);
model.addAttribute("nu", num);
model.addAttribute("obj", board);
return "home"; // home.jsp 표시
}
}
Oracle
'트레이닝' 카테고리의 다른 글
31. Spring ( 로그인, 권한 설정 ) (0) | 2021.09.23 |
---|---|
30. Spring 게시판 작성 ( Oracle 이미지 연동 ) (0) | 2021.09.16 |
28. cron,agent 시간별 자동화, 접속자 확인기능 (0) | 2021.09.13 |
27. 데이터 크롤링 ( node.js, selenium ) (0) | 2021.09.10 |
26. socket.io를 이용한 실시간 채팅 기초 틀 (0) | 2021.09.08 |