soominkim Study
article thumbnail
728x90

 

취미로 크립토투자를 하고있습니다. 해외 거래소인 경우 거래소별 상장해 있는 코인이 크게 차이가 나다보니 거래소별로 거래를 하는 경우가 많았습니다. 그러다보니 거래소별로 하나씩 들어가 엑셀로 다운받아 매매일지를 만들기도 힘들고 이참에 API를 활용해 웹 서비스 형태로 통합관리를 하면 괜찮지 않을까 생각이 들었습니다. 본 프로젝트는 오픈 템플릿을 활용하여 제작합니다.

 

프레임워크는 SpringBoot를 이용합니다. 또한 thymeleaf를 활용합니다. 빌드 도구는 Gradle입니다. 데이터 베이스는 많은 고민을 했지만 MySQL를 이용합니다.  application.properties에 설정하기 전에 build.gradle에서 thymeleaf를 설정합니다.

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

먼저 thymeleaf 설정을 간단하게 합니다.

server.port=4040
spring.devtools.restart.enabled=true

spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.check-template-location=true
spring.thymeleaf.cache=false

이어서 MySQL 설정을 합니다. 먼저 build.gradle에서 mysql-connector를 설정합니다.

runtimeOnly 'com.mysql:mysql-connector-j'

application.properties도 설정합니다.

 

spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/<databasename>?useSSL=false&useUnicode=true&serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true
spring.datasource.username=<username>
spring.datasource.password=<password>

이제 mybatis 사용에 필요한 의존성을 추가합니다. 

implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.3.0'

그리고 mybatis에 대해 필요한 것을 application.properties에서 설정합니다.

# mybatis
mybatis.mapper-locations: mybatis/mapper/*.xml

thymeleaf를 사용하기 때문에 페이지에 html 확장자를 사용하고 위치는 templates 아래에 index.html를 생성합니다. 그리고 index.html 파일상단에 다음을 추가합니다.

<html xmlns:th="http://www.thymeleaf.org">

 

PageController를 만들고 다음과 같이 작성합니다.

package com.JavaProject01.controller;



import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import jakarta.servlet.http.HttpSession;



@Controller
public class PageController {
	
	@GetMapping("/")
	public String index()  {
		return "index";
	}
	@GetMapping("/login")
	public String login() {
		return "login";
	}
	@GetMapping("/register")
	public String register() {
		return "register";
	}
	@GetMapping("/password")
	public String password() {
		return "password";
	}
}

마지막으로 projectApplication.java와 다른 폴더에 위치하기 때문에  projectApplication.java@ComponentScan으로 PageController의 패키지명을 작성합니다. 

package JavaProject01;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@ComponentScan("com.JavaProject01.controller")
@MapperScan("com.JavaProject01.mapper")
@SpringBootApplication
public class JavaProject01Application {

	public static void main(String[] args) {
		SpringApplication.run(JavaProject01Application.class, args);
	}
}

 

 

본 글은 웹프로젝트의 기록물입니다.

This article is a record of the web project.

728x90
profile

soominkim Study

@soominkim

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그