Configure Database Connection using MyBatis

Step 1: Create a propertiese file and store the relevant MySQL connection details here.

In my case the file contains the following

Step 2: Create a configuration.xml file, this should contain all the required information needed to connect to your MySQL instance.  you need to reference the above propertiese file in the XML config file.

< ?xml version="1.0" encoding="UTF-8" ?>
< !DOCTYPE configuration PUBLIC "-// Config 3.0//EN" "">
<properties resource="" />
<environments default="staging">
<environment id="staging">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>

Step 3: We now use SqlSessionFactoryBuilder to create an instance of SqlSessionFactory.


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

public class SqlConnection {
private static SqlConnection sqlFactory = null;
private static SqlSessionFactory sessionFactory = null;

private SqlConnection() {


private static SqlConnection getInstance() throws IOException {
String resource = null;
Reader reader = null;

if(sqlFactory == null) {
resource = "configuration.xml";
reader = Resources.getResourceAsReader(resource);

sqlFactory = new SqlConnection();
sqlFactory.sessionFactory = new SqlSessionFactoryBuilder().build(reader);
return sqlFactory;

public static synchronized SqlSessionFactory getSessionFactory() throws IOException {
if(sqlFactory == null) {
return sessionFactory;

public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();

Step 4: We can now get a new session that will allow us to make database calls.

SqlSession session = null;
    session = SqlConnection.getSessionFactory().openSession();
    System.out.println("Session Object Created: " + session);

Step 5: Create a domain object, to store our data in the object world. The attributes / fields of our simple bank account record resemble the structure of our bankrecords table in the database.


import java.util.Date;

public class BankAccountRecord implements Serializable {
private Integer accountID;
private String firstName;
private String surName;
private Date setupDate;
private Integer accountBalance;
private Date lastModified;
public BankAccountRecord() {

// Define the getters and setters for our pojo.

public String toString() {
return "Record{" +
"ID=" + accountID +
", Firstname='" + firstName + "'" +
", Surname=" + surname +
", Setup Date=" + setupDate +
", Account Balance=" + accountBalance+


Step 6: We now define a mapper interface, we'll create an file that contains a method to get data from the table. At this time the interface will be as the following:



public interface BankAccountRecordMapper {
     * Get a single Bank Account Record from the database based on the bank account identifier
     * @param id is bank acccount identifier.
     * @return a Bank Account Record object.
    BankAccountRecord getBankAccountRecord(int id);


Post a Comment

Popular posts from this blog

ActiveMQ, easy to use open source message oriented middleware (MOM)

Basic Send Message to MQ with Java and IBM MQ JMS