Search Results for 'sqlMapConfig.xml'

1 POSTS

  1. 2007.08.13 iBatis 예제 책보고 따라하기

iBatis 예제 책보고 따라하기

Posted 2007. 8. 13. 21:18

어떤것이든지 일단 프로그램 돌려보고 화면이나 결과가 나와야 할 맛이 난다.
iBatis In Action 책에 나오는 첫번째 세상에서 가장 간단한 예제 따라서 코딩해봤다.
클래스 패스에 넣은 jar는 ibatis-2.3.0.677.jar, mysql-connector-java-5.0.6-bin.jar 두개뿐이다.

실행의 편이성을 위해 패키지 지정 안했다.
==================== Main.java ========================
public class Main {

 /**
  * @param args
  */
 public static void main(String[] args)throws Exception {
  // TODO Auto-generated method stub
  SqlMapClient sqlMap;

        String resource = "sqlMapConfig.xml";

        Reader reader = Resources.getResourceAsReader(resource);
        sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
        Map<String,String> map = new HashMap<String, String>();
        map.put("firstName","uncle");
        map.put("lastName","joe");
        map.put("updateName","Mark");
        map.put("actorId",(String)sqlMap.queryForObject("selectMaxActorId"));
         
       
        List list = sqlMap.queryForList("getActor", map);//(List)map.get("FIRST_NAME");
       
        System.out.println("Selected " + list.size() + " records."+map);
       
        int ii = sqlMap.update("updateUser", map);
        System.out.println("updateUser::::"+ii);
        //sqlMap.insert("insertUser", map);
        //System.out.println("ddddddd::"+se.);
       
        //sqlMap.queryForMap("getActor", map, "ACTOR");//  .queryForList("getActor", map);//(List)map.get("FIRST_NAME");
       
        list = sqlMap.queryForList("getAllActor", map);//(List)map.get("FIRST_NAME");
       
        System.out.println("Selected " + list.size() + " records.");
        for(int i=0;i<list.size();i++) {
         System.out.println(":::"+list.get(i));
        }   
 }

=============== sqlMapConfig.xml ===============
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
==> 이것도 옵션
<settings cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true"
maxRequests="32" maxSessions="10" maxTransactions="5" useStatementNamespaces="false"
/>
<!--Type aliases allow you to use a shorter name for long fully qualified class names. -->
<typeAlias alias="order" type="testdomain.Order"/>
<!--Configure a datasource to use with this SQL Map using SimpleDataSource.
Notice the use of the properties from the above resource -->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/test"/>
<property name="JDBC.Username" value="loginUser"/>
<property name="JDBC.Password" value="usrPassword"/>
<!--OPTIONAL PROPERTIES BELOW -->
<!-- property name="Pool.MaximumActiveConnections" value="10"/>
<property name="Pool.MaximumIdleConnections" value="5"/>
<property name="Pool.MaximumCheckoutTime" value="120000"/>
<property name="Pool.TimeToWait" value="10000"/>
<property name="Pool.PingQuery" value="select * from dual"/>
<property name="Pool.PingEnabled" value="false"/>
<property name="Pool.PingConnectionsOlderThan" value="0"/>
<property name="Pool.PingConnectionsNotUsedFor" value="0"/-->
</dataSource>
</transactionManager>
<!--Identify all SQL Map XML files to be loaded by this SQL map. Notice the paths
are relative to the classpath. For now, we only have one… -->
<sqlMap resource="sqlMap.xml" />

</sqlMapConfig>

======================= sqlMap.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>

<select id="getActor" resultClass="hashmap">
 select * from ACTOR
 where FIRST_NAME = #firstName#
     AND LAST_NAME  = #lastName#
</select>

<select id="getAllActor" resultClass="hashmap">
 select * from ACTOR
 where LAST_NAME  = #updateName#
</select>

<select id="selectMaxActorId" resultClass="int">
 select MAX(ACTOR_ID) AS ACTOR_ID from ACTOR
 where  FIRST_NAME = #firstName#
</select>


<insert id="insertUser">
 INSERT INTO ACTOR (FIRST_NAME, LAST_NAME)
 VALUES (
  #firstName#, #lastName#
 )
</insert>

<update id="updateUser">
 UPDATE ACTOR
 SET LAST_NAME = #updateName#
 WHERE ACTOR_ID = #actorId#
</update>

</sqlMap>


=============================
요렇게 해놓구 eclipse에서 실행했는데....


=================== 결과 ===================
Selected 12 records.{lastName=joe, updateName=Mark, actorId=null, firstName=uncle}
updateUser::::0
Selected 2 records.
:::{first_name=uncle, last_update=2007-07-27 19:59:46.0, actor_id=213, last_name=Mark}
:::{first_name=uncle, last_update=2007-07-27 20:01:24.0, actor_id=214, last_name=Mark}

요렇게 나온다....