* Account.xml의 이름을 member.xml로 변경
* 여기 이하 글은 다 지우기
* sqlMapConfig.xml : 전반적인 실행 환경에 대한 정보를 넣어 준다.
* 전역전인 설정을 sqlMapConfig.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>
<!-- 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>
<!-- xxxx.insert("insertMember", mdto); -->
<!-- Object obj = xxx.queryForObject("selectMemberById","abcde"); 추후 MemberDTO로 캐스팅해서 사용 -->
<?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>
<!-- Configure a built-in transaction manager. If you're using an
app server, you probably want to use its transaction manager
and a managed datasource -->
<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>
<!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data...) -->
<sqlMap resource="config/member.xml"/>
</sqlMapConfig>
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");//SqlMapConfig.xml과 연결되어있는 reader를 하나 읽어옴
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);
}
}
package ibatis.dto;
public class MemberDTO {
private String id;
private String password;
private String name;
private String registerNumber1; //db: register_number1
private String registerNumber2; //register_number2
private int mileage;
//생성자
public MemberDTO() {
super();
}
public MemberDTO(String id, String password, String name,
String registerNumber1, String registerNumber2, int mileage) {
super();
this.id = id;
this.password = password;
this.name = name;
this.registerNumber1 = registerNumber1;
this.registerNumber2 = registerNumber2;
this.mileage = mileage;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRegisterNumber1() {
return registerNumber1;
}
public void setRegisterNumber1(String registerNumber1) {
this.registerNumber1 = registerNumber1;
}
public String getRegisterNumber2() {
return registerNumber2;
}
public void setRegisterNumber2(String registerNumber2) {
this.registerNumber2 = registerNumber2;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
@Override
public String toString() {
return "MemberDTO [id=" + id + ", password=" + password + ", name="
+ name + ", registerNumber1=" + registerNumber1
+ ", registerNumber2=" + registerNumber2 + ", mileage="
+ mileage + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + mileage;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
result = prime * result
+ ((registerNumber1 == null) ? 0 : registerNumber1.hashCode());
result = prime * result
+ ((registerNumber2 == null) ? 0 : registerNumber2.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MemberDTO other = (MemberDTO) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (mileage != other.mileage)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (registerNumber1 == null) {
if (other.registerNumber1 != null)
return false;
} else if (!registerNumber1.equals(other.registerNumber1))
return false;
if (registerNumber2 == null) {
if (other.registerNumber2 != null)
return false;
} else if (!registerNumber2.equals(other.registerNumber2))
return false;
return true;
}
}
package ibatis.main;
import java.sql.SQLException;
import ibatis.dao.MemberDAO;
import ibatis.dto.MemberDTO;
public class TestMember {
public static void main(String[] args) throws SQLException {
MemberDTO mdto1 = new MemberDTO("id-66", "1111", "이상운", "111111", "1111111", 10000);
MemberDTO mdto2 = new MemberDTO("id-55", "2222", "장동건", "222222", "2222222", 20000);
MemberDTO mdto3 = new MemberDTO("id-77", "3333", "원빈", "333333", "3333333", 30000);
MemberDAO dao = MemberDAO.getInstance();
dao.insertMember(mdto1);
dao.insertMember(mdto2);
dao.insertMember(mdto3);
MemberDTO mdto4 = dao.selectMemberById("id-22");
System.out.println(mdto4);
}
}
* 결과
'프로그래밍 > ibatis(Mybatis)' 카테고리의 다른 글
iBatis 예제 - insert와 select하기 (0) | 2012.06.15 |
---|---|
iBATIS 실행 흐름도와 설정파일 (0) | 2012.06.15 |
iBATIS Quick Start (0) | 2012.06.15 |
framwork 종류와 개념 (0) | 2012.06.14 |
Ibatis 개요 (0) | 2012.06.14 |