그 전 react, springboot 한 파일에서 작업하기
application.properties
에 유저이름, 테이블명, 패스워드 mysql과 비교해서 넣기spring.datasource.url=jdbc:mysql://localhost:3306/festibook?&serverTimezone=UTC&autoReconnect=true&allowMultiQueries=true&characterEncoding=UTF-8
spring.datasource.username=Festibook
spring.datasource.password=f112
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:/mappers/*.xml
mybatis.type-aliases-package=com.in4mation.festibook
// application.properties 에!!
mybatis.mapper-locations=classpath:/mappers/*.xml
🔗 데이터 넣는 법 참고 : https://jellfyu.tistory.com/2
Mysql
에 지정한 곳에 더미 데이터(예시 데이터)와 테이블 넣어주기CREATE TABLE demo_table
(
DEMO_ID VARCHAR(32) NULL,
DEMO_NAME VARCHAR(32) NULL
);
INSERT INTO demo_table VALUES("ID1", "DEMO_NAME");
INSERT INTO demo_table VALUES("ID2", "DEMO_NAME2");
INSERT INTO demo_table VALUES("ID3", "DEMO_NAME3");
@Data
를 이용해 get,set 메소드 만들어줌package com.in4mation.festibook.domain;
import lombok.Data;
@Data
public class DemoVo {
private String demo_id;
private String demo_name;
}
인터페이스
임)package com.in4mation.festibook.repository;
import com.in4mation.festibook.domain.DemoVo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface DemoMapper {
List<DemoVo> select();
void insert();
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.in4mation.festibook.repository.DemoMapper">
<select id="select" resultType="com.in4mation.festibook.domain.DemoVo">
SELECT *
FROM demo_table
</select>
<insert id="insert">
INSERT INTO demo_table(
DEMO_ID
,DEMO_NAME
) VALUES(
#{demo_id}
,#{demo_name}
)
</insert>
</mapper>
packagecom.in4mation.festibook.service;
import com.in4mation.festibook.domain.DemoVo;
import com.in4mation.festibook.repository.DemoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional
public classDemoService {
@Autowired
private DemoMappermapper;
public List<DemoVo> select() {
return mapper.select();
}
public void insert(DemoVo vo) {
mapper.insert();
}
}
package com.in4mation.festibook.controller;
import com.in4mation.festibook.domain.DemoVo;
import com.in4mation.festibook.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class DemoController {
@Autowired
private DemoService demoService;
@GetMapping("select")
public List<DemoVo> getSelectList(){ return demoService.select(); }
@GetMapping("insert")
public void insertDemoVo(){
DemoVo vo = new DemoVo();
vo.setDemo_id("내가만든dummy_id");
vo.setDemo_name("내가만든dummy_name");
demoService.insert(vo);
}
}
axios
를 연동해야 함npm install axios
import React, { useState, useEffect } from 'react';
import axios from 'axios';
function Community() {
const [data, setData] = useState([]);
useEffect(() => {
// 데이터를 가져옵니다.
axios.get('http://localhost:8080/select')
.then(response => {
setData(response.data);
})
.catch(error => {
console.error("There was an error fetching the data!", error);
});
}, []); // 빈 의존성 배열을 사용하여 마운트 시 한 번만 실행
return (
<div>
<h1>커뮤니티페이지</h1>
<ul>
{data.map(item => (
<li key={item.demo_id}>
{item.demo_name}
</li>
))}
</ul>
</div>
);
}
export default Community;
잘 들어간 걸 확인했다면 잘 연동이 된 것일 것이다!
@CrossOrigin(origins = "http://localhost:3000")
를 해줘야 한다고 한다.클래스
전체로 (DemoController)package com.in4mation.festibook.controller;
import com.in4mation.festibook.domain.DemoVo;
import com.in4mation.festibook.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@CrossOrigin(origins = "http://localhost:3000")
@RestController
public class DemoController {
@Autowired
private DemoService demoService;
@GetMapping("select")
public List<DemoVo> getSelectList(){ return demoService.select(); }
@GetMapping("insert")
public void insertDemoVo(){
DemoVo vo = new DemoVo();
vo.setDemo_id("내가만든dummy_id");
vo.setDemo_name("내가만든dummy_name");
demoService.insert(vo);
}
}
@RestController
@CrossOrigin(origins = "http://localhost:3000")
public class DemoController {
// ... (생략)
}
WebMvcConfigurer
를 구현하여 전역 CORS 설정을 추가import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000")
.allowedMethods("GET", "POST", "PUT", "DELETE");
}
}
기능명
패키지 적고 그 안에서 자바 파일 작업3-teamplates
에서 받아야 합니당git clone -b {branch_name} --single-branch {저장소 URL}
ex) git clone -b javajigi --single-branch https://github.com/javajigi/java-racingcar
GitHub - GoldenPearls/teamlogin at 3-templates
package com.in4mation.festibook.controller.front;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/{path:[^\\.]*}")
public String redirect() {
return "forward:/";
}
}
잘 들어가 진다.