Spring + React +MyBatis(MySQL)
Project: SpringReactProject
Package: com.spring.SpringReactProject
이렇게 잘 뜨면 프로젝트 잘 만들어진거다 !!
pom.xml
<!-- ********************************************************* -->
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.37</version>
</dependency>
<!-- Connection Pool -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.11.0</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.1.2</version> <!-- 3.0.3 버전을 쓰려면 Spring은 6.x 이어야하기 떄문 !! -->
</dependency>
<!-- JSON -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<!-- ********************************************************* -->
HomeController.java
package com.spring.SpringReactProject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping(value="/")
public String index() {
return "index";
}
}
servlet-context.xml
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/" />
<beans:property name="suffix" value=".html" />
</beans:bean>
views 폴더 지우기 !
WEB-INF
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello
</body>
</html>
D:\Spring\workspace\SpringReactProject\
D:\Spring\workspace\SpringReactProject
터미널 열기
cd front
npm start
yes
App.js
import React from 'react';
const App = () => {
return (
<div>
<h1>Hello React!!</h1>
</div>
);
};
export default App;
npm start
yes
SpringReactProject
webapp
WEB-INF
front(React Project)
src
css
style.css
components
Main.jsx
Login.jsx
style.css
.menunav {width: 100%; background: #efefef; padding: 25px 0;}
.menunav ul {display: flex; justify-content: center;}
.menunav ul li {list-style: none; margin-right: 30px;}
.menunav ul li a {transition: 0.3s;}
.menunav ul li a:hover {color: tomato;}
.main {width: 1000px; margin: 50px auto; text-align: center;}
.main h1 {font-size: 70px; margin-bottom: 15px;}
.main h2 {font-size: 50px;}
.products {width: 1100px; margin: 50px auto;}
.products h2 {font-size: 40px; margin-bottom: 20px; font-weight: 600;}
.products div {display: flex; justify-content: space-between;}
.products article {border: 1px solid #dcdcdc; padding: 20px;}
.products article h3 {font-size: 25px; margin-bottom: 15px; text-align: center; color: tomato;}
.products article img {width: 200px;}
.item {width: 1000px; margin: 50px auto; text-align: center;}
.item h2 {font-size: 50px; margin-bottom: 20px; color: tomato;}
.item h3 {font-size: 30px; margin-bottom: 15px;}
.item img {width: 300px;}
.item p:last-of-type {margin-top: 20px; font-size: 20px; margin-bottom: 15px;}
.item button {width: 200px; height: 40px; background: #000; color: #fff; border: none;}
App.js
import React from 'react';
import {BrowserRouter, Routes, Route, Link} from 'react-router-dom';
import Main from './components/Main';
import Login from './components/Login';
import './css/style.css';
const App = () => {
return (
<BrowserRouter>
<>
<nav className='menunav'>
<ul>
<li><Link to='/' >메인화면</Link></li>
<li><Link to='/login' >Login</Link></li>
</ul>
</nav>
{/* 화면에 보이는 영역 */}
<Routes>
<Route path='/' element={ <Main/>} />
<Route path='/login' element={ <Login/>} />
</Routes>
</>
</BrowserRouter>
);
};
export default App;
Main.jsx
import React from 'react';
const Main = () => {
return (
<div>
<h3>Main</h3>
</div>
);
};
export default Main;
Login.jsx
import React from 'react';
const Login = () => {
return (
<div>
<h3>Login</h3>
</div>
);
};
export default Login;
Main.jsx
import React from 'react';
const Main = () => {
return (
<div className='main'>
<h1>짱구의 홈페이지</h1>
<h2>방문해주셔서 감사합니다.</h2>
<img src='https://mblogthumb-phinf.pstatic.net/MjAyMDA5MTJfOTgg/MDAxNTk5OTA0OTYyMTg4.m_gfYk5un7sSElyWaR5coC51DQhXC8CSRNdtEjFmnsEg.nhkJTCFAzxFcGc63lbnmB1SUuprO3IlHS7sujKzKZ5Yg.JPEG.kn010123/IMG_1512.JPG?type=w800'
width={500} height={300}
alt='메인사진' />
</div>
);
};
export default Main;
npm start
Login.jsx
axios.get('http://localhost:8080/SpringReacrProject/login')
위에 있는 경로로 해야한다.
import axios from 'axios';
import React, { useEffect, useState } from 'react';
const Login = () => {
const [str, setStr] = useState('');
useEffect(() => {
axios.get('http://localhost:8080/SpringReactProject/login')
.then(res => setStr(res.data))
}, []);
return (
<div>
<h3>Login</h3>
<h4>{ str }</h4>
</div>
);
};
export default Login;
HomeController.java
package com.spring.SpringReactProject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HomeController {
@RequestMapping(value="/")
public String index() {
return "index";
}
@RequestMapping(value = "/login")
@ResponseBody
public String login() {
return "Hello Login!!";
}
}
vscode에서는 3000포트인데
위에서 8080포트를 요청하므로 에러가 뜬다.
HomeController.java
@CrossOrigin 추가하기
package com.spring.SpringReactProject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@CrossOrigin
public class HomeController {
@RequestMapping(value="/")
public String index() {
return "index";
}
@RequestMapping(value = "/login")
@ResponseBody
public String login() {
return "Hello Login!!";
}
}
위에 만든거 보류~~~~~~~~~~~~~~~~~~~~~~~~~~
Spring Framwork + MyBatis(MySQL) + React
- Spring Framwork, React 프로젝트 따로 만든다.
[ Spring Framwork 프로젝트]
SpringReactProject
com.spring.SpringReactProject
- http://localhost:8080
http://localhost:3000
하나는 8080 하나는 3000으로 따로 만들어서 요청할 것이다.
스프링부트할 때는 합쳐서 만들 것이다.
밑에 세 개 파일만 보관하고 삭제할 것
지운다.
SpringReactProject
com.spring.SpringReactProject
새로만든다.
run on server
[ Spring Framework 프로젝트 ]
SpringReactProject
[React 프로젝트 ]
frontend
src
css
style.css
components
Main.jsx
Login.jsx
npm start
App.js
import React from 'react';
import {BrowserRouter, Routes, Route, Link} from 'react-router-dom';
import Main from './components/main/Main';
import LoginForm from './components/member/LoginForm';
import './css/style.css';
import './css/login.css';
const App = () => {
return (
<BrowserRouter>
<>
<nav className='menunav'>
<ul>
<li><Link to='/' >메인화면</Link></li>
<li><Link to='/loginForm' >로그인</Link></li>
</ul>
</nav>
{/* 화면에 보이는 영역 */}
<Routes>
<Route path='/' element={ <Main/>} />
<Route path='/loginForm' element={ <LoginForm/>} />
</Routes>
</>
</BrowserRouter>
);
};
export default App;
frontend
src
css
style.css
components
Main.jsx
member
LoginForm.jsx
board
Main.jsx
import React from 'react';
const Main = () => {
return (
<div className='main'>
<h1>짱구의 홈페이지</h1>
<h2>방문해주셔서 감사합니다.</h2>
<p><img src='https://mblogthumb-phinf.pstatic.net/MjAyMDA5MTJfOTgg/MDAxNTk5OTA0OTYyMTg4.m_gfYk5un7sSElyWaR5coC51DQhXC8CSRNdtEjFmnsEg.nhkJTCFAzxFcGc63lbnmB1SUuprO3IlHS7sujKzKZ5Yg.JPEG.kn010123/IMG_1512.JPG?type=w800'
width={500} height={300}
alt='메인사진' />
</p>
</div>
);
};
export default Main;
LoginForm.jsx
import React from 'react';
const LoginForm = () => {
return (
<div className='main'>
zzz
</div>
);
};
export default LoginForm;
member 테이블 이용해보자 !!
LoginForm.module.css
.login-container {
display: flex;
justify-content: center;
align-items: center;
height: 50vh;
flex-direction: column;
}
.login-box {
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
width: 300px;
box-shadow: 0px 4px 10px rgba(82, 80, 80, 0.1);
}
.login-title {
margin-bottom: 10px;
font-weight: bold;
}
.login-input {
width: 100%;
padding: 8px;
box-sizing: border-box;
border-radius: 4px;
border: 1px solid #ddd;
margin-bottom: 10px;
}
.login-button {
width: 100%;
padding: 10px;
border-radius: 4px;
border: none;
background-color: #f8c1e8;
color: black;
font-weight: bold;
cursor: pointer;
}
LoginForm.jsx
import { useState } from 'react';
import styles from '../../css/LoginForm.module.css';
const LoginForm = () => {
const [id, setId] = useState('');
const [pwd, setPwd] = useState('');
return (
<div className={styles['login-container']}>
<form className={styles['login-box']}>
<div>
<label className={styles['login-title']}>아이디</label>
<input type="text" className={styles['login-input']} name='id' value={ id } onChange={ e => setId(e.target.value) } />
</div>
<div>
<label className={styles['login-title']}>비밀번호</label>
<input type="password" className={styles['login-input']} name="pwd" value={ pwd } onChange={ e => setPwd(e.target.value) }/>
</div>
<button className={styles['login-button']}>로그인</button>
</form>
</div>
);
};
export default LoginForm;
<button className={styles['login-button']} onClick={ onLoginSubmit }>로그인</button>
onClick 함수 설정
<div>
<label className={styles['login-title']}>아이디</label>
<input type="text" className={styles['login-input']} name='id' value={ id } onChange={ e => setId(e.target.value) } />
<div id='idDiv' className={styles['div']}>{ idDiv }</div>
</div>
idDiv 설정
<div>
<label className={styles['login-title']}>비밀번호</label>
<input type="password" className={styles['login-input']} name="pwd" value={ pwd } onChange={ e => setPwd(e.target.value) }/>
<div id='pwdDiv' className={styles['div']}>{ pwdDiv }</div>
</div>
pwdDiv 설정
.div {
color: red;
font-size: 10pt;
margin-bottom: 10px;
}
css 추가
const onLoginSubmit = (e) => {
e.preventDefault();
setIdDiv('');
setPwdDiv('');
if(!id)
setIdDiv('아이디를 입력해주세요');
else if(!pwd)
setPwdDiv('비밀번호를 입력해주세요.')
}
onLoginSubmit 함수
if(!id)
setIdDiv('아이디를 입력해주세요');
else if(!pwd)
setPwdDiv('비밀번호를 입력해주세요')
else{
axios.get('http://localhost:8080/')
.then()
}
이제 axios를 통해 SpringReactProject로 가야되는데 너무 이름이 길어서 이름부터 바꾸러 가자 !
Edit 버튼 눌러서 바꾸기
SpringReactProject
com.spring. SpringReactProject
member.controller
MemberController.java
MemberController.java
package member.controller;
import org.springframework.stereotype.Controller;
@Controller
public class MemberController {
}
컨트롤러로 잡아줬음으로 설정하러 가기 ~!
servlet-context.xml
<context:component-scan base-package="member.controller" />
MemberController.java
package member.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@CrossOrigin
@Controller
@RequestMapping(value="member")
public class MemberController {
//@RequestMapping(value="login", method = RequestMethod.GET)
@GetMapping(value = "login")
@ResponseBody
public String login() {
return "오나?";
}
}
일단 잘 연결됐나 확인하기위한 return 값 적어줌
if(!id)
setIdDiv('아이디를 입력해주세요');
else if(!pwd)
setPwdDiv('비밀번호를 입력해주세요')
else{
axios.get('http://localhost:8080/spring/member/login')
.then(res => alert(res.data))
}
아까 마저 쓰려던거 써주고 위에서 보낸 데이터 잘 오는지 alert창으로 찍어보자 !!
로그인
결과를 로그인 성공하면 "success" 보내서 메인화면을 띄운다.
로그인 실패하면 "fail" 보내서 로그인창 아래에 "아이디 또는 비밀번호가 틀렸습니다." 뜨게하기 !!
SpringReactProject
sprin.conf
SpringConfiguration.java
(spring 폴더 - db.properties 만들러가기)
(mapper 폴더 - memberMapper.xml 만들러가기)
member.controller
MemberController.java
member.service
MemberService.java (Interface)
member.service.impl
MemberServiceImpl.java
member.dao
MemberDAO.java (Interface)
root-context.xml에 설정하기
<context:component-scan base-package="member.bean" />
<context:component-scan base-package="member.dao" />
<context:component-scan base-package="member.service.impl"/>
<context:component-scan base-package="spring.conf" />
pom.xml -- 추가하기
<!-- ********************************************************* -->
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.37</version>
</dependency>
<!-- Connection Pool -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.11.0</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.1.2</version> <!-- 3.0.3 버전을 쓰려면 Spring은 6.x 이어야하기 떄문 !! -->
</dependency>
<!-- JSON -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.9.1</version>
</dependency>
<!-- ********************************************************* -->
SpringConfiguration.java
package spring.conf;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:spring/db.properties")
@MapperScan("**.dao")
public class SpringConfiguration {
private @Value("${jdbc.driver}") String driver;
private @Value("${jdbc.url}") String url;
private @Value("${jdbc.username}") String username;
private @Value("${jdbc.password}") String password;
@Autowired
private ApplicationContext context;
@Bean
public BasicDataSource dataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setDriverClassName(driver);
basicDataSource.setUrl(url);
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
return basicDataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
//sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("spring/mybatis-config.xml"));
//1개
//sqlSessionFactoryBean.setMapperLocations(new ClassPathResource("mapper/userMapper.xml"));
//Mapper.xml이 2개 이상일 때
/*
sqlSessionFactoryBean.setMapperLocations(
new ClassPathResource("mapper/userMapper.xml"),
new ClassPathResource("mapper/userUploadMapper.xml"));
*/
sqlSessionFactoryBean.setMapperLocations(context.getResources("classpath:mapper/*Mapper.xml"));
//sqlSessionFactoryBean.setTypeAliasesPackage("user.bean");
sqlSessionFactoryBean.setTypeAliasesPackage("*.bean");
return sqlSessionFactoryBean.getObject(); // SqlSessionFactory 변환 역할
}
@Bean
public SqlSessionTemplate sqlSession() throws Exception{
SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory());
return sqlSessionTemplate;
}
@Bean
public DataSourceTransactionManager transactionManager(){
DataSourceTransactionManager dataSourceTransactionManager =
new DataSourceTransactionManager(dataSource());
return dataSourceTransactionManager;
}
}
MemberDTO.java
package member.bean;
import java.util.Date;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class MemberDTO {
private String name;
private String id;
private String pwd;
private String gender;
private String email1;
private String email2;
private String tel1;
private String tel2;
private String tel3;
private String zipcode;
private String addr1;
private String addr2;
private Date logtime;
}
LoginForm.module.css
.login-container {
display: flex;
justify-content: center;
align-items: center;
height: 50vh;
flex-direction: column;
}
.login-box {
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
width: 300px;
box-shadow: 0px 4px 10px rgba(82, 80, 80, 0.1);
}
.login-title {
margin-bottom: 10px;
font-weight: bold;
}
.login-input {
width: 100%;
padding: 8px;
box-sizing: border-box;
border-radius: 4px;
border: 1px solid #ddd;
margin-bottom: 10px;
}
.login-button {
width: 100%;
padding: 10px;
border-radius: 4px;
border: none;
background-color: #f8c1e8;
color: black;
font-weight: bold;
cursor: pointer;
}
.div {
color: red;
font-size: 10pt;
margin-bottom: 10px;
}
LoginForm.jsx
import axios from 'axios';
import { useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import styles from '../../css/LoginForm.module.css';
const LoginForm = () => {
const [id, setId] = useState('');
const [pwd, setPwd] = useState('');
const [idDiv, setIdDiv] = useState('');
const [pwdDiv, setPwdDiv] = useState('');
const [loginDiv, setLoginDiv] = useState('');
const navigaete = useNavigate();
const onLoginSubmit = (e) => {
e.preventDefault();
setIdDiv('');
setPwdDiv('');
if(!id)
setIdDiv('아이디를 입력해주세요');
else if(!pwd)
setPwdDiv('비밀번호를 입력해주세요')
else{
axios.get(`http://localhost:8080/spring/member/login?id=${id}&pwd=${pwd}`)
.then(res => {
if (res.data === 'success') {
alert('로그인에 성공하였습니다.')
navigaete('/')
} else {
setLoginDiv('아이디 또는 비밀번호가 틀렸습니다')
}
})
}
}
return (
<div className={styles['login-container']}>
<h2 className={styles['board-list-title']}>로그인</h2>
<form className={styles['login-box']}>
<div>
<label className={styles['login-title']}>아이디</label>
<input type="text" className={styles['login-input']} name='id' value={ id } onChange={ e => setId(e.target.value) } />
<div id='idDiv' className={styles['div']}>{ idDiv }</div>
</div>
<div>
<label className={styles['login-title']}>비밀번호</label>
<input type="password" className={styles['login-input']} name="pwd" value={ pwd } onChange={ e => setPwd(e.target.value) }/>
<div id='pwdDiv' className={styles['div']}>{ pwdDiv }</div>
</div>
<div id='loginDiv' className={styles['div']} style={{textAlign: 'center'}}>{ loginDiv }</div>
<button className={styles['login-button']} onClick={ onLoginSubmit }>로그인</button>
</form>
</div>
);
};
export default LoginForm;
MemberController.java
package member.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import member.service.MemberService;
@CrossOrigin
@Controller
@RequestMapping(value="member")
public class MemberController {
@Autowired
private MemberService memberService;
//@RequestMapping(value="login", method = RequestMethod.GET)
@GetMapping(value = "login")
@ResponseBody
public String login(@RequestParam("id") String id, @RequestParam("pwd") String pwd) {
int result = memberService.login(id, pwd);
System.out.println("result = " + result);
if(result == 1) {
return "success";
}
else
return "fail";
}
}
MemberService.java
package member.service;
public interface MemberService {
public int login(String id, String pwd);
}
MemberServiceImpl.java
package member.service.impl;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import member.bean.MemberDTO;
import member.dao.MemberDAO;
import member.service.MemberService;
@Service
@Transactional
public class MemberServiceImpl implements MemberService {
@Autowired
private MemberDAO memberDAO;
@Override
public String login(String id, String pwd) {
Map<String, String> map = new HashMap<>();
map.put("id", id);
map.put("pwd", pwd);
MemberDTO memberDTO = memberDAO.login(map);
System.out.println("memberDTO = " + memberDTO);
if(memberDTO != null) {
return "success";
}
else
return "fail";
}
}
MemberDAO.java
package member.dao;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface MemberDAO {
int login(Map<String, String> map);
}
memberMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member.dao.MemberDAO">
<select id="login" parameterType="map" resultType="int">
select count(*) from member where id=#{id} and pwd=#{pwd}
</select>
</mapper>
MemberController.java
@CrossOrigin
@Controller
@RequestMapping(value="member")
public class MemberController {
@Autowired
private MemberService memberService;
//@RequestMapping(value="login", method = RequestMethod.GET)
@GetMapping(value = "login")
@ResponseBody
public String login(@RequestParam("id") String id, @RequestParam("pwd") String pwd) {
return memberService.login(id, pwd);
}
}
'REACT' 카테고리의 다른 글
DAY 80 - Spring + React +MyBatis(MySQL) - 글 상세보기 (2024.10.30) (0) | 2024.10.30 |
---|---|
DAY 79, 80 - Spring + React +MyBatis(MySQL) HOMEWORK - 글쓰기 / 글목록 (2024.10.29) (2024.10.30) (0) | 2024.10.30 |
DAY 78 - React HOMEWORK (2024.10.28) (0) | 2024.10.28 |
DAY 78 - React - useReducer / React-Router/ JSX (2024.10.28) (0) | 2024.10.28 |
DAY 77 - React - 데이터 읽기, 쓰기 / 비동기통신(aixos) / useMemo (2024.10.25) (0) | 2024.10.27 |