Step 1
Drop iBatis jar in lib folder of your Grails application.
Step 2
Define iBatis beans in Spring application context. In your resources.xml file add something like this:
<bean id="ibatisTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:ibatis/sqlmap-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
Or, if you prefer using resources.groovy:
ibatisTemplate(org.springframework.orm.ibatis.SqlMapClientTemplate) {
sqlMapClient = ref("sqlMapClient")
}
sqlMapClient(org.springframework.orm.ibatis.SqlMapClientFactoryBean) {
configLocation = "classpath:ibatis/sqlmap-config.xml"
dataSource = ref("dataSource")
}
Step 3
Create iBatis configuration files in your application conf folder. For instance;
conf/ibatis/sqlmap-config.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>
<settings useStatementNamespaces="true"/>
<sqlMap resource="ibatis/Book.xml"/>
</sqlMapConfig>
conf/ibatis/Book.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="Book">
<resultMap id="result" class="Book">
<result property="id"/>
<result property="author"/>
<result property="title"/>
</resultMap>
<select id="list" resultMap="result">
select id, author, title from book
</select>
</sqlMap>
Step 4
Use the ibatisTemplate bean in any Grails artifact which accepts bean injection. You even can test it in Grails shell:
groovy:000> ctx.getBean('ibatisTemplate').queryForList('Book.list')
impresionante!
ReplyDeleteVery interesting! I'll have to try this!
ReplyDeleteAny further thoughts on the subject? I really love iBatis and just don't have the faith in Hibernate to "do the right thing" when it comes to very complex SQL.
ReplyDelete