메뉴 건너뛰기

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;

}

}

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.

위로