Complete Spring IBatis project

here i am describing a very simple Spring project with IBatis and Display tag. let me describe the working environment. my another blog 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;

Eclipse create new project

Eclipse create new project

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-

output1

One Response to “Complete Spring IBatis project”

  1. Carole Says:

    Just dropping by.Btw, you website have great content!

    ______________________________
    Why this one-minute therapy is being suppressed in the U.S. while more than 15,000 European doctors have been using it to heal millions of patients

Leave a Reply