Mybatis : Implementation :Sample Project

Pre-Requisites: (Eclipse with Maven Plugin)

Steps:

1.Create Mybatis Enabled Project
2.Add Mybatis Maven Dependency
3.Create Mybatis Configuration
4.Create SQLSessionFactory
5.Create Sample Table in Database
6.Create Mapper XML
7.Create Results Object POJO
8.Create Mapper Interface
9.Create Service Class and Implement Mapper Interface
10.Create Junit Test case to Run Service Class

Step 1: Create MavenEnabled Project

CreateMavenProject

Step 2: Add Mybatis Depedency

 

CreateMavenProject

 

Step 3: 3.Create Mybatis Configuration

CreateMavenProject

Contents:

<?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”&gt;
<configuration>
<typeAliases>

<typeAlias alias=”DomainClassName” type=”com.company.project.mybatis.domain.DomainClassName” />
</typeAliases>
<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://DBIP:3306/DBName” />
<property name=”username” value=”username” />
<property name=”password” value=”password” />
</dataSource>
</environment>
</environments>
<mappers>

<mapper resource=”com/makemytrip/activiti/mybatis/mappers/DomainClassNameMapper.xml” />

</mappers>
</configuration>

Step 4:;Create SQLSessionFactory

package com.mybatis3.utils;

import java.io.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;

public class MyBatisSqlSessionFactory {
 private static SqlSessionFactory sqlSessionFactory;

public static SqlSessionFactory getSqlSessionFactory() {
 if (sqlSessionFactory == null) {
 InputStream inputStream;
 try {
 inputStream = Resources.getResourceAsStream("mybatis-config.xml");
 
 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
 } catch (IOException e) {
 e.printStackTrace();
 throw new RuntimeException(e.getCause());
  }
 }
 return sqlSessionFactory;
 }

public static SqlSession openSession() {
 try{
 
 }
 catch(Exception e)
 {
 e.printStackTrace();
 
 }
 return getSqlSessionFactory().openSession();
 }
}





5.Create Sample Table in Database

CREATE TABLE `SAMPLE_DEMO_TABLE` (

  `EMPLOYEE_ID` int(11) DEFAULT NULL,

  `EMPLOYEE_NAME` varchar(200) DEFAULT NULL,

  `JOINING_DATE` date DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

Step 6:.Create Mapper 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.company.domain.dao.TestMapper">
 <resultMap id="BaseResultMap" type="com.company.domain.model.Test">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 <result column="queries" jdbcType="VARCHAR" property="queries" />
 </resultMap>
 <sql id="Example_Where_Clause">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 <where>
 <foreach collection="oredCriteria" item="criteria" separator="or">
 <if test="criteria.valid">
 <trim prefix="(" prefixOverrides="and" suffix=")">
 <foreach collection="criteria.criteria" item="criterion">
 <choose>
 <when test="criterion.noValue">
 and ${criterion.condition}
 </when>
 <when test="criterion.singleValue">
 and ${criterion.condition} #{criterion.value}
 </when>
 <when test="criterion.betweenValue">
 and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
 </when>
 <when test="criterion.listValue">
 and ${criterion.condition}
 <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
 #{listItem}
 </foreach>
 </when>
 </choose>
 </foreach>
 </trim>
 </if>
 </foreach>
 </where>
 </sql>
 <sql id="Update_By_Example_Where_Clause">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 <where>
 <foreach collection="example.oredCriteria" item="criteria" separator="or">
 <if test="criteria.valid">
 <trim prefix="(" prefixOverrides="and" suffix=")">
 <foreach collection="criteria.criteria" item="criterion">
 <choose>
 <when test="criterion.noValue">
 and ${criterion.condition}
 </when>
 <when test="criterion.singleValue">
 and ${criterion.condition} #{criterion.value}
 </when>
 <when test="criterion.betweenValue">
 and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
 </when>
 <when test="criterion.listValue">
 and ${criterion.condition}
 <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
 #{listItem}
 </foreach>
 </when>
 </choose>
 </foreach>
 </trim>
 </if>
 </foreach>
 </where>
 </sql>
 <sql id="Base_Column_List">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 queries
 </sql>
 <select id="selectByExample" parameterType="com.makemytrip.mybatisApi.model.TestExample" resultMap="BaseResultMap">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 select
 <if test="distinct">
 distinct
 </if>
 <include refid="Base_Column_List" />
 from public.test
 <if test="_parameter != null">
 <include refid="Example_Where_Clause" />
 </if>
 <if test="orderByClause != null">
 order by ${orderByClause}
 </if>
 </select>
 <delete id="deleteByExample" parameterType="com.makemytrip.mybatisApi.model.TestExample">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 delete from public.test
 <if test="_parameter != null">
 <include refid="Example_Where_Clause" />
 </if>
 </delete>
 <insert id="insert" parameterType="com.makemytrip.mybatisApi.model.Test">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 insert into public.test (queries)
 values (#{queries,jdbcType=VARCHAR})
 </insert>
 <insert id="insertSelective" parameterType="com.makemytrip.mybatisApi.model.Test">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 insert into public.test
 <trim prefix="(" suffix=")" suffixOverrides=",">
 <if test="queries != null">
 queries,
 </if>
 </trim>
 <trim prefix="values (" suffix=")" suffixOverrides=",">
 <if test="queries != null">
 #{queries,jdbcType=VARCHAR},
 </if>
 </trim>
 </insert>
 <select id="countByExample" parameterType="com.makemytrip.mybatisApi.model.TestExample" resultType="java.lang.Integer">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 select count(*) from public.test
 <if test="_parameter != null">
 <include refid="Example_Where_Clause" />
 </if>
 </select>
 <update id="updateByExampleSelective" parameterType="map">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 update public.test
 <set>
 <if test="record.queries != null">
 queries = #{record.queries,jdbcType=VARCHAR},
 </if>
 </set>
 <if test="_parameter != null">
 <include refid="Update_By_Example_Where_Clause" />
 </if>
 </update>
 <update id="updateByExample" parameterType="map">
 <!--
 WARNING - @mbggenerated
 This element is automatically generated by MyBatis Generator, do not modify.
 This element was generated on Mon Aug 01 20:31:39 IST 2016.
 -->
 update public.test
 set queries = #{record.queries,jdbcType=VARCHAR}
 <if test="_parameter != null">
 <include refid="Update_By_Example_Where_Clause" />
 </if>
 </update>
</mapper>

Step 7:Create Results Object POJO

 

–> Create a POJO for Response

Step :8.Create Mapper Interface
Step :9.Create Service Class and Implement Mapper Interface
Step 10.Create Junit Test case to Run Service Class

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s