12.5. iBATIS SQL Maps

The iBATIS support in the Spring Framework much resembles the JDBC / Hibernate support in that it supports the same template style programming and just as with JDBC or Hibernate, the iBATIS support works with Spring's exception hierarchy and let's you enjoy the all IoC features Spring has.

Transaction management can be handled through Spring's standard facilities. There are no special transaction strategies for iBATIS, as there is no special transactional resource involved other than a JDBCConnection. Hence, Spring's standard JDBC DataSourceTransactionManager orJtaTransactionManager are perfectly sufficient.

Note

Spring does actually support both iBatis 1.x and 2.x. However, only support for iBatis 2.x is actually shipped with the core Spring distribution. The iBatis 1.x support classes were moved to the Spring Modules project as of Spring 2.0, and you are directed there for documentation.

12.5.1. Setting up the SqlMapClient

If we want to map the previous Account class with iBATIS 2.x we need to create the following SQL map'Account.xml':

<sqlMap namespace="Account">

 

  <resultMap id="result" class="examples.Account">

    <result property="name" column="NAME" columnIndex="1"/>

    <result property="email" column="EMAIL" columnIndex="2"/>

  </resultMap>

 

  <select id="getAccountByEmail" resultMap="result">

    select ACCOUNT.NAME, ACCOUNT.EMAIL

    from ACCOUNT

    where ACCOUNT.EMAIL = #value#

  </select>

 

  <insert id="insertAccount">

    insert into ACCOUNT (NAME, EMAIL) values (#name#, #email#)

  </insert>

 

</sqlMap>

The configuration file for iBATIS 2 looks like this:

<sqlMapConfig>

 

  <sqlMap resource="example/Account.xml"/>

 

</sqlMapConfig>

Remember that iBATIS loads resources from the class path, so be sure to add the 'Account.xml' file to the class path.

We can use the SqlMapClientFactoryBean in the Spring container. Note that with iBATIS SQL Maps 2.x, the JDBC DataSource is usually specified on the SqlMapClientFactoryBean, which enables lazy loading.

<beans>

 

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

    <property name="driverClassName" value="${jdbc.driverClassName}"/>

    <property name="url" value="${jdbc.url}"/>

    <property name="username" value="${jdbc.username}"/>

    <property name="password" value="${jdbc.password}"/>

  </bean>

 

  <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

    <property name="configLocation" value="WEB-INF/sqlmap-config.xml"/>

    <property name="dataSource" ref="dataSource"/>

  </bean>

 

</beans>

12.5.2. Using SqlMapClientTemplate and SqlMapClientDaoSupport

The SqlMapClientDaoSupport class offers a supporting class similar to the SqlMapDaoSupport. We extend it to implement our DAO:

public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao {

 

    public Account getAccount(String email) throws DataAccessException {

        return (Account) getSqlMapClientTemplate().queryForObject("getAccountByEmail", email);

    }

 

    public void insertAccount(Account account) throws DataAccessException {

        getSqlMapClientTemplate().update("insertAccount", account);

    }

}

In the DAO, we use the pre-configured SqlMapClientTemplate to execute the queries, after setting up the SqlMapAccountDao in the application context and wiring it with our SqlMapClient instance:

<beans>

 

  <bean id="accountDao" class="example.SqlMapAccountDao">

    <property name="sqlMapClient" ref="sqlMapClient"/>

  </bean>

 

</beans>

Note that a SqlMapTemplate instance could also be created manually, passing in the SqlMapClient as constructor argument. The SqlMapClientDaoSupport base class simply pre-initializes aSqlMapClientTemplate instance for us.

The SqlMapClientTemplate also offers a generic execute method, taking a customSqlMapClientCallback implementation as argument. This can, for example, be used for batching:

public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao {

 

    public void insertAccount(Account account) throws DataAccessException {

        getSqlMapClientTemplate().execute(new SqlMapClientCallback() {

            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {

                executor.startBatch();

                executor.update("insertAccount", account);

                executor.update("insertAddress", account.getAddress());

                executor.executeBatch();

            }

        });

    }

}

In general, any combination of operations offered by the native SqlMapExecutor API can be used in such a callback. Any SQLException thrown will automatically get converted to Spring's genericDataAccessException hierarchy.

12.5.3. Implementing DAOs based on plain iBATIS API

DAOs can also be written against plain iBATIS API, without any Spring dependencies, directly using an injected SqlMapClient. A corresponding DAO implementation looks like as follows:

public class SqlMapAccountDao implements AccountDao {

       

    private SqlMapClient sqlMapClient;

   

    public void setSqlMapClient(SqlMapClient sqlMapClient) {

        this.sqlMapClient = sqlMapClient;

    }

 

    public Account getAccount(String email) {

        try {

            return (Account) this.sqlMapClient.queryForObject("getAccountByEmail", email);

        }

        catch (SQLException ex) {

            throw new MyDaoException(ex);

        }

    }

 

    public void insertAccount(Account account) throws DataAccessException {

        try {

            this.sqlMapClient.update("insertAccount", account);

        }

        catch (SQLException ex) {

            throw new MyDaoException(ex);

        }

    }

}

In such a scenario, the SQLException thrown by the iBATIS API needs to be handled in a custom fashion: usually, wrapping it in your own application-specific DAO exception. Wiring in the application context would still look like before, due to the fact that the plain iBATIS-based DAO still follows the Dependency Injection pattern:

<beans>

 

  <bean id="accountDao" class="example.SqlMapAccountDao">

    <property name="sqlMapClient" ref="sqlMapClient"/>

  </bean>

 

</beans>

Posted by 나랑살자
,

itunes 한글 깨질때

defaults write -app iTunes AppleLanguages "(ko)"

defaults write -app iTunes AppleLanguages "(en)"

Posted by 나랑살자
,
1. 준비

Z_METADATA
- Z_VERSION: PRIMARY KEY, INTEGER
- Z_UUID: VARCHAR(255)
- Z_PLIST: BLOB

CREATE TABLE Z_METADATA (Z_VERSION INTEGER PRIMARY KEY, Z_UUID VARCHAR(255), Z_PLIST BLOB)


Z_PRIMARYKEY
- Z_ENT: PRIMARY KEY, INTEGER
- Z_NAME: VARCHAR
- Z_SUPER: INTEGER
- Z_MAX: INTEGER

CREATE TABLE Z_PRIMARYKEY (Z_ENT INTEGER PRIMARY KEY, Z_NAME VARCHAR, Z_SUPER INTEGER, Z_MAX INTEGER)

INSERT INTO Z_PRIMARYKEY (Z_ENT, Z_NAME, Z_SUPER, Z_MAX) VALUES ()


- Table 이름은 ‘Z’ prefix를 붙인다. 
(실제 Core Data의 table 이름에는 Z가 없다)

- 모든 field도 Z로 시작해야 한다. 
(Core Data에서는 app schema가 ‘name’이라는 attribute를 가져도 SQLite에서는 ‘Zname’으로 field로)

- 다음 세 field를 가져야 한다.
Z_PK: PRIMARY KEY, INTEGER
Z_ENT: INTEGER
Z_OPT: INTEGER













'개발 > iOS' 카테고리의 다른 글

[IOS] image size  (0) 2010.11.28
[XCODE] Breakpoint가 안먹을 때  (0) 2010.11.28
[XCODE] 메모리 관리 규칙  (0) 2010.11.16
[XCODE] Redefine NSLog  (0) 2010.11.16
[XCODE] DEBUGING 팁  (0) 2010.11.16
Posted by 나랑살자
,