Skip to end of metadata
Go to start of metadata

To process SQL statements using MyBatis, the user needs to create a Configuration XML file and mapper XML file. These files allow programmers to easily map JavaBeans to PreparedStatement parameters and ResultSets. 

This chapter describes how to create Configuration XML file, Mapper XML file, and how to process SQL by using these files in the application.

Creating Mapper File

The Mapper XML file is a file that specifies the SQL statements to be transferred to the DB, mapping of parameters to be bound to PreparedStatement and mapping of ResultSet results.

The following is an example of creating a Mapper XML file that processes CRUD in the person table. (UserMapper.xml)

In the <insert>, <update>, <delete>, and <select> tags, each SQL statement for the CRUB operation is defined.

For more information about each tag, refer to

Creating Configuration File

The Configuration file is the SQL Maps configuration file that writes properties to control SqlMapClient in addition to setting dataSource, Mapper file path, and typeAliases for DB connection.

The following is an example of a configuration file (mybatis-config.xml).

In the <properties> tag, specify the path and name of the properties file where the properties defined in the form of name = value are written. In the <settings> tag, write the properties to control the configuration. In the <transactionManager> and <dataSource>, write the DB information to connect.

In addition, the <mappers> tag writes the path and name of the previously created Mapper files. Unlike the previous iBatis, MyBatis can set several DBs in the configuration file and set a specific DB at the time of creating SqlSessionFactory object, compared to the fact that only one DB setting can be done in iBatis.

For more information about each tag, refer to

Creating Application

CRUB operations can be processed by integrating with objects mapped to DB tables by using the instance of the SqlSession object provided by MyBatis in the application. In order to integrate with DB using MyBatis, first, read the configuration file with the configuration for the DB connection, create a SqlSession Factory object, obtain the SqlSession object from the object, and process the query mapped to the Mapper.

The following is an application that inserts, changes, and selects data in the user table of the DB. That part that gets the connection from the application was classified as a separate class.

Ex) of altibase_mybatis_sidu

First, read Mapper file to create SqlSessionFactory object and create SqlSession object from the created SqlSessionFactory object. The method to get SqlSession from Sample is as follows. 

sqlSessionFactory.openSession (boolean arg0);
The Boolean argument of the method is a value that determines the autocommit mode.(
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession(false);

Then, each method of CRUD class is called with id named in SqlSession object and Mapper.(
String statement = "com.altibase.sidu.mapper.UserMapper.updateUserData";
sqlSession.update(statement, update)

For more information about each tag, refer to

  • No labels