728x90
반응형

세팅 ( http://tylee82.tistory.com/60 )이 완료되면 이제 코딩을 해보겠습니다.
머드초보의 블러그의 글을 참고로 작성된 글임을 다시한번 강조합니다.



<< MySQL >>
일단 DB 구조입니다. 
CREATE TABLE `products` (

  `id` int(11) NOT NULL,
  `description` varchar(255) default NULL,
  `price` decimal(15,2) default NULL,
  PRIMARY KEY  (`id`),
  KEY `products_description` (`description`)
) ENGINE=MyISAM DEFAULT CHARSET=UTF8;

INSERT INTO `products` (`id`, `description`, `price`) VALUES 
(1, 'Lamp', 391.50),
(2, 'Table', 2918.85),
(3, 'Chair', 884.27);

※ mysql 설치시 charset을 utf-8로 설정을 하였기때문에 UTF8로 charset을 잡았습니다.

<< 프로젝트 구조 >>

그냥 src폴더는 자바서버단 폴더구요. flex_src폴더는 플렉스클라이언트단 폴더입니다.



<< web.xml 의 수정 (스프링 설정) >>

web.xml 아래 코드 추가
<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>


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

applicationContext*라고 적어 놓은 이유가 ibatis랑 분리를 하려고 합니다.
설정파일은 applicationContext.xml과 applicationContext-ibatis.xml 두개를 사용할껍니다.

※ 위 코드 추가시 주의 사항 : web.xml의 엘리먼트의 위치는 순서가 있습니다. 순서가 맞지 않는다면 에러가 납니다. ( 참고글 : http://woongbox.tistory.com/tag/The%20content%20of%20element%20type%20"web-app"%20must%20match )

<< 소스코딩 >>
src에다가 코딩을 해봅시다.
java는 perspective를 Java EE로 바꾸고 합시다.


우선 domain부분에 ValueObject를 하나 만들어봅시다.
Java Resources:src에다가 New를 하고 class를 선택합니다.
package는 springapp.domain이라고 하구요.
Name은 Product라고 하고 Finish

Product.java

package springapp.domain;

public class Product {

 
private int id;
 
private String description;
 
private Double price;

 
public int getId() {
 
return id;
 
}

 
public void setId(int id) {
 
this.id = id;
 
}

 
public String getDescription() {
 
return description;
 
}

 
public void setDescription(String description) {
 
this.description = description;
 
}

 
public Double getPrice() {
 
return price;
 
}

 
public void setPrice(Double price) {
 
this.price = price;
 
}
}

ProductDao를 만들어봅시다.
Java Resource:src에 New해서 interface를 추가합시다.
package에다가 springapp.dao라고 써놓고, 
Name에다가 ProductDao라고 씁시다.

package springapp.dao;

import java.util.List;
import springapp.domain.Product;

public interface ProductDao {
 
public List<Product> getProductList();
}

딸랑 ProductList만 가져오는 메소드가 있어요! 
저 Dao인터페이스를 구현해봅시다!!!

package springapp.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import springapp.domain.Product;

public class ProductDaoImpl extends SqlMapClientDaoSupport implements
 
ProductDao {

 
protected final Log logger = LogFactory.getLog(getClass());

 
@SuppressWarnings("unchecked")
 
@Override
 
public List<Product> getProductList() {
  logger
.info("Getting products!");
 
return getSqlMapClientTemplate().queryForList("getProductList");
 
}
}

<< ibatis >>
ibatis부분인데요. 설정파일을 보도록 하겠습니다.

src/springapp/dao/SqlMapConfig.xml

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

<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">


<sqlMapConfig>
 
<typeAlias alias="Product" type="springapp.domain.Product"/>

 
<sqlMap resource="springapp/dao/MySQLProduct.xml" />
</sqlMapConfig>

alias지정해주고, resource는 src/springapp/dao/MySQLProduct.xml파일입니다.

MySQLProduct.xml

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

<!DOCTYPE sqlMap      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">


<sqlMap namespace="Product">

 
<resultMap id="ProductMap" class="Product">
 
<result property="id" column="id" />
 
<result property="description" column="description" />
 
<result property="price" column="price" />
 
</resultMap>

 
<select id="getProductList" resultMap="ProductMap">
  select id, description, price from products
 
</select>

</sqlMap>

getProductList라는 것은 products테이블에서 내용을 select하는 것이네요.
Dao가 완성되었네요! 너무 길어지니 다음이시간에 ...

728x90
반응형

+ Recent posts