iBatis
maven dependency
Spring Framework에 포함되어 있다.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-ibatis</artifactId>
<version>2.0.8</version>
</dependency>
Config(java)
public class MyAppSqlConfig {
//static 변수로 SqlMapClient를 설정해둔다.
private static final SqlMapClient sqlMap;
static {
try {
//실제 설정은 xml로..
String resource = "sqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
...
throw new RuntimeException();
}
}
//getter
public static SqlMapClient getSqlMapInstance () {
return sqlMap;
}
}
config(xml)
기존 튜토리얼 중에는 DOCTYPE decalairation 이 틀리게 되어 있는 것들이 있다. ibatis 사이트는 더이상 운영을 안하기 때문에 아래 url로 설정해야 한다.
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/.... .dtd">
전체 설정 xml
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC
"-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!--datasource 셋팅에 들어가는 설정값을 저장할 properties 파일 -->
<properties resource="SqlMapConfig.properties" />
<transactionManager type="JDBC" >
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
<sqlMap resource="Person.xml" /> <!--각 오브젝트별 sqlmap 설정 파-->
</sqlMapConfig>
optional fields
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<typeAlias alias="order" type="testdomain.Order"/>
Object(java) 별 설정 (xml)
Person.java
public class Person {
private int id;
private String firstName;
private String lastName;
private Date birthDate;
private double weightInKilograms;
private double heightInMeters;
//...getter 와 setter 메소드들을 아래 작성한다.
}
Person.xml (여기에 각 오브젝트별 쿼리문을 작성한다.)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Person">
<!--결과 테이블의 컬럼을 객체의 필드에 매핑-->
<resultMap id="PersonResult" class="pe.neocjmix.Person">
<result property="id" column="PER_ID"/>
<result property="firstName" column="PER_FIRST_NAME"/>
<result property="lastName" column="PER_LAST_NAME"/>
<result property="birthDate" column="PER_BIRTH_DATE"/>
<result property="weightInKilograms" column="PER_WEIGHT_KG"/>
<result property="heightInMeters" column="PER_HEIGHT_M"/>
</resultMap>
<select id="selectAllPersons" resultMap="PersonResult">
select * from PERSON
</select>
<!--#value# 는 javaBean 객체인 parameterClass 인스턴스의 properties 이다. resultClass에 return 할 타입의 full path 를 적는다..-->
<select id="getPerson" parameterClass="int" resultClass="pe.neocjmix.Person">
SELECT
PER_ID as id,
PER_FIRST_NAME as firstName, PER_LAST_NAME as lastName, PER_BIRTH_DATE as birthDate, PER_WEIGHT_KG as weightInKilograms,
PER_HEIGHT_M as heightInMeters
FROM PERSON
WHERE PER_ID = #value#
</select>
<insert id="insertPerson" parameterClass="pe.neocjmix.Person">
INSERT INTO
PERSON (PER_ID, PER_FIRST_NAME, PER_LAST_NAME,
PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M) VALUES (#id#, #firstName#, #lastName#,
#birthDate#, #weightInKilograms#, #heightInMeters#) </insert>
<update id="updatePerson" parameterClass="pe.neocjmix.Person">
UPDATE PERSON
SET PER_FIRST_NAME = #firstName#,
PER_LAST_NAME = #lastName#, PER_BIRTH_DATE = #birthDate#, PER_WEIGHT_KG = #weightInKilograms#,
PER_HEIGHT_M = #heightInMeters#
WHERE PER_ID = #id# </update>
<delete id="deletePerson" parameterClass="pe.neocjmix.Person">
DELETE from PERSON
WHERE PER_ID = #id# </delete>
</sqlMap>
사용(java)
SqlMapClient sqlMap = MyAppSqlConfig.getSqlMapInstance();
//get Object
Person person = (Person) sqlMap.queryForObject ("getPerson", 0);
//update object
person.setHeightInMeters(person.getHeightInMeters()+1);
sqlMap.update("updatePerson",person);
//delete object
sqlMap.delete("deletePerson", person);
//insert object
person.setLastName(person.getLastName() + "!");
sqlMap.insert("insertPerson", person);
//get list
return sqlMap.queryForList("selectAllPersons").toString();
일