Re:실전! 웹스퀘어5(Websquare5)_Grid 구성 및 서버 통신모듈 만들기_최성찬

 

 

서버 프로젝트 구성

 

c903b4daf5908ef8a243a9aa218aaeaf_1525253
 

 

쿼리

use mydb;

 

create table employee(

empno int(255) primary key auto_increment,

ename varchar(20),

    gender varchar(2),

job varchar(20),

    position varchar(20),

    email varchar(30),

salary int(255),

hiredate date,

    createdDate datetime default localtimestamp,

    lastUpdateDate datetime default localtimestamp  

);

 

insert into employee values(default,'최성찬','남','개발자','사원','csc1234@gamil.com','3000','20170502',default,default);

insert into employee values(default,'홍길동','남','퍼블리셔','대리','HKD1313@gamil.com','4500','20160402',default,default);

insert into employee values(default,'임꺾정','남','영업사원','사원','LMK@naver.com','2500','20170202',default,default);

insert into employee values(default,'장길산','남','영업사원','사원','JKS@naver.com','2500','20170102',default,default);

insert into employee values(default,'장그래','남','영업사원','대리','misang@naver.com','3000','20160202',default,default);

insert into employee values(default,'한석율','남','영업사원','과장','hsus@naver.com','3200','20160202',default,default);

insert into employee values(default,'손민정','여','개발자','사원','SMJ@naver.com','3000','20170502',default,default);

insert into employee values(default,'송윤선','여','PL','과장','SUS@gamil.com','3500','20120502',default,default);

insert into employee values(default,'전희영','여','개발자','대리','JHY1314@gamil.com','3300','20140502',default,default);

insert into employee values(default,'이민교','남','개발자','대리','LMK1313@gamil.com','3100','20170502',default,default);

insert into employee values(default,'송석원','남','PM','차장','SSW414@gamil.com','4000','20070502',default,default);

insert into employee values(default,'김동현','남','개발자','사원','KDH@gamil.com','3000','20170502',default,default);

insert into employee values(default,'이경민','남','개발자','사원','LGM@gamil.com','3000','20170502',default,default);

insert into employee values(default,'박수연','여','개발자','사원','PSW@gamil.com','3000','20170502',default,default);

insert into employee values(default,'이종철','남','아키텍트','부장','topcre@topcre.com','5000','19970502',default,default);

insert into employee values(default,'선지영','여','영업사원','차장','SJoo@naver.com','4000','20000502',default,default);

 

commit; 

 

select * from employee;

 

use mydb;

 

 

application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3307/mydb?useSSL=false

spring.datasource.username=root

 

spring.datasource.password=1234

 

EmpRestController

package com.emp.server.controller;

 

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.web.bind.annotation.DeleteMapping;

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

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.util.UriComponentsBuilder;

 

import com.emp.server.error.CustomErrorType;

import com.emp.server.mapper.EmpMapper;

import com.emp.server.model.Emp;

import com.emp.server.service.EmpService;

 

@CrossOrigin(origins = "*", maxAge= 3600)

@RestController

@RequestMapping("/api")

public class EmpRestController {

public static final Logger logger = LoggerFactory.getLogger(EmpRestController.class);

@Autowired

EmpService empService;

//모든 사원

//Get 매핑으로 할경우 웹스퀘어 에서 못받음

@PostMapping("/emp.post")

public Map getAllEmp() {

Map resObj = new HashMap<>();

resObj.put("emp_userInfo", empService.getAllEmps());

Map resData1 = new HashMap();

resObj.put("msg", "조회가 완료되었습니다");

resObj.put("msgCode", "S");

return resObj;

}

@PostMapping("/searchMember.do")

public Map searchMember(@RequestBody(required = true) Map param) throws Exception {

Map resObj = new HashMap();

Map sParam = null;

Emp sMember = null;

try {

sParam = (Map)param.get("emp_searchParam");

if(sParam == null){

throw new RuntimeException("필수 파라메터가 누락되었습니다."); 

}

sMember = new Emp();

sMember.setGender((String)sParam.get("gender"));

sMember.setPosition((String)sParam.get("position"));

resObj.put("emp_userInfo", empService.searchMember(sMember));

 

resObj.put("msg", "조회가 완료되었습니다.");

resObj.put("msgCode", "S");

} catch (Exception e) {

e.printStackTrace();

}

return resObj;

}

//사원 생성

@PostMapping("/createEmp.do")

public Map saveMember(@RequestBody(required = true) Map param ) throws Exception {

Map resObj = new HashMap();

return resObj;

}

 

}



 

 

 

CustomerErrorType

package com.emp.server.error;

 

public class CustomErrorType {

private String errorMessage;

public CustomErrorType(String errorMessage) {

this.errorMessage = errorMessage;

}

public String getErrorMessage() {

return errorMessage;

}

 

}


 

 

 

EmpMapper

package com.emp.server.mapper;

 

import java.util.List;

 

import org.apache.ibatis.annotations.Mapper;

import org.apache.ibatis.annotations.Select;

 

import com.emp.server.model.Emp;

 

 

package com.emp.server.mapper;

 

import java.util.List;

import java.util.Map;

 

import org.apache.ibatis.annotations.Mapper;

import org.apache.ibatis.annotations.Select;

 

import com.emp.server.model.Emp;

 

 

@Mapper

public interface EmpMapper{

public int insert(Emp emp);

public int update(Emp emp);

public int deleteByEmpNo(int empno);

@Select("SELECT * FROM employee")

public List<Emp> findAllEmps();

public Map selectByEmp(Emp emp);

 

public int increment(int empno);

}


 

 

 

Emp

package com.emp.server.model;

 

import java.sql.Date;

import java.time.LocalDateTime;

 

import org.springframework.format.annotation.DateTimeFormat;

 

import lombok.Data;

 

@Data

public class Emp {

 

private int empno;  //사번

 

private String ename; //이름

 

private String gender; //성별

private String job; //직무

private String position; //직급

private String email; //이메일

 

private int salary; //급여

 

private Date hiredate; //입사일

@DateTimeFormat(pattern="yyyy-MM-dd kk:mm:ss")

private LocalDateTime createdDate; //생성날짜

@DateTimeFormat(pattern="yyyy-MM-dd kk:mm:ss")

private LocalDateTime lastUpdateDate; //해당 row 수정일

 

}



 

 

EmpService

package com.emp.server.service;

 

import java.util.List;

import java.util.Map;

 

import com.emp.server.model.Emp;

 

public interface EmpService {

int createEmp(Emp emp);

int updateEmp(Emp emp);

int deleteEmpByEmpno(int empno);

List<Emp> getAllEmps();

Map searchMember(Emp emp);

}



 

 

EmpServiceImpl

package com.emp.server.service; 


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emp.server.mapper.EmpMapper;
import com.emp.server.model.Emp;

@Service
public class EmpServiceImpl implements EmpService{

@Autowired
EmpMapper empMapper;


@Override
public Map searchMember(Emp emp) {
return empMapper.selectByEmp(emp);
}

@Override
public int createEmp(Emp emp) {
return empMapper.insert(emp);
}

@Override
public int updateEmp(Emp emp) {
return empMapper.update(emp);
}

@Override
public int deleteEmpByEmpno(int empno) {
return empMapper.deleteByEmpNo(empno);
}

@Override
public List<Emp> getAllEmps() {
return empMapper.findAllEmps();
}


}

 

Mybatis-mapper/emp-mapper

<?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.emp.server.mapper.EmpMapper">

<resultMap type="Emp" id="EmpResultMap">

<result property="empno" column="EMPNO"/>

<result property="ename" column="ENAME"/>

<result property="job" column="JOB"/>

<result property="salary" column="SALARY"/>

<result property="hiredate" column="HIREDATE"/>

</resultMap>

<insert id="insert" parameterType="Emp">

INSERT INTO employee 

VALUES(default, #{ename}, #{job}, #{salary}, #{hiredate})

</insert>

 

<update id="update" parameterType="int">

UPDATE Emp 

SET name=#{ename}, age=#{age}, salary=#{salary}

WHERE empno=#{empno} 

</update>

<delete id="deleteByEmpNo" parameterType="int">

DELETE 

FROM employee

WHERE empno=#{empno}

</delete>

<select id="selectByEmp" resultType="Map" parameterType="com.emp.server.model.Emp">

SELECT empno, ename, gender, job, position, email, salary, hiredate, createdDate, lastUpdateDate

FROM employee

WHERE 1=1

<if test="GENDER != null and !GENDER.equals('all')">

AND GENDER = #{GENDER}

</if>

<if test="POSITION != null and !POSITION.equals('all')">

AND POSITION = #{POSITION}

</if>

</select>

</mapper> 

 

 

 웹스퀘어5 Grid구성및 통신모듈(submission) 만들기

 

 

 

51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 


 

51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

51d826b22182cd65ba2ebd80d8ce88a6_1525817

  

 

 

 

51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 

51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

 


51d826b22182cd65ba2ebd80d8ce88a6_1525817

 

 

 

 

 

 

 

 

 

 

 

아래와 같은 에러가 발생하면

a9408c87f909b9bbbb6ffe0e3ae078f4_1525980
스프링 컨트롤러 부분에

 

@CrossOrigin(origins = "*", maxAge= 3600)

위 어노테이션을 꼭 붙여주자



 

@CrossOrigin(origins = "*", maxAge= 3600)  //←이렇게 넣어줘야됨

@RestController

@RequestMapping("/server")

public class EmpController {

 

@Autowired

EmpService empService;

@PostMapping("/emp")

public Map getAllEmp() {

Map resObj = new HashMap<>();

resObj.put("empList", empService.getAllEmps());

Map resData = new HashMap();

resObj.put("msg", "조회가 완료되었습니다");

resObj.put("msgCode", "S");

return resObj;

}

}













 

Comments

FSP 05.24 18:06
감사합니다.
FSP 05.24 18:07
와우~ 한결 진보하셨네요~