Tool-설정및사용2012. 4. 23. 18:38

KLDPWiki 에서 제공되는 기초적인 Shell Programming 입니다.

필요하실때 한번씩 보면 좋을 것 같습니다.

http://wiki.kldp.org/wiki.php/ShellProgrammingTutorial

감사합니다.

Posted by lahuman
Spring2012. 4. 23. 13:10

[1장] Spring 기본 환경 셋팅 및 Spring-MVC 설정
[2장] Spring 을 이용한 Database 및 Transaction Setting



  1. 목표
    Spring 에 myBatis 와 Sitemesh 연동

  2. 학습
  • Default Setting Information
    • Add Library


myBatis 관련 셋팅

mybatis-context.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">



<!-- http://groups.google.com/group/ksug/browse_thread/thread/766cd1fd8ba39c96 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<!-- mapper file location -->

<property name="mapperLocations" value="classpath:sql/mybatis/mapper/**/*.xml" />

</bean>


<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

<constructor-arg index="0" ref="sqlSessionFactory" />

</bean>



</beans>


tesSqlmap.xml - 위치 : sql.mybatis.mapper.test

<?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.pe.lahuman.service.TestDao">


  <insert id="insert" parameterType="string" >

    INSERT INTO TEST_STR (STR) VALUES( #{str})

  </insert>


<delete id="deleteAllData">

DELETE FROM TEST_STR

</delete>

<select id="getList" resultType="java.util.Map">

SELECT str FROM TEST_STR

</select>

</mapper>


TestDao

package kr.pe.lahuman.service;


import java.util.Collection;

import java.util.Map;


import org.apache.ibatis.annotations.Param;


public interface TestDao {

int insert(@Param("str") String str) throws Exception;

void deleteAllData() throws Exception;

Collection<Map<String, String>> getList()throws Exception;

}


* Dao의 실제 구현체는 없다, 


사용 법 : 


TestServiceImpl

package kr.pe.lahuman.service.impl;


import java.util.Collection;

import java.util.Map;


import kr.pe.lahuman.service.TestService;


import org.apache.ibatis.session.SqlSession;

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

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;


@Service

public class TestServiceImpl implements TestService {


private kr.pe.lahuman.service.TestDao testDao;


@Autowired

public void setSqlSession(SqlSession sqlSession){

   // 이부분에서 이렇게 사용 하였으나, 다른 방법도 있음

this.testDao = sqlSession.getMapper(kr.pe.lahuman.service.TestDao.class);

}

@Override

public String getData(String str) throws Exception{

return str;

}



@Override

@Transactional(rollbackFor=Exception.class) //anotation을 사용할 경우

public String insert2Error(String str) throws Exception {

testDao.insert(str);

testDao.insert(str);

testDao.insert(str);

testDao.insert(str);

testDao.insert(str);

testDao.insert(str);

throw new Exception("Insert Error");

}



@Override

public String insert(String str) throws Exception {

return testDao.insert(str)+"";

}



@Override

public Collection<Map<String, String>> getList() throws Exception {

return testDao.getList();

}



@Override

public void deleteAllData() throws Exception {

testDao.deleteAllData();

}


}




sitemesh 관련 셋팅

web.xml

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

<display-name>DefaultWeb</display-name>


<!-- sitemesh 관련 추가 사항 -->

<filter>

<filter-name>sitemesh</filter-name>

<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>

</filter>


<filter-mapping>

<filter-name>sitemesh</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- sitemesh 관련 추가 사항 -->

<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>utf-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>*.do</url-pattern>

</filter-mapping>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/config/*-context.xml

</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/config/spring-servlet.xml

</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

</web-app>


appengine-web.xml

<!-- THIS FILE IS ONLY REQUIRED IF YOU WANT TO DEPLOY TO GOOGLE APP ENGINE. THIS IS NOT NEEDED FOR SITEMESH -->

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

    <application>sitemesh-examples-hellowebapp</application>

    <version>1</version>

    <system-properties>

        <property name="file.encoding" value="UTF-8"/>

    </system-properties>

    <precompilation-enabled>true</precompilation-enabled>

    <static-files>

        <exclude path="**.html" />

    </static-files>

</appengine-web-app>



sitemesh3.xml

<sitemesh>

  <mapping path="/*" decorator="/WEB-INF/decorators/master.jsp"/>

</sitemesh>


master.jsp

<html>

  <head>

    <title>SiteMesh example: <sitemesh:write property='title'>Title goes here</sitemesh:write></title>

    <style type='text/css'>

      body { font-family: arial, sans-serif; background-color: #ffffcc; }

      h1, h2, h3, h4 { text-align: center; background-color: #ccffcc; border-top: 1px solid #66ff66; }

      .disclaimer { text-align: center; border-top: 1px solid #cccccc; margin-top: 40px; color: #666666; font-size:10pter; }

    </style>

    <sitemesh:write property='head'/>

  </head>

  <body>


    <h1 class='title'>SiteMesh example site: <sitemesh:write property='title'>Title goes here</sitemesh:write></h1>


    <sitemesh:write property='body'>Body goes here. Blah blah blah.</sitemesh:write>


    <div class='disclaimer'>Site disclaimer. This is an example.</div>

    <div class='navigation'>

      <b>Examples:</b>

      [<a href="./">Static example</a>]

      [<a href="demo.jsp">Dynamic example</a>]

    </div>


  </body>

</html>



* 기존에 사용되던 2.x 버전과 달라진 점이 있다 가장 큰것은 파일 명들이 바뀌었다.

기존 decorator.xml => sitemesh3.xml   /  sitemesh.xml => appengine-web.xml


첨부 파일 : 


DefaultWeb.zip.001


DefaultWeb.zip.002



Posted by lahuman
Spring2012. 4. 22. 00:03
  1. 목표
    Database 연결과, Transction 을 Setting 과 Test를 한다.

  2. 학습
  • Default Setting Information
    • Add Library
  • commons-dbcp - Database Connection Pool은 DBCP 를 이용한다.
  • commons-pool - DBCP를 사용하기 위한 필수 Library
  • 접속을 원하는 Database 의 Driver
  • aopalliance - Spring AOP를 사용하기 위한 Library
  • jakarta-oro - Spring AOP를 사용하기 위한 Library
  • com.springsource.org.aspectj.tools -  Spring AOP를 사용하기 위한 Library


  • Add context
    • datasource-context.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<!-- hsql  -->

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="net.sf.log4jdbc.DriverSpy"/>

<property name="url" value="jdbc:log4jdbc:hsqldb:hsql://localhost/javaworld"/>

<property name="username" value="sa"/>

</bean>

<!-- hsql  -->

</beans>


    • transaction-context.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">


<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

 <property name="dataSource" ref="dataSource"/>

</bean>


<!-- if do you want Annotaion 

참조 : http://www.egovframe.org/wiki/doku.php?id=egovframework:rte:psl:transaction:declarative_transaction_management

-->

<tx:annotation-driven transaction-manager="txManager"/>

<!-- if do you want Annotaion  -->


<!-- if Method throw Exception, rollback! -->

<tx:advice id="txAdvice" transaction-manager="txManager">

<tx:attributes>

<tx:method name="*" rollback-for="Exception"/>

</tx:attributes>

</tx:advice>

<!-- if Method throw Exception, rollback! -->


<!-- Aop setting -->

<aop:config>

<aop:pointcut id="requiredTx"

expression="execution(* kr.pe.lahuman.service.impl.*Impl.*(..))"/>

<aop:advisor advice-ref="txAdvice"

pointcut-ref="requiredTx" />

</aop:config>

<!-- Aop setting -->

</beans>


  • 주요 Class
    • TestDao

package kr.pe.lahuman.service.impl;


import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Collection;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import javax.sql.DataSource;


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

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;


@Repository

public class TestDao {


@Autowired

public void setDataSource(DataSource dataSource) {

this.jdbcTemplate = new JdbcTemplate(dataSource);

}

private JdbcTemplate jdbcTemplate;

public String getData(String str) {

return str;

}

public void deleteAllData(){ 

this.jdbcTemplate.update(

"delete from test_str");

}

private void insertMethod(String str) {

this.jdbcTemplate.update(

"insert into test_str(str) " +

"values(?)", 

str);

}

public String insert(String str) throws Exception{

//insert

insertMethod(str);

return str;

}

public Collection<Map<String, String>> getList()throws Exception{

Collection<Map<String, String>> results = this.jdbcTemplate.query(

   "select str from test_str",

   new RowMapper() {


       public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

           Map<String, String> map = new HashMap<String, String>();

           map.put("str", rs.getString("str"));

           return map;

       }

   });

return results;

}

}




    • TestServiceImpl

package kr.pe.lahuman.service.impl;


import java.util.Collection;

import java.util.Map;


import kr.pe.lahuman.service.TestService;


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

import org.springframework.stereotype.Service;

import org.springframework.transaction.PlatformTransactionManager;

import org.springframework.transaction.TransactionStatus;

import org.springframework.transaction.annotation.Transactional;

import org.springframework.transaction.support.DefaultTransactionDefinition;


@Service

public class TestServiceImpl implements TestService {


@Autowired

private TestDao testDao;


@Override

public String getData(String str) throws Exception{

return testDao.getData(str);

}



@Override

@Transactional(rollbackFor=Exception.class) //anotation을 사용할 경우

public String insert2Error(String str) throws Exception {

testDao.insert(str);

testDao.insert(str);

testDao.insert(str);

testDao.insert(str);

testDao.insert(str);

testDao.insert(str);

//강제로 Exception 발생

throw new Exception("Insert Error");

}



@Override

public String insert(String str) throws Exception {

return testDao.insert(str);

}



@Override

public Collection<Map<String, String>> getList() throws Exception {

return testDao.getList();

}



@Override

public void deleteAllData() throws Exception {

testDao.deleteAllData();

}


}



첨부 파일 : 


RestWeb.zip.001


RestWeb.zip.002


Posted by lahuman
Spring2012. 4. 20. 13:48
  1. 목표
    Spring Framework 에서 꼭 필요한 Library와 설정 을 알아본다.

  2. 학습
    기본적으로 Spring framework를 사용하기 위해서 필요한, 기본 셋팅 정보
  • JDK 1.6
  • eclipse
  • spring framwork
  • commons-logging - Spring framework 에서 필수로 필요함
  • jakarta-taglibs-standard Spring-MVC 에서 필수로 필요함


web.xml 설정

<!-- Filter Setting -->

 <filter>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>utf-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>*.do</url-pattern>

</filter-mapping>

<!-- Filter Setting -->

<!-- Spring Config Setting -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/config/*-context.xml

</param-value>

</context-param>

     <!-- Spring Config Setting -->

<!-- listener Register -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- listener Register -->

<!-- Spring-MVC Config Setting -->

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/config/spring-servlet.xml

</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<!-- Spring-MVC Config Setting -->


application-context.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">


<!-- component scan -->

<context:component-scan base-package="kr.pe.lahuman" >

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

<!-- component scan -->


</beans>


spring-servlet.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">


<!-- component scan -->

<context:component-scan base-package="kr.pe.lahuman"

use-default-filters="false">

<context:include-filter type="annotation"

expression="org.springframework.stereotype.Controller" />

</context:component-scan>

<!-- component scan -->


   <!--

        - This bean configures the 'prefix' and 'suffix' properties of 

        - InternalResourceViewResolver, which resolves logical view names 

        - returned by Controllers. For example, a logical view name of "vets" 

        - will be mapped to "/jsp/vets.jsp".

    -->

<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"

p:order="1" p:viewClass="org.springframework.web.servlet.view.JstlView"

p:prefix="/jsp/" p:suffix=".jsp" />

</beans>


TestDao.java

@Repository

public class TestDao {


public String getData(String str){

return str;

}

}


TestServiceImpl.java

@Service

public class TestServiceImpl implements TestService {


@Autowired

private TestDao testDao;

@Override

public String getData(String str) {

return testDao.getData(str);

}

}


TestController.java

@Controller

public class TestController {


@Autowired

private TestService testService; 

@RequestMapping(value="/test.do")

public String testView(){

return testService.getData("test");

}

}



첨부파일 참조


SpringWeb.zip


Posted by lahuman