'전체'에 해당되는 글 82건
- 2016.12.13 첫 출근.... 그리고 두번째...
- 2016.10.31 Simple Modal Popup
- 2015.08.11 java + mybatis communication
- 2015.08.07 [linux] rsync Setting
- 2015.07.22 [Spring] Listener 등록
- 2015.07.22 [RabbitMQ] Topic Example
- 2015.07.21 Apache + Tomcat 연동 설치
- 2015.07.03 Data Bind
- 2015.06.19 Map key 추출 (Iterator)
- 2015.04.30 [Spring] Listener
지난 시간에 JAVA + SPRING + MYBATIS를 연동 하는 것을 배워 보았다. 이번에는 JAVA에서 MYBATIS를 연동 하는 법을 배워보자
우선 MAVEN을 통해서 라이브러리를 받아 보도록 하자. 본인은 mybatis 3.3.0을 활용 하였다.
======================================================================
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
<scope>compile</scope>
</dependency>
======================================================================
Mybatis는 DB 접속 정보를 XML로 따로 빼서 관리 할 수도 있고, 그렇지 않으면 코드 상으로 입력을 할 수도 있다. 근데 서버 정보를 소스에 넣으면 서버 정보가 변경 되게 되면 새로 컴파일을 해야 하는데 관리자가 개발 하고 프로젝트를 빠져 버리게 되면 누가 관리 하겠는가? 그래서 XML로 별도 관리 하는 것을 추천 한다.
파일명을 mybatis-config.xml이라는 빈 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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.mariadb.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://www.cdesystem.com/test"/>
<property name="username" value="wcs"/>
<property name="password" value="wcs"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="BMRBTMapper.xml"/>
</mappers>
</configuration>
======================================================================
여기서 보면 mapper resource라고 해서 다른 XML을 참조 하게 된다. 이 부분은 쉽게 이야기 하자면 쿼리를 모아 놓은 XML이라고 생각하자!! 단순히 쿼리만 모아 놓은것이 아니라 VO와 맵핑을 시켜 준다.
그렇다면 이제 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="kr.laby">
<select id="selectSystemLogs" parameterType="String" resultType="kr.laby.VO.SystemLogs">
SELECT
TYPE,
SYSTEM,
INTERFACE AS IFNAME,
IP,
SERVERDESC,
STATUS,
MESSAGE,
SERVICE_ID,
DEVICE,
REQTIME,
DATASIZE,
FILE1,
FILE2,
CRUSER,
CRTYPE,
CRTIME,
FROMDEVICE,
TIME
FROM SYSTEM_LOGS
</select>
<select id="hlsServerList" parameterType="String" resultType="kr.laby.VO.HLSServerList">
SELECT IP FROM HLS_SERVER_LIST WHERE PURPOSE='HLS_실시간'
</select>
</mapper>
=======================================================================
이렇게 쿼리를 넣어 주시면 되겠다.그러고 나서 그 쿼리에 해당 하는 ResultType을 VO의 패키지명으로 선언을 하게 되면 바로 맵핑이 되어서 자바에서 쓸 수 있다는 사실!!
이제 자바 소스 코드를 확인해 보자
환경 설정 파일을 만들었으니 환경 설정을 불러 봐야 겠죠? InputStream을 활용하여 파일을 읽어 들입니다.
String configFile = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(configFile);
이렇게 하면 DB와 연결할 정보를 주입 하는데 까지는 성공 하신거죠!!
Mybatis의 경우 모든 MyBatis 애플리케이션은 SqlSessionFactory 인스턴스를 사용합니다. SqlSessionFactory 인스턴스는 SqlSessionFactoryBuilder 를 사용하여 만들수 있습니다. 어떻게요? 요로케요
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
이렇게 하면 디비와 세션 까지 맺어지게 됩니다.
그럼 이제 쿼리 문을 날려 봐야 겠죠?
List<?>result = null;
result = sqlSession.selectList(mapper);
이렇게 하면 디비에 연결 해서 값까지 가져 오는데 까지 성공!! 이젠 빼서 쓰시기만 하시면 되요~
소스 정리
String configFile = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(configFile);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
result = sqlSession.selectList(mapper);
inputStream.close();
sqlSession.close();
for (int i=0 ; i<result.size() ; i++)
{
System.out.println(result.get(i).getIP());
}
참~~ 쉽죠? 근데 모르면 어렵더라구요 ㅠ.ㅠ
서버간의 파일을 공유 하고 싶을때 rsync나 scp를 활용 할 수 있다.
이번에는 rsync에 대해서 알아 보도록 하자.
우선 rsync를 사용하기 위해서는 rsync가 필요 하겠지? 패키지가 설치 되어 있는지 확인해 볼 필요가 있겠다.
Server program install
# rpm -qa | grep rsync
없다면
# yum install rsync
xinetd 패키지도 필요 하다.
# rpm -qa | grep xinetd
마찬가지로 없다면
# yum install xinetd
사용할 준비는 되었다.
Server Setting
# vi /etc/xinetd.d/rsync 를 열어서
disable = yes라고 되어 있는 부분을 no로 변경 하자.
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
Client Setting
이제는 환경 설정 파일을 만들어 줄 차례이다
# vi /etc/rsyncd.conf
[bmrbt] #rsync 서비스명
path=/svc/ #경로
comment = rsync_channel_image
uid = svc
gid = svc
use chroot=yes
read only = no
host allow=114.202.131.248
max connections = 5
timeout=300
rsync: mkstemp "/.test.PZQvTe" (in BACKUP) failed: Permission denied (13)
/etc/selinux/conf 파일에서
SELINUX=enforcing으로 되어 있는데
SELINUX=disabled 로 변경 한다.
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
요로코롬... 변경하고 서버를 재부팅 한다.
#reboot 또는 shutdown -r now
그런담에 다시 해 보시면.. 우왕... 신세계!!
web.xml을 열어서 아래 내용을 추가해 주세요.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.laby.listener.InitListener</listener-class>
</listener>
그렇다면 initListener는?
tomcat-connectors-1.2.40-src.tar.gz
Apache install
cat > /svc/was/apache/conf/mod_jk.conf
cat > workers.properties
httpd.conf 에 내용 추가
Tomcat Start!
./startup.sh
Server Start!
# ./httpd -k start
<html ng-app="parking">
<title>
Angular JS Sample
</title>
<script src= "angular.min.js"></script>
<script>
var parking = angular.module("parking", []);
parking.controller("parkingCtrl", function($scope){
$scope.appTitle = "[Packt] Parking2";
$scope.cars=[];
$scope.park = function(car){
$scope.appTitle = "changed";
alert("changed");
}
});
</script>
<body ng-controller="parkingCtrl">
{{appTitle}}<br>
<input type="button" ng-click="park(car)" value="DataChange">
</body>
</html>
// 방법1
Iterator<String> keys = map.keySet().iterator();
while( keys.hasNext() ){
String key = keys.next();
System.out.println( String.format("키 : %s, 값 : %s", key, map.get(key)) );
}
// 방법2
for( Map.Entry<String, String> elem : map.entrySet() ){
System.out.println( String.format("키 : %s, 값 : %s", elem.getKey(), elem.getValue()) );
}
// 방법3
for( String key : map.keySet() ){
System.out.println( String.format("키 : %s, 값 : %s", key, map.get(key)) );
}
1. WEB.XML
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.laby.listener.InitListener</listener-class>
</listener>
2. Add Class file
package com.laby.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class InitListener implements ServletContextListener{
@Override
public void contextInitialized (ServletContextEvent sce)
{
System.out.println("LISTENER");
}
@Override
public void contextDestroyed (ServletContextEvent sce)
{
}
}