1. iBATIS SqlMap Config(sqlmap-config.xml) 작성
- iBATIS framework에 대한 설정
<?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">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:XE"/>
<property name="JDBC.Username" value="scott"/>
<property name="JDBC.Password" value="tiger"/>
</dataSource>
</transactionManager>
<sqlMap resource="config/member.xml"/>
</sqlMapConfig>
2. SqlMap 파일 작성(User.xml) - SQL문을 등록하는 설정파일
<?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>
<!-- insert 태그 작성 -->
<insert id="insertMember" parameterClass="ibatis.dto.MemberDTO">
insert into member(id, password, name, register_Number1, register_Number2, mileage) values(#id#,#password#,#name#,#registerNumber1#,#registerNumber2#,#mileage#)
</insert>
<!-- select 태그 작성 -->
<select id="selectMemberById" parameterClass="java.lang.String" resultClass="ibatis.dto.MemberDTO">
select id,
password
name,
register_Number1 as registerNumber1,
register_Number2 as registerNumber2,
mileage
from member
where id=#id#
</select>
</sqlMap>
<!-- xxx.insert("insertMember", mdto); -->
<!-- Object obj = xxx.queryForObject("selectMemberById", "id"); 나중에 MemberDTO로 캐스팅 -->
3. iBATIS Data Access Object 작성
package ibatis.dao;
import ibatis.dto.MemberDTO;
import java.io.Reader;
import java.sql.SQLException;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class MemberDAO {
private static MemberDAO instance = new MemberDAO();
private SqlMapClient sqlMap;//sql문 실행 메소드를 가진 객체
private MemberDAO(){
try {
Reader reader = Resources.getResourceAsReader("config/SqlMapConfig.xml");
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public static MemberDAO getInstance(){
return instance;
}
//회원 데이터 insert 메소드
public void insertMember(MemberDTO mdto) throws SQLException{
sqlMap.insert("insertMember", mdto);
}
//id로 회원 정보 조회
public MemberDTO selectMemberById(String id) throws SQLException{
return (MemberDTO) sqlMap.queryForObject("selectMemberById", id);
}
}
'프로그래밍 > ibatis(Mybatis)' 카테고리의 다른 글
iBatis 예제 - insert와 select하기 (0) | 2012.06.15 |
---|---|
iBATIS 실행 흐름도와 설정파일 (0) | 2012.06.15 |
ibatis 시작하기[실습] (0) | 2012.06.14 |
framwork 종류와 개념 (0) | 2012.06.14 |
Ibatis 개요 (0) | 2012.06.14 |