Setting MyBatis in Spring

MyBatis, a free software distributed under the Apache License 2.0, helps to connect Spring with relational databases using XML or annotations.
Here’s a simple example loading a jsp page to display data from a mysql table.

  1. Create a mysql database named mybatis containing a table numbers whose dump is
  2. Create a project in NetBeans or in your development environment named HelloWorldMyBatis
  3. Add the following libraries in the directory lib of Tomcat
    • commons-dbcp-1.4.jar
    • commons-pool-1.5.6.jar

    from Apache Commons and the library

    • mysql-connector-java-5.1.17-bin.jar

    from MySQLYou need these libraries to connect with mysql.

  4. Add the following libraries in the directory /WEB-INF/lib
    • jstl-api-1.2.jar
    • jstl-impl-1.2.jar
  5. Add the following libraries in the directory /WEB-INF/lib
    • org.springframework.aop-3.0.6.RELEASE.jar
    • org.springframework.asm-3.0.6.RELEASE.jar
    • org.springframework.aspects-3.0.6.RELEASE.jar
    • org.springframework.beans-3.0.6.RELEASE.jar
    • org.springframework.context-3.0.6.RELEASE.jar
    • org.springframework.core-3.0.6.RELEASE.jar
    • org.springframework.expression-3.0.6.RELEASE.jar
    • org.springframework.instrument-3.0.6.RELEASE.jar
    • org.springframework.instrument.tomcat-3.0.6.RELEASE.jar
    • org.springframework.jdbc-3.0.6.RELEASE.jar
    • org.springframework.jms-3.0.6.RELEASE.jar
    • org.springframework.orm-3.0.6.RELEASE.jar
    • org.springframework.oxm-3.0.6.RELEASE.jar
    • org.springframework.test-3.0.6.RELEASE.jar
    • org.springframework.transaction-3.0.6.RELEASE.jar
    • org.springframework.web-3.0.6.RELEASE.jar
    • org.springframework.web.portlet-3.0.6.RELEASE.jar
    • org.springframework.web.servlet-3.0.6.RELEASE.jar
    • org.springframework.web.struts-3.0.6.RELEASE.jar
  6. Add the following libraries in the directory /WEB-INF/lib
    • mybatis-3.0.6-SNAPSHOT.jar
    • mybatis-spring-1.0.2-SNAPSHOT.jar
  7. Create the file /WEB-INF/web.xml
  8. Create the /WEB-INF/HelloWorldMyBatis-servlet.xml
  9. Create the file /WEB-INF/classes/log4j-helloworld.xml
  10. Create the file index.jsp in the application root
  11. Create the directory /WEB-INF/views and the file /WEB-INF/views/helloWorld.jsp
  12. Create the file /WEB-INF/classes/it/helloworld/controller/
  13. Create the file /WEB-INF/classes/it/helloworld/dao/model/
  14. Create the interface /WEB-INF/classes/it/helloworld/dao/ and its implementation /WEB-INF/classes/it/helloworld/dao/impl/

  15. Create the interface /WEB-INF/classes/it/helloworld/service/ and its implementation /WEB-INF/classes/it/helloworld/service/impl/

  16. Create the interface /WEB-INF/classes/it/helloworld/mybatis/
  17. Launch the application

3 Replies to “Setting MyBatis in Spring”

  1. Really really nice tutorial. Although I would like to suggest a few changes to the code as I struggled with it for hours.

    1.) Change the for log4j-helloworld to

    ** ** (Ignore the asterisks)

    2.) Add the following beans to the HelloWorldMyBatis-servlet.xml :

    3.) Add the following to :

    private SqlSession sqlSession;

    public void setsqlSession(SqlSession sqlSession)


    4.) Remove destroy-method from the dataSource bean.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.