Spring ibatis example

Spring ibatis example

Here I am describing a spring ibatis example. It is a very simple Spring project with iBatis and Display tag. let me describe the working environment. my another post shows how to set up your environment.

note – this project is available at google code.
project name – springibatis
# Non-members may check out a read-only working copy anonymously over HTTP.

url(springibatis-read-only): svn checkout http://springibatis.googlecode.com/svn/trunk/

Eclipse 3.4: Ganymede;

Plugs in: svn, Maven, spring-ide, tomcat lunher, ant.

Tomcat: 5.5

FireFox: 3

Create a new project in Eclipse;

spring ibatis example - Eclipse create new project

Eclipse create new project

spring ibatis example - newproject1

create one package name:  click the right button on the src folder and select new -> Package and put  com.rajib.spring.ibatis value.

create a class Person under the ibatis folder. similar way create some other files – Person.xml, PersonDao.java, PersonDaoImpl.java

create applicationContext-ibatis.xml under src folder. we will describe at web.xml that where the applicationContext file is located. it looks

——————  applicationContext-ibatis.xm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" id="dataSource">
<!-- property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/-->
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost/development"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>

<!-- Transaction manager for a single JDBC DataSource -->
<bean  class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- SqlMap setup for iBATIS Database Layer -->
<bean class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" id="sqlMapClient">
<property name="configLocation">
<value>classpath:SqlMapConfig.xml</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="personDao" class="com.rajib.spring.ibatis.PersonDaoImpl">
<property name="dataSource" ref="dataSource"></property>
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>

</beans>

 

here we create one bean ‘dataSource’ that store database connnection. we create another bean sqlMapClient that will be use all ibatis query. we push datasource bean to sqlMapClient bean. these 2 bean will use personDao later.

I used a new display tag that does column display, sorting, paging, cropping, grouping, exporting, smart linking and decoration of a table.

In index.jsp,

BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"applicationContext-ibatis.xml"));
PersonDao personDao = (PersonDao)beanFactory.getBean("personDao");
List<Person> allPersons = personDao.getAllPersons();
request.setAttribute( "persons", allPersons );

this is used to collecting data from database. the persons list is put into request so that the display tag can used it easily.

<display:table name="persons"  class="mars" pagesize="25">
<display:column property="id" title="ID" />
<display:column property="name" />
</display:table>

the output looks-

Spring ibatis example - output1