1. library 등록
  • jar:file:/C:/libraries/ibatis-2.3.4.726/lib/ibatis-2.3.4.726.jar!/
  • jar:file:/C:/oraclexe/app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14.jar!/


2. SqlMapConfig.xml      <--DTD 등록XML 생성
-----------------------------------------------
-//ibatis.apache.org/DTD sql Map Config 2.0//EN
http://ibatis.apache.org/dtd/sql-map-config-2.dtd
Root : <sqlMapConfig>
-----------------------------------------------
<?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>
    <transactionManager type="jdbc" commitRequired="false">          <--DB와 연결하고 트랜잭션관리
        <dataSource type="SIMPLE">                             <--커넥션풀 'SIMPLE'=iBATIS자체에서 관리
            <property name="JDBC.Driver" value="oracle.jdbc.OracleDriver"/>
            <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@203.236.209.50:1521:XE"/>
            <property name="JDBC.Username" value="test"/>
            <property name="JDBC.Password" value="0105"/>
        </dataSource>
    </transactionManager>
    <sqlMap resource="ex/employees.xml"/>

</sqlMapConfig>

3. Employees VO 생성

4. employees.xml 작성     <--DTD 등록
-----------------------------------------------
-//ibatis.apache.org/DTD Sql Map 2.0//EN
http://ibatis.apache.org/dtd/sql-map-2.dtd
Root : <sqlMap>
-----------------------------------------------
<?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>
     <typeAlias alias="empvo" type="ex.vo.EmpVO"/>          <---alias지정
    <select id="selectAll" resultClass="ex.vo.EmployeesVO">
        SELECT * FROM employees
    </select>
    <select id="selectByID" resultClass="ex.vo.EmployeesVO" parameterClass="String"> 
        SELECT * FROM employees WHERE id=#id#
    </select>
    <select id="selectByLikeName_Emp" resultClass="empvo" parameterClass="String">
        SELECT * FROM emp WHERE ename LIKE '%$ename$%'           
                         <--"J"  $안에는 다른 명령어까지 이어서 사용가능          
                         <--LIKE검색 #ename#으로 검색할경우 "%J%"         
    </select>
     <select id="selectByHiredate" parameterClass="String" resultClass="empvo">
        <![CDATA[                                         
        SELECT * FROM emp WHERE hiredate < #hiredate#
        ]]>
                         <--XML이므로 CDATA 섹션을써서 파싱안되고 그대로 실행하게하거나 , 
                         <--XML이므로 &lt; or &gt; 이용
    </select>
    <insert id="insert_Emp" parameterClass="ex.vo.EmpVO">
        INSERT INTO emp(empno, ename, job, hiredate, sal, deptno)
        VALUES (#empno#,#ename#,#job#,sysdate,#sal#,#deptno#)
    </insert>
                      <--INSERT 반환은 없고 입력은 객체로 sqlMap.insert("insert_Emp",emp);
     <insert id="insert_Board">                    <--DAO board.xml 따로만들고  시퀀스생성후
        INSERT INTO board VALUES(board_seq.nextval)
     </insert>

      <insert id="insert_Board2" parameterClass="ex.vo.BoardVO">     <--9i 이전버젼에선
        <selectKey keyProperty="boardno" resultClass="int">      
                  <--selectKey로 다음값을 검색한 int형 값을 ex.vo.BoardVo에 boardno에 넣고 그값을
            SELECT board_seq.nextval from dual
        </selectKey>
        INSERT INTO board VALUES(#boardno#)      <--boardno에 넣음
    </insert>
</sqlMap>

5. EmployeesClient.java 작성

package ex;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import ex.vo.EmployeesVO;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

public class EmployeesClient {
    public static void main(String[] args) throws IOException, SQLException {
        //1.iBATIS 설정파일인 SqlMapConfig.xml을 읽어온다.
        Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
        //2.읽어온 reader 로 SqlMapClient를 생성한다.
        SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
        //3.우리가 만든 id : selectAll인 select를 실행한다.
        List<EmployeesVO> employees = sqlMap.queryForList("selectAll");
        //4.출력
        for(EmployeesVO emp : employees){
            System.out.println("id : " + emp.getId());
            System.out.println("pwd : " + emp.getPassword());
            System.out.println("name"+ emp.getName());
            System.out.println("hiredate"+ emp.getHiredate());
        }
        EmployeesVO emp = (EmployeesVO)sqlMap.queryForObject("selectByID", "admin");
        System.out.println(emp.getName());
    }
}

신고

'programing > Framework' 카테고리의 다른 글

iBATIS 1강  (0) 2009.04.22
Posted by 대절님

댓글을 달아 주세요



티스토리 툴바