TechnoBuzz

A Techno Blog, mainly about Java

The IBatis Model

Been a while since I started first looking at IBatis. The Spring Framework has a sample application named JPetstore (available in the core download) that gives you an example of IBatis, Spring, and Struts 1.x .

Different Spring Ibatis SQLMaps approaches:

Use Of Annotations

Implementing DAOs based on plain iBATIS API

<bean
id=”dataSource”
class=”org.springframework.jdbc.datasource.SingleConnectionDataSource”
destroy-method=”destroy”
>

<bean id=”transactionManager”
class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
<property name=”dataSource” ref=”dataSource”/>
</bean>

<bean id=”sqlMapClient” class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”>
<property name=”configLocation” value=”classpath:/sqlmap-config.xml”/>
<property name=”dataSource” ref=”dataSource”/>

</bean>

<?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”&gt;
<sqlMapConfig>
<settings defaultStatementTimeout=”5″ />
<sqlMap resource=”MyType.xml” />
</sqlMapConfig>

You can actually omit the ‘transaction-manager’ attribute in the <tx:annotation-driven/> tag if the bean name of the PlatformTransactionManager that you want to wire in has the name ‘transactionManager’.

<!– Instructs Spring to perfrom declarative transaction managemenet on annotated classes –>
<tx:annotation-driven/>

@Autowired
public void setSqlMapClient(SqlMapClient sqlMapClient) {
iBatisTemplate = new SqlMapClientTemplate(sqlMapClient);
}

@@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)

– The @Transactional annotation may be placed before an interface definition, a method on an interface, a class definition, or a public method on a class. However, please note that the mere presence of the @Transactional annotation is not enough to actually turn on the transactional behavior.
public MyType insertMyType(MyType myType) {
MyType myType = null;
myType= (MyType) iBatisTemplate.insert(“insertIntoMyType”, MyType;
return myType;
}

Use Of Spring’s DaoSupport Class

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

<bean id=”sqlMapClient” class=”org.springframework.orm.ibatis.SqlMapClientFactoryBean”>
<property name=”configLocation” value=”classpath:/sqlmap-config.xml”/>
<property name=”dataSource” ref=”dataSource”/>
</bean>

<?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”&gt;
<sqlMapConfig>
<settings useStatementNamespaces=”true” />
<sqlMap resource=”person.xml” />
</sqlMapConfig>

PersonDaoIbatisImpl extends SqlMapClientDaoSupport

public void addPerson(Person person) {
Long id = (Long) this.getSqlMapClientTemplate().insert(“person.insert”,
person);
person.setId(id);
}

Advertisements

August 17, 2008 Posted by | Uncategorized | 1 Comment