iBatis기본 설정 및 사용방법(회원가입 및 수정 탈퇴 예제)입니다.
war파일 첨부 합니다.
필요한 jar 파일들(war파일에 모두 포함되어 있습니다!!)
ibatis-2.3.4.726.jar(ibatis)
jstl.jar(jstl)
standard.jar(jstl)
ojdbc14.jar(jdbc)
데이터베이스(Oracle)
테이블 구성(user_t 테이블)
열 이름 | 데이터 유형 | 널 가능 | 기본값 | 기본 키 |
---|---|---|---|---|
USRID | VARCHAR2(20) | No | - | 1 |
UNAME | VARCHAR2(30) | No | - | - |
UPW | VARCHAR2(20) | Yes | - | - |
UCMT | VARCHAR2(4000) | Yes | - | - |
=============== IBatisUtil.java ==================
package exam.ibatis.util;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;
import java.io.Reader;
import java.io.IOException;
public class IBatisUtil {
private static SqlMapClient sqlMapper;
static {
try {
//클래스 패스 경로
Reader reader = Resources.getResourceAsReader(
"exam/ibatis/sqlmap/SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
// Fail fast.
throw new RuntimeException(
"Something bad happened while building the SqlMapClient instance." + e, e);
}
}
public static SqlMapClient getSqlMapClient(){
return sqlMapper;
}
}
=============== 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>
<!-- 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="java"/>
<property name="JDBC.Password" value="java"/>
</dataSource>
</transactionManager>
<!-- List the SQL Map XML files. They can be loaded from the
classpath, as they are here (com.domain.data...) -->
<sqlMap resource="exam/ibatis/sqlmap/user.xml"/>
<!-- List more here...
<sqlMap resource="com/mydomain/data/Order.xml"/>
<sqlMap resource="com/mydomain/data/Documents.xml"/>
-->
</sqlMapConfig>
=============== user.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="user_t">
<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAlias alias="user" type="exam.ibatis.dto.UserDto"/>
<!-- Result maps describe the mapping between the columns returned
from a query, and the class properties. A result map isn't
necessary if the columns (or aliases) match to the properties
exactly. -->
<!-- Select with no parameters using the result map for Account class. -->
<select id="selectAllUsers" resultClass="exam.ibatis.dto.UserDto">
select usrid,uname,upw,ucmt,rn
from(select usrid,uname,upw,ucmt,ceil(rownum/30)as rn
from(select * from user_t order by usrid desc))a
where rn = 1
</select>
<!-- 회원 등록 -->
<insert id="registerUser" parameterClass="exam.ibatis.dto.UserDto">
insert into user_t (usrid,uname,upw,ucmt)
values(#usrid#,#uname#,#upw#,#ucmt#)
</insert>
<!-- 상세 보기용 -->
<select id="selectUser" parameterClass="String" resultClass="exam.ibatis.dto.UserDto">
select * from user_t where usrid = #usrid#
</select>
<!-- 정보 수정 -->
<insert id="updateUser" parameterClass="exam.ibatis.dto.UserDto">
update user_t set upw=#newUpw#,uname=#uname# where usrid = #usrid#
</insert>
<!-- 회원 탈퇴 -->
<insert id="deleteUser" parameterClass="String">
delete user_t where usrid=#usrid#
</insert>
</sqlMap>
================= UserDao.java ===============
package exam.ibatis.dao;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;
import exam.ibatis.dto.UserDto;
import exam.ibatis.util.IBatisUtil;
public class UserDao {
public void insert(UserDto dto) throws ClassNotFoundException, SQLException{
SqlMapClient client = IBatisUtil.getSqlMapClient();
client.insert("registerUser", dto);
}//end insert
public List<UserDto> list() throws ClassNotFoundException, SQLException{
SqlMapClient client = IBatisUtil.getSqlMapClient();
return client.queryForList("selectAllUsers");
}
public UserDto findById( String usrid ) throws ClassNotFoundException, SQLException{
SqlMapClient client = IBatisUtil.getSqlMapClient();
return (UserDto)client.queryForObject("selectUser",usrid);
}
public void update( UserDto dto ) throws ClassNotFoundException, SQLException{
SqlMapClient client = IBatisUtil.getSqlMapClient();
client.update("updateUser", dto);
}
public void delete( String usrid) throws ClassNotFoundException, SQLException{
SqlMapClient client = IBatisUtil.getSqlMapClient();
client.delete("deleteUser",usrid);
}
}//end class
'SQL > IBATIS' 카테고리의 다른 글
ibatis iterate ( map , list , array ) (0) | 2014.02.12 |
---|---|
iBatis 예제 - 3 (iBatis 프로시저(procedure)를 이용한 데이터 insert) (0) | 2014.02.12 |
iBatis 예제 - 2 (iBatis iterate를 이용한 동적 쿼리) (0) | 2014.02.12 |
iBatis 예제 - 1 (iBatis 기본설정 및 데이터 출력) (0) | 2014.02.12 |
iBatis 동적인 SQL 항목별 요소 (0) | 2014.02.12 |
iBatis와 DBCP 바로알기 (0) | 2014.02.12 |
ibatis iterate 사용하기 (0) | 2014.02.12 |
[iBatis] iterate 사용법 (0) | 2014.02.12 |