<JDBC 연결을 위한 DB서버 정보 세팅>
database.propertis
#ORACLE 10g DB server관련 드라이버,URL,계정,비밀번호 세팅
db_driver=oracle.jdbc.driver.OracleDriver
db_url=jdbc:oracle:thin:@localhost:1521:XE
db_usernm=scott
db_pw=tiger
앞의 프러퍼티 이름은 임의로 정해주면 된다.
ORM을 위한 POJO 객체를 생성한다.
Member.java
//POJO
package com;
import java.io.Serializable;
public class Member implements Serializable {
private String id;
private String pw;
private String nm;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNm() {
return nm;
}
public void setNm(String nm) {
this.nm = nm;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
}
테이블과 연관하기 위한 정보및 쿼리를 작성한다.
Member.xml
<?xml version="1.0" encoding="EUC-KR" standalone="no"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Member">
<!--
Member
-->
<typeAlias alias="member" type="com.Member" /><!-- pojo클래스 별칭주기 -->
<resultMap id="getMemberAll" class="member"><!-- 클래스 프러퍼티와 디비 컬럼맵핑 -->
<result property="id" column="id" />
<result property="pw" column="pw" />
<result property="nm" column="nm" />
</resultMap>
<select id="getMemberAll" resultMap="getMemberAll">
SELECT id, pw, nm FROM MEMBER
</select>
<select id="getMember" resultMap="getMemberAll">
SELECT id, pw, nm FROM MEMBER
WHERE ID=#id#
</select>
<insert id="insertMember" parameterClass="member">
INSERT INTO MEMBER (id, pw, nm) VALUES (#id#, #pw#, #nm#)
</insert>
<update id="updateMember" parameterClass="member">
UPDATE MEMBER SET pw = #pw#, nm = #nm#
WHERE ID = #id#
</update>
<delete id="deleteMember" parameterClass="member">
DELETE FROM MEMBER WHERE ID = #id#
</delete>
</sqlMap>
PreparedStatement에서 동적으로 값을 할당하기 위해 ?를 사용 하듯이
#id#처럼 동적으로 값이 할당 되는 부분을 정한다.
SqlMapClientManager.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>
<properties resource="com/database.properties" /><!-- 디비 정보 프러퍼티 세팅 -->
<settings cacheModelsEnabled="false"
useStatementNamespaces="true"
/>
<transactionManager type="JDBC"> <!-- 프러퍼티 값을 세팅-->
<dataSource type="SIMPLE">
<property name ="JDBC.Driver" value="${db_driver}"/>
<property name ="JDBC.ConnectionURL" value="${db_url}"/>
<property name ="JDBC.Username" value="${db_usernm}"/>
<property name ="JDBC.Password" value="${db_pw}"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/Member.xml" /> <!-- pojo, 테이블을 맵핑한 xml들 -->
</sqlMapConfig>
프러퍼티에 세팅한 정보를 가져와 JDBC관련 세팅을 하고,
pojo와 연관시킨 xml를 읽어 들인다.
SqlMapClientManager.java
package com;
import java.io.Reader;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
/**
*
* @author Administrator
*
*/
public class SqlMapClientManager {
private static final SqlMapClient sqlMap;
static {
try {
String resource = "com/SqlMapClientManager.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
private SqlMapClientManager(){}
public static SqlMapClient getSqlMapClient() {
return sqlMap;
}
}
SqlMapClientManager.xml 파일을 읽어 들여 SqlMapClient 인스턴스를 생성한다.
MemberDAO.java
package com;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;
public class MemberDAO {
private static SqlMapClient sqlMap = SqlMapClientManager.getSqlMapClient();
public List listAll(){//모든 회원 조회
Member paramMember = new Member();
Member returnMember = new Member();
List list=null;
try {
list = sqlMap.queryForList("Member.getMemberAll", paramMember);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public Member getMember(Member member){ //회원 정보 조회
Member paramMember = member;
Member returnMember = new Member();
try {
member = (Member) sqlMap.queryForObject("Member.getMember", paramMember);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return member;
}
public void insert(Member member){ //회원 등록
Member paramMember = member;
System.out.println("insert Member");
System.out.println(member.getId());
System.out.println(member.getNm());
System.out.println(member.getPw());
try {
sqlMap.insert("Member.insertMember",paramMember);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(Member member){
// 사용자 수정
Member paramMember = member;
try {
sqlMap.update("Member.updateMember",paramMember);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void delete(Member member){
// 사용자 삭제
Member paramMember = member;
try {
sqlMap.update("Member.deleteMember",paramMember);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
sqlMapClient 인스턴스를 통해 MEMBER테이블에 접근하기 위한 API제공
[출처] [본문스크랩] IBatis 기본 CRUD 테스트 해보기|작성자 옹씨루