mybatis
Create MyBatis SqlSession
This is an example of how to create an SqlSession using MyBatis framework. MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. It can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records. In order to create a MyBatis SqlSession you can follow the steps below as described in the example:
- Create a class,
Employee.java
with variables and their getters and setters.
package com.javacodegeeks.snippets.enterprise; import java.util.Date; public class Employee { private Long id; private String name; private String surname; private String title; private Date created; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } }
- Define the configuration file. Here, the
mybatis.conf.xml
file contains settings for the core of the MyBatis system, including a DataSource for acquiring database Connection instances, as well as a TransactionManager for determining how transactions should be scoped and controlled. The body of theenvironment
element contains the environment configuration for transaction management and connection pooling. ThedataSource
element configures the source of JDBC Connection objects using the standard JDBC DataSource interface. Themappers
element contains the EmployeeMapper.xml that contains the mapping definition.
mybatis.conf.xml<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <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/companydb" /> <property name="username" value="jcg" /> <property name="password" value="jcg" /> </dataSource> </environment> </environments> <mappers> <mapper resource="EmployeeMapper.xml" /> </mappers> </configuration>
- Define the mapper. The
EmployeeMapper.java
class is defined inEmployeeMapper.xml
. The SQL query is writen here and mapped to an object.
EmployeeMapper.xml<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.javacodegeeks.snippets.enterprise.EmployeeMapper"> <select id="selectEmployee" parameterType="int" resultType="com.javacodegeeks.snippets.enterprise.Employee"> select * from Employee where id = #{id} </select> </mapper>
- Create a main application. Get the
mybatis.conf.xml
file as a Reader object, using thegetResourceAsReader(java.lang.String resource)
API method oforg.apache.ibatis.io.Resources
. - Create a new
org.apache.ibatis.session.SqlSessionFactoryBuilder
and use itsbuild(Reader reader)
API method to create aorg.apache.ibatis.session.SqlSessionFactory
, and with itsopenSession()
API method open a neworg.apache.ibatis.session.SqlSession
. - Use the
selectOne(String statement, Object parameter)
API method of SqlSession to get theEmployee
and invoke its method to get the result.package com.javacodegeeks.snippets.enterprise; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class CreateMyBatisSqlSession { private static final String conf = "mybatis.conf.xml"; public static void main(String[] args) throws Exception { Reader reader = Resources.getResourceAsReader(conf); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory sessionFactory = builder.build(reader); SqlSession session = sessionFactory.openSession(); Employee employee = (Employee) session.selectOne("selectEmployee", 1); System.out.println(employee.getId() + " - " + employee.getName() + " - " + employee.getSurname()); } }
Output:
1 - Jack - Thomson
This was an example of how to create a MyBatis SqlSession in Java.