메뉴 건너뛰기

Bigdata, Semantic IoT, Hadoop, NoSQL

Bigdata, Hadoop ecosystem, Semantic IoT등의 프로젝트를 진행중에 습득한 내용을 정리하는 곳입니다.
필요한 분을 위해서 공개하고 있습니다. 문의사항은 gooper@gooper.com로 메일을 보내주세요.


1. 아래 소스를 참고하여 아래와 같은 폴더에 파일을 위치시킨다.

(maven으로 빌드하는 경우의 source파일및 resource파일 구조)

가. TestMain.java에서 sch.hist.insert를 호출하는 형태를 갖는다.

나. Mybatis의 트랜잭션은 Sch2Service.java의 SqlSession sqlSession2 = factory2.openSession(); 부터 시작되며

SqlSession은 반드시 close해줘야한다.

다. rollback()은 따로 해주지 않아도 되는가 보다(?)



-----소스폴더 구조----

src/main/java/TestMain.java


src/main/resource/Configuration.xml

src/main/resource/mariadb/db.properties

src/main/resource/mariadb/mapper/sch/SchHist_SQL.xml



-------Sch_SQL.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="sch.hist">

<insert id="insert" parameterType="com.gooper.icbms.sda.comm.sch.dto.SchHistDTO">

<![CDATA[

insert into thsda_sch_hist(task_group_id, task_id, start_time, task_class, task_expression, work_cnt, work_time, cuser)

values (#{task_group_id}, #{task_id}, #{start_time}, #{task_class}, #{task_expression}, #{work_cnt}, #{work_time}, #{cuser})

]]>

</insert>



<select id="updateFinishTime" parameterType="com.gooper.icbms.sda.comm.sch.dto.SchHistDTO">

<![CDATA[

update thsda_sch_hist set finish_time=#{finish_time}, work_result=#{work_result}, triple_file_name=#{triple_file_name}, triple_check_result=#{triple_check_result},

uuser  = #{uuser}, udate = now()

where task_group_id = #{task_group_id} and task_id = #{task_id} and start_time = #{start_time}

]]>

</select>

</mapper>



------------TestMain.java(일부분임)

//test start
int id = idx.getAndIncrement();
log.info("direct calling from mybatis without spring test start(id : "+id+")");
String task_group_id = "PARKKKKKK_task_group_id_+"+id;
String task_id = "PARKKKKK_task_id_"+id;
int work_cnt = 10;
String start_time = Utils.dateFormat.format(new Date());
String end_time = Utils.dateFormat.format(new Date());
schComm.insertSchHist(task_group_id, task_id, work_cnt, start_time, end_time);
log.info("direct calling from mybatis without spring test end(id: "+id+")");
//test end


-------SchComm.java의 insertSchHist()부분 

// schHist테이블에 데이타 insert

public int insertSchHist(String task_group_id, String task_id, int work_cnt, String start_time, String end_time)

throws Exception {

int updateCnt = 0;

SchHistDTO schHistDTO = new SchHistDTO();

schHistDTO.setTask_group_id(task_group_id);

schHistDTO.setTask_id(task_id);

schHistDTO.setStart_time(start_time);


schHistDTO.setTask_class("NONE");

schHistDTO.setTask_expression("NONE");


schHistDTO.setWork_cnt(work_cnt);

schHistDTO.setWork_time(end_time);

schHistDTO.setCuser(user_id);

schHistDTO.setUuser(user_id);


List<SchHistDTO> list = new ArrayList<SchHistDTO>(); 

Map<String, List<SchHistDTO>> updateSchHistMap = new HashMap<String, List<SchHistDTO>>();

list.add(schHistDTO);

updateSchHistMap.put("list", list);

try {

updateCnt = sch2Service.insertSchHist(updateSchHistMap);

// pass

} catch (Exception e) {

e.printStackTrace();

throw e;

}

return updateCnt;

}



------------Sch2Service.java의 insertSchHist() 부분

         // 아래문장은 클래스 변수에 지정해주고 반복 사용되도록 해도된다.

SqlSessionFactory factory = SqlMapConfig.getSqlSession();



// sch_hist메서드

public int insertSchHist(Map<String, List<SchHistDTO>> map) throws Exception {

int cnt = 0;


// mapper에 접근하기 위한 SqlSession

SqlSession sqlSession = factory.openSession(); 


SchHist2DAO schHist2DAO = new SchHist2DAO(sqlSession);

List<SchHistDTO> list = map.get("list");

for(int i = 0; i < list.size(); i++) {

SchHistDTO schHistDTO = (SchHistDTO)list.get(i);

try {

log.debug("insertSchHist() ......................... start ");

cnt = schHist2DAO.insert(schHistDTO);


sqlSession.commit();

log.debug("insertSchHist()......................... commit() ");

} catch (Exception e) {

e.printStackTrace();

throw e;

} finally {

sqlSession.close();

log.debug("insertSchHist() ......................... close() ");

}

}

log.debug("insertSchHist() ......................... end ");

return cnt;

}


------Configuration.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>


<!-- DB접속정보 값을 가지고 있는 파일 설정 -->

<properties resource="mariadb/db.properties" />

<!-- 별명 부여 -->

<!-- 

<typeAliases> 

<typeAlias type="pack.business.DataDto" alias="dto"/>

</typeAliases>

-->

<!-- DB접속 정보 셋팅 -->

<environments default="development">

<environment id="development">

<transactionManager type="JDBC" />

<dataSource type="POOLED">

<property name="driver" value="${driver}" />

<property name="url" value="${url}" />

<property name="username" value="${username}" />

<property name="password" value="${password}" />

<property name="poolMaximumActiveConnections" value="20"/>

                <property name="poolMaximumIdleConnections" value="20"/>

                                <property name="poolMaximumCheckoutTime" value="20000"/>

                        <property name="poolPingEnabled" value="true"/>

                        <property name="poolPingQuery" value="select 1"/>

                        <property name="poolPingConnectionsNotUsedFor" value="10000"/>

                        <property name="poolTimeToWait" value="15000"/>

</dataSource>

</environment>

</environments>

<!-- mapper.xml 파일 설정 -->

<mappers>

<mapper resource="mariadb/mapper/DataMapper.xml" />

<mapper resource="mariadb/mapper/sch/Sch_SQL.xml" />

<mapper resource="mariadb/mapper/sch/Aggr_SQL.xml" />

<mapper resource="mariadb/mapper/sch/SchHist_SQL.xml" />

</mappers>

</configuration>




------DB정보를 가지고 있는 파일(설정파일)(db.properties)

driver=org.mariadb.jdbc.Driver

url=jdbc:mariadb://XXX.XXX.XXX.XXX:3306/sda

username=db명

password=db패스워드



------DB정보를 가지고 있는 클래스 파일(SqlMapConfig.java)

package com.gooper.icbms.sda.comm;


import java.io.Reader;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;


public class SqlMapConfig {

private static SqlSessionFactory sqlSession;


static {

String resource = "Configuration.xml";


try {

Reader reader = Resources.getResourceAsReader(resource);

sqlSession = new SqlSessionFactoryBuilder().build(reader);

reader.close();

} catch (Exception e) {

System.out.println("SqlMapConfig 오류 : " + e);

}

}


public static SqlSessionFactory getSqlSession() {

return sqlSession;

}

}

번호 제목 글쓴이 날짜 조회 수
53 ./gradlew :composeDown 및 ./gradlew :composeUp 를 성공했을때의 메세지 gooper 2023.02.20 6
52 호출 url현황 gooper 2023.02.21 6
51 [vue storefrontui]외부 API통합하기 참고 문서 총관리자 2022.02.09 7
50 eclipse editor 설정방법 총관리자 2022.02.01 9
49 주문히스토리 조회 총관리자 2022.04.30 10
48 주문 생성 데이터 예시 총관리자 2022.04.30 11
47 windows10 pro에서 microservice pattern책의 예제를 kubernetes에서 기동하는 방법 총관리자 2022.01.30 17
46 vuestorefrontui.io를 이용한 front end project 생성하기 총관리자 2022.02.06 23
45 restaurant-controller,에서 등록 예시 총관리자 2022.04.30 33
44 Collections.sort를 이용한 List<Map<String, String>>형태의 데이타 정렬 소스 총관리자 2016.12.15 45
43 lombok설치방법 총관리자 2020.06.20 49
42 Windows에서 sbt개발환경 구축 방법(링크) 총관리자 2016.06.02 54
41 Lagom프레임웍에서 제공하는 HelloWorld 테스트를 수행시 [unknown-version]오류가 발생하면서 빌드가 되지 않는 경우 조치사항 총관리자 2017.12.22 56
40 lagom-windows용 build.sbt파일 내용 총관리자 2017.10.12 60
39 python 2.6.6에서 print 'A=' 형태의 사용이 python 3.5.1에서 오류(SyntaxError: Missing parentheses in call to 'print') 발생함.. 총관리자 2016.05.27 62
38 CentOS6에 python3.5.1 소스코드로 빌드하여 설치하기 총관리자 2016.05.27 71
37 jdk 9이상 사용하려면 repository를 아래와 같이 지정해야한다. 총관리자 2019.06.02 71
36 DBCP Datasource(org.apache.commons.dbcp.BasicDataSource) 설정 및 속성 설명 총관리자 2016.09.26 74
35 python3.5에서 numpy버젼에 따른 문제점을 조치하는 방법및 pymysql import할때 오류 발생시 조치사항 총관리자 2017.09.28 80
34 Lagom에서 제공하는 Maven을 이용한 Hello프로젝트 자동생성 및 실행 총관리자 2018.01.19 81

A personal place to organize information learned during the development of such Hadoop, Hive, Hbase, Semantic IoT, etc.
We are open to the required minutes. Please send inquiries to gooper@gooper.com.

위로