'iBatis'에 해당되는 글 9

  1. 2009.12.31 다음에서도 iBatis를 쓰는구나.
  2. 2009.09.21 [iBatis] parameterClass에 List 전달하기
  3. 2009.09.19 iBatis 에러 메세지
  4. 2009.08.27 O 프로그램 구조 분석
  5. 2009.06.10 JPetStore 분석 2 - iBatis
  6. 2009.06.10 JPetStore 분석 1 - iBatis
  7. 2009.06.10 Open quote is expected for attribute "{1}" associated with an element type "parameterClass".
  8. 2009.06.10 JPetStore 설치 - iBatis
  9. 2009.06.09 iBATIS

다음에서도 iBatis를 쓰는구나.

다음에서 글을 보는데 에러가 발생했다.

SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; --- The error occurred in net/daum/gaia/core/domain/dao/sql/QnaInfo.xml. --- The error occurred while executing query. --- Check the SELECT bbsId, articleId, qId, viewUrl, itemJsonUrl, regDate FROM qnaInfo WHERE bbsId = ? and articleId = ? . --- Check the SQL Statement (preparation failed). --- Cause: org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException: --- The error occurred in net/daum/gaia/core/domain/dao/sql/QnaInfo.xml. --- The error occurred while executing query. --- Check the SELECT bbsId, articleId, qId, viewUrl, itemJsonUrl, regDate FROM qnaInfo WHERE bbsId = ? and articleId = ? . --- Check the SQL Statement (preparation failed). --- Cause: org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object

iBatis를 사용하고
Tomcat을 사용하나 보다.[각주:1] DBCP에 속한 예외 클래스가 보인다.
Spring을 쓰나?
  1. 2010-05-05 Tomcat에 포함된 DBCP 클래스가 아니잖아. [본문으로]

[iBatis] parameterClass에 List 전달하기

- parameterClass를 List로 전달
List<Xxx> params = new List<Xxx>();
params.add(new Xxx("a", "1"));
params.add(new Xxx("b", "2"));
params.add(new Xxx("c", "3"));

RESULT = getSqlMapClientTemplate().queryForList("xxx.getList", params);
<select id="xxx.getList" parameterClass="java.util.List" resultClass="Xxx">
    SELECT ...
    FROM TB_XXX
    WHERE 1 = 1
    <iterate prepend="AND" conjunction="OR" open="(" close=")">
        ( pk1 = #[].pk1# AND pk1 = #[].pk2# )
    </iterate>
</select>
- Map이나 다른 클래스에 포함된 경우
List<Xxx> params = new List<Xxx>();
params.add(new Xxx("a", "1"));
params.add(new Xxx("b", "2"));
params.add(new Xxx("c", "3"));

HashMap paramMap = new HashMap();
paramMap.put("pks", params);

RESULT = getSqlMapClientTemplate().queryForList("xxx.getList", paramMap);
<select id="xxx.getList" parameterClass="java.util.Map" resultClass="Xxx">
    SELECT ...
    FROM TB_XXX
    WHERE 1 = 1
    <iterate prepend="AND" property="pks" conjunction="OR" open="(" close=")">
        ( pk1 = #pks[].pk1# AND pk1 = #pks[].pk2# )
    </iterate>
</select>

- 배열을 전달하는 것은 안됨.

iBatis 에러 메세지

com.ibatis.common.beans.ProbeException: There is no READABLE property named 'isseuYear' in class 'project.domain.Close'
isseuYear 가 아니라 issueYear 임.

O 프로그램 구조 분석

- do라는 확장자를 가진 요청이 들어오면 스트러츠(Struts2)에서 처리한다.

struts-xxx.xml
<action name="xxx_list" class="xxx.action.XxxAction" method="findList">
    <result>/jsp/xxx/list.jsp</result>
</action>
xxx_list 라는 요청이 들어오면 xxx.action.XxxAction.findList() 가 호출되면 /jsp/xxx/list.jsp 로 이동하게 된다.

- 스트러츠 액션에서 서비스를 호출해서 사용하면 되는데 우리쪽에서는 모델1 방식이라서 jsp 를 직접 호출하게 되어 있다.
jsp 에서 서비스를 호출하는데 do라는 확장만 호출하게 되어 있는 구조라서 액션에서 편법을 사용했다.[각주:1]
public class XxxAction extends BaseAction(ActionSupport){
    ...
    public String findList() throws Exception {
        return SUCCESS;
    }
}
/jsp/xxx/list.jsp 로 이동해서 서비스를 호출해서 데이터를 처리하는 방식을 사용했다.
그런데 BaseAction 에 페이징관련 메서드가 있는 이유를 모르겠다.

- DAO는 iBatis를 사용
예외처리가 세련되지 못함.
public class XxxServiceImpl implements XxxService{
	
	private static XxxServiceImpl instance = new XxxServiceImpl();

	public static XxxServiceImpl getInstance(){
		return instance;
	}

	DaoManager daoManager = null;
	XxxDAO dao;

	private XxxServiceImpl(){  
		daoManager = DaoConfig.getDaoManager();
		dao = (XxxDAO)daoManager.getDao(XxxDAO.class);
	}

	public List findList() throws Exception{ //Exception 을 안던지게 하도록
		List list = null;
		try{
			list = dao.getList();
		} catch (DaoException de) { //여긴 없어도 될듯
			log.error(de, de);
			throw de;
		} catch (Exception e) {
			log.error(e, e);
			throw e;
		}
		return list;
	}

	public int insert(XxxVO vo) throws Exception{ //음...
		int result = 1;
		try{
			dao.insert(vo); //트랜잭션 처리를 하지 않아도 예외가 발생하면 Rollback된다.
		} catch (DaoException de) {
			result = 0;
			System.out.println("de="+de.toString());
		} catch (Exception e) {
			result = 0;
			System.out.println("e="+e.toString());
		}	
		return result;
	}

	public String change(XxxVO vo, String state) throws Exception{
		String err_msg = "FAIL";
		try{			
			daoMgr.startTransaction(); //명시적인 트랜잭션 처리
			
			err_msg = dao.update(vo);
			dao.insertState(vo, state);
			
			daoMgr.commitTransaction();
		} catch (DaoException de) {
			log.error(de, de);
			throw de;
		} catch (Exception e) {
			log.error(e, e);
			throw e;
		}finally{
			daoMgr.endTransaction();
		}
		return err_msg;
	}
	...
}
public class XxxDAOImpl extends SqlMapDaoTemplate implements XxxDAO{
	public XxxDAOImpl(DaoManager daoManager) {
		super(daoManager);
	}

	public List getList() throws DaoException{ //DaoException은 Runtime Exception이다.
		return (XxxVO) queryForObject("xxx.list");
	}
	...
}
- 일부 Ajax도 사용
  1. 그런데 jsp 에서 직접 서비스를 호출하는 것을 액션에서 처리하도록 수정하는데 얼마 안걸렸을거 같다. [본문으로]

JPetStore 분석 2 - iBatis

Product ID가 빠졌다.

catalog/Product.jsp
13번째 라인에서 item#productId 임.

1. item 을 product 로 변경한다.
<div id="Catalog">

  <h2><bean:write name="product" property="name"/></h2>

  <table>
    <tr><th>Item ID</th>  <th>Product ID</th>  <th>Description</th>  <th>List
      Price</th>  <th> </th></tr>
    <logic:iterate id="item" name="itemList">
      <tr>
        <td>
          <html:link paramId="itemId" paramName="item" paramProperty="itemId" page="/shop/viewItem.shtml">
            <bean:write name="item" property="itemId"/></html:link></td>
        <td><bean:write name="item" property="productId"/></td>
        <td>
          <bean:write name="item" property="attribute1"/>
          <bean:write name="item" property="attribute2"/>
          <bean:write name="item" property="attribute3"/>
          <bean:write name="item" property="attribute4"/>
          <bean:write name="item" property="attribute5"/>
          <bean:write name="product" property="name"/>
        </td>
        <td><bean:write name="item" property="listPrice" format="$#,##0.00"/></td>
        <td><html:link styleClass="Button" paramId="workingItemId" paramName="item" paramProperty="itemId" page="/shop/addItemToCart.shtml">
          Add to Cart</html:link></td>
      </tr>
    </logic:iterate>
    <tr><td>
      <logic:notEqual name="itemList" property="firstPage" value="true">
        <a class="Button" href="switchItemListPage.shtml?pageDirection=previous"><< Prev</a>
      </logic:notEqual>
      <logic:notEqual name="itemList" property="lastPage" value="true">
        <a class="Button" href="switchItemListPage.shtml?pageDirection=next">Next >></a>
      </logic:notEqual>
    </td></tr>
  </table>

</div>

Item.xml
product#productId 에는 들어가는데 item#productId 에는 들어가지 않는다.

2. 쿼리에 productId 를 추가한다.
<select id="getItemListByProduct" resultClass="item" parameterClass="string" cacheModel="itemCache">
	SELECT
		ITEMID,
		LISTPRICE,
		UNITCOST,
		SUPPLIER AS supplierId,
		I.PRODUCTID AS "product.productId",
		/* I.PRODUCTID, */
		NAME AS "product.name",
		DESCN AS "product.description",
		CATEGORY AS "product.categoryId",
		STATUS,
		ATTR1 AS attribute1,
		ATTR2 AS attribute2,
		ATTR3 AS attribute3,
		ATTR4 AS attribute4,
		ATTR5 AS attribute5
	FROM ITEM I, PRODUCT P
	WHERE P.PRODUCTID = I.PRODUCTID
	AND I.PRODUCTID = #value#
</select>

iBatis에서 주석은 -- 대신 /* */ 를 사용할 것.

JPetStore 분석 1 - iBatis

- JPetStore 5.0 Example Application
Struts 1.2, beanaction.jar(23,198  bytes)
iBatis 2

- web.xml
  • *.shtml 는 Struts action에서 처리하도록 함.
  • security-constraint 로 jsp파일을 직접 호출하지 못하게 함.
    <security-constraint>
    	<web-resource-collection>
    		<web-resource-name>Restrict access to JSP pages</web-resource-name> 
    		<url-pattern>*.jsp</url-pattern> 
    	</web-resource-collection>
    	<auth-constraint>
    		<description>With no roles defined, no access granted</description> 
    	</auth-constraint>
    </security-constraint>

- /shop/index.shtml
org.apache.struts.beanaction.BeanAction
name=catalogBean
parameter="*"
/catalog/Main.jsp
<%@ include file="../common/IncludeTop.jsp" %>
내용
<%@ include file="../common/IncludeBottom.jsp" %>

- /shop/viewCategory.shtml?categoryId=FISH
org.apache.struts.beanaction.BeanAction
name=catalogBean
catalogBean#viewCategory() //비지니스 메서드 호출
/catalog/Category.jsp
<bean:define id="category" name="catalogBean" property="category"/>
<bean:define id="productList" name="catalogBean" property="productList"/>
<logic:iterate id="product" name="productList">
<tr>
    <td>
        <html:link paramId="productId" paramName="product" paramProperty="productId" page="/shop/viewProduct.shtml">
            <bean:write name="product" property="productId"/>
        </html:link>
    </td>
    <td><bean:write name="product" property="name"/></td>
</tr>
</logic:iterate>
- BeanAction에서는 parameter 설정이 있으면 name의 해당 메서드 호출
parameter 설정이 없으면 path 에서 마지막 경로에 해당하는 메서드 호출
단, parameter가 *이면 어떤 메서드도 호출 안됨.

- 아래와 같이 특정 확장자에 대한 매핑이 가능
<action path="/test.elf" type="org.apache.struts.beanaction.BeanAction"
   name="catalogBean" parameter="*" validate="false">
<forward name="success" path="/t.jsp"/>
</action>

Open quote is expected for attribute "{1}" associated with an element type "parameterClass".

java.lang.RuntimeException: Error occurred. 
Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. 
Cause: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. 
Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. 
Cause: org.xml.sax.SAXParseException: Open quote is expected for attribute "{1}" associated with an  element type  "parameterClass".
    at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:49)
    at com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(SqlMapClientBuilder.java:63)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Person">
<select id="getPerson" parameterClass=”int” resultClass="examples.domain.Person">
	SELECT
	PER_ID as id,
	PER_FIRST_NAME as firstName,
	PER_LAST_NAME as lastName,
	PER_BIRTH_DATE as birthDate,
	PER_WEIGHT_KG as weightInKilograms,
	PER_HEIGHT_M as heightInMeters
	FROM PERSON
	WHERE PER_ID = #value#
</select>
...

” 를 " 로 바꾼다.

JPetStore 설치 - iBatis

- JPetStore-5.0/build/wars/jpetstore.war 로 배포할 수 있다.
또는
- Eclipse에서 구동시키기
  1. Dynamic Web Project 를 생성한다.
  2. Import
        src : JavaSource
        web : WebContent
        lib : WebContent/WEB-INF/lib

- 기본 설정이 hsqldb로 되어 있으므로 추가적인 설정이나 데이터베이스 없이 바로 구동할 수 있다.

- 다른 데이터베이스로 변경하는 경우에는
  1. JPetStore-5.0/src/ddl/ 의 스크립트를 사용하여 테이블을 생성하고 데이터를 입력한다.
  2. WEB-INF/classes/properties/database.properties 를 설정한다.
  3. Item.xml에서 getItem의 쿼리를 수정한다. : itemid 가 명확하지 않아 에러가 발생하므로 v.ITEMID로 변경한다.

iBATIS