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