Simple Example of Mybatis JAVA Maven Implementation

Mybatis is a powerful and flexible persistent framework. One of the advantage of it, compared to Hibernate, is that you don’t have to pick up new script language. You still create the sql, as you normally create in JDBC implementation, but you will save a lot of low-level JDBC coding.

I am going to walk you through step by step a simple example, probably the simplest example you can find. You will need eclipse as IDE, mysql as testing database.

1. Create a maven project with eclipse, and update pom.xml file.

<project xmlns="" xmlns:xsi=""





Note: there are 3 dependencies here. JUnit, MYBATIS and MySQL JDBC driver.

2. run the following sql to create testing database, testing table and inserting a testing record.

delimiter $$
delimiter $$

CREATE TABLE `category` (
  `category_id` int(11) NOT NULL AUTO_INCREMENT,
  `category_name` varchar(45) NOT NULL,
  PRIMARY KEY (`category_id`)

INSERT INTO `category` VALUES (1,'Apparel');

3. create a database configuration file, database-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-// Config 3.0//EN"

	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/test" />
				<property name="username" value="root" />
				<property name="password" value="" />
		<mapper resource="com/springriver/example/mybatis/mapper/CategoryMapper.xml" />

Note: the configuration file contains database access information, and a mapper file which we will explain next.

4. Create a database connection factory java class

package com.springriver.example.mybatis.util;


import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class ConnectionFactory {
	private static SqlSessionFactory factory;

	static {
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader("database-config.xml");
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		factory = new SqlSessionFactoryBuilder().build(reader);

	public static SqlSessionFactory getSqlSessionFactory() {
		return factory;

Note: this util class read in the database configuration file, and maintains a global SqlSessionFactory instance. SqlSessionFactory is used to open new database session.

5. Create a mybatis mapper xml file to define the database functions, CategoryMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-// Mapper 3.0//EN"
<mapper namespace="com.springriver.example.mybatis.mapper.CategoryMapper">

<select id="selectCategoryById" parameterType="int" resultType="com.springriver.example.mybatis.bean.Category">
	select category_name as categoryName from Category where category_id = #{id}


Note: this mapper file is registered in database-config.xml file

6. create a java interface file,, to expose the functions defined in CategoryMapper.xml

package com.springriver.example.mybatis.mapper;

import com.springriver.example.mybatis.bean.Category;

public interface CategoryMapper {
	public Category selectCategoryById(int id);

7. create a manager file,, to create business functions out of the database functions.

package com.springriver.example.mybatis.util;

import org.apache.ibatis.session.SqlSession;

import com.springriver.example.mybatis.bean.Category;
import com.springriver.example.mybatis.mapper.CategoryMapper;

public class CatgegoryManager {
	public static Category selectCategoryById(int id) {
		SqlSession sqlSession = ConnectionFactory.getSqlSessionFactory()
		try {
			CategoryMapper categoryMapper = sqlSession
			return categoryMapper.selectCategoryById(id);
		} finally {

8. create a junit testing class,, to test the manager function.

package com.springriver.example.mybatis;

import org.junit.Test;

import com.springriver.example.mybatis.bean.Category;
import com.springriver.example.mybatis.util.CatgegoryManager;

public class CategoryManagerTest {

	public void testUpdateUser() {
		Category category = CatgegoryManager.selectCategoryById(1);
		assert(category != null);


Here is the code structure.

download the eclipse project

Tagged with: , , , , , ,
Posted in MyBatis
2 comments on “Simple Example of Mybatis JAVA Maven Implementation
  1. vit bernatik says:

    Dude you are missing step where you shall create “”. I mean thanks – you have helped me a lot. But I always ask myself – what kind of guy write such a great tutorial and miss one step? Without any step whole tutorial would be kinda useless… Anyway the mentioned file is provided in download of eclipse project that you have attached at the end of tutorial – so it has seved a day for me… Thanks again…

  2. india says:

    This example is awesome

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>