일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 밸류즈
- 배포
- Typesciprt
- ui탬플릿
- 로그인 로직
- register
- 관리자페이지
- 스프링시큐리티
- mypage
- Update
- 파생상품평가
- react
- 이미지 업로드
- stock option
- 로그인
- 마이페이지
- MRC
- jsonwebtoken
- RCPS
- 빌드 및 배포
- 공통메서드
- Styled Components
- Token
- Ajax
- 인증처리
- userManagement
- 캘린더 라이브러리
- 밸류즈 홈페이지
- 회원가입로직
- 달력 라이브러리
- Today
- Total
I T H
[Spring Boot] 4. 로그인 구현 (DB 연결 테스트) (Week 2) 본문
Mybatis (참고)
자바의 관계형 데이터베이스 프로그래밍을 좀 더 쉽게 할 수 있게 도와주는 개발 프레임워크
복잡한 JDBC 코드를 걷어내며 깔끔한 소스코드를 유지할 수 있다.
객체(Object)와 SQL 사이에서 자동 맵핑을 도와주는 프레임워크
[ build.gradle 설정 ]
spring boot 프로젝트에 mybatis 설정을 위한 의존성 추가 및
오라클 데이터베이스 연결을 위한 jdbc 의존성 추가
build.gradle 파일에 아래 내용 추가
build.gradle 파일 수정 후 프로젝트 우측 마우스 클릭 > gradle > refresh 적용
경로 )
프로젝트 하위
// mybatis
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'
// jdbc
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'com.oracle.ojdbc:ojdbc8:19.3.0.0'
compileOnly 'com.oracle.ojdbc:orai18n:19.3.0.0'
[ application.properties 설정 ]
경로 )
src\main\resources\application.properties
# ORACLE
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/xe
spring.datasource.username=[id]
spring.datasource.password=[password]
# MyBatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com/info/fastboard/**/domain/**
[ 테스트용 관리자 계정 insert ]
INSERT INTO INFO.CRT_USER_INFO
SELECT 'admin'
, '1234'
, '관리자'
, 'ROLE_ADMIN'
, '1'
, 'Y'
, SYSDATE
FROM DUAL
연결 테스트를 위한 컨트롤러 및 매퍼 인터페이스를 구현하여 작성
[ TestController.java ]
경로 )
src\main\java\com\info\fastboard\test\controller\TestController.java
package com.info.fastboard.test.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.info.fastboard.test.persistence.TestMapper;
@Controller
public class TestController {
@Autowired
private TestMapper testMapper;
@GetMapping("/test")
public String main() {
return "test";
}
@ResponseBody
@GetMapping("/testData")
public String getTestDbConn() {
Map<String, Object> testMap = new HashMap<String, Object>();
Map<String, Object> resultMap = testMapper.findUserInfo(testMap);
System.out.println("resultMap =====================================");
System.out.println(resultMap);
return resultMap.toString();
}
}
[ TestMapper.java ]
경로 )
src\main\java\com\info\fastboard\test\persistence\TestMapper.java
package com.info.fastboard.test.persistence;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TestMapper {
// 테스트용 사용자 정보 조회
Map<String, Object> findUserInfo(Map<String, Object> map);
}
[ TestMapper.xml ]
경로 )
src\main\resources
폴더 아래에 mapper 폴더 생성 후
src\main\resources\mapper\TestMapper.xml
<?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.info.fastboard.test.persistence.TestMapper">
<!-- 테스트용 사용자 정보 조회 -->
<select id="findUserInfo" parameterType="hashmap" resultType="hashmap">
SELECT *
FROM INFO.CRT_USER_INFO
</select>
</mapper>
[ html 경로를 static 에서 templates로 변경하여 사용하기 ]
기본적으로 SpringBoot 에서는 static 폴더 아래를 정적 파일 리소스 경로로 체크하고 있음
static 폴더는 css 파일 등의 리소스 자원만 모아놓고
html 파일은 templates 폴더 아래에서 생성 및 관리하기 위해 아래와 같이 의존성 추가하여 사용
경로 )
build.gradle 파일 수정 (bulid.gradle은 pom.xml처럼 라이브러리 추가하는 설정파일)
// thymeleaf
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
- 1) 컨트롤러에서 ~.html을 쓰지 않고 리턴시 ex) "login.html" -> "login"만 쓰고 호출할수 있도록 함
- 2) templates 경로를 자동으로 바라보게 하는 기능(자동으로 html파일을 찾아줌)
[ test.html ]
경로 )
src\main\resources\templates\test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring Boot Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
// ajax get call을 통해 main url을 호출 후 전달되는 값을 화면에 출력
$.ajax({
type: "GET",
url: "/testData",
success: (data) => {
$('#divContent').html(data);
}
});
</script>
</head>
<body>
컨트롤러를 이용하여 서버에서 응답값을 받아옴
<div id="divContent">
</div>
</body>
</html>
연결이 정상적으로 되는지 확인하기 위해 프로젝트를 실행한 후
브라우저 접속에 http://localhost:8092/test 로 접속

브라우저 접속 테스트
'Spring Basic' 카테고리의 다른 글
[Spring Boot] 3. 테이블 설계 (Week 1) (0) | 2024.01.24 |
---|---|
[Spring boot] 2. Spring Boot 설정 (Week 1) (0) | 2024.01.24 |
[Spring Boot] 1. 계획 수립 / Spring Boot 시작하기 (Week 1) (0) | 2024.01.24 |
[스프링프로젝트연습 19] 스프링 프로젝트 구현 - Spring Security (0) | 2024.01.22 |
[스프링프로젝트연습 18] 스프링 프로젝트 구현 - 관리자페이지: 이미지 업로드 (0) | 2024.01.22 |