1) node.js

https://nodejs.org/en/download/

 

OS와 상황에 맞게 다운로드. 일반적으로는 이미지에 선택된 것으로.

 

원하는 폴더에 압축해제 한다.

 

환경변수에 node 경로 추가(제어판 - 시스템 - 고급시스템 설정 - 고급 탭 - 환경변수 - PATH, Windows7 기준)

 

터미널에서 위와 같이 실행하여 정상 출력되면 설정 완료.

 

파일 - 작업영역에 폴더 추가 한 후

터미널에서 해당 작업폴더에 접근한다.

 

위와 같이 명령어를 작성하면 package.json 파일이 생성된다.

 

해당 폴더에서 app.js라는 이름의 새 파일을 작성하고

테스트 소스코드를 작성한다.

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello Second\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

 

디버그 - 디버깅 시작 하면

디버그 콘솔에 아래와 같이 console.log의 내용이 찍히고,

브라우저로 접근했을때 res.end 의 내용이 출력되면 정상.

vscode 화면
브라우저로 URL 접근 시 출력화면

 

 

2) mysql / sql mapper

먼저 mysql 을 설치.

 

mysql 설치 및 설치결과 화면

 

 

업무용으로 사용하는 것이 전제되었기 때문에 sql 관리는 필수이다.

다행히도 mybatis의 node.js 버전을 찾았다.

 

mybatis 설치 및 설치결과 화면

 

설치 후 mapper 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_emp">  
  <select id="SEL001">
    SELECT *
    FROM COM_EMP_INFO
    WHERE EMP_NO = #{EMP_NO}
  </select>
</mapper>

app.js로 콘솔에 json 데이터 출력

const mysql = require('mysql');  //My-sql
const mybatisMapper = require('mybatis-mapper');  //mybatis
 
const connection = mysql.createConnection({  //create connection
    host: '127.0.0.1',
    port: '3306',
    user: 'study',
    database: 'study',
    password : 'study1!'
});
 
//mapper load
mybatisMapper.createMapper([ './sql/com/com_emp.xml' ]);
 
//param
var param = {
    EMP_NO : '10000002'
}
 
//create query
var format = {language: 'sql', indent: '  '};
var query = mybatisMapper.getStatement('com_emp', 'SEL001', param, format);
//getStatement([map namespace], [sql id], [param], format);

console.log(query);  //qury print

connection.connect();
connection.query(query, function (error, results, fields) {  
    if (error) {
        console.log(error);
    }
    console.log(results); //result print
});
connection.end();

실행결과(맵핑된 쿼리 및 결과값)

 

+ Recent posts