We just need to include the mybatis-x.x.x.jar file in the classpath to use MyBatis.

We can download it at "https://github.com/mybatis/migrations/releases".

If we will use Maven, we just add the following dependency to our pom.xml


==== pom.xml ====

<dependency>

  <groupId>org.mybatis</groupId>

  <artifactId>mybatis</artifactId>

  <version>x.x.x</version>

</dependency>

: we can find the above source code at "http://mvnrepository.com/".


1. Setting the configuration XML file

The configuration XML file contains settings for the core of the MyBatis system, including a DataSouce for acquiring database connection instances. 

Here is simple example.


==== mybatis.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>

    <!-- Elements should be coded keeping order. -->

   

    <!-- including a property's file -->

    <properties resource="mybatis.properties" />


    <typeAliases>

        <typeAlias alias="GoodsCmnDto" type="com.world.test.dto.GoodsCmnDto"/>

    </typeAliases>

    

    

    <!-- the setting of Database Connection -->

    <environments default="development">

        <environment id="development">

            <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}" />


            </dataSource>

        </environment>

    </environments>

   

    <!-- sql Mappers -->

    <mappers>

        <mapper resource="com/mobile/test/service/sqlmapper/GoodsCmnSql.xml" />

    </mappers>

 

</configuration>



2. Acquiring a SqlSession from SqlSessionFactory

We should define a Class, SqlMapClient to acquire a sql session from SqlSessionFactory like the following.


==== SqlMapClient.java ====

package com.mobile.mybatis;


import java.io.IOException;

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 SqlMapClient {

private static SqlSession session;

    

    public static SqlSession getSqlSession() {

    try {

             String resource = "mybatis.xml";

             Reader reader = Resources.getResourceAsReader(resource);

             SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);

              

             session = sqlMapper.openSession();

              

         } catch (IOException e) {

             e.printStackTrace();

         }

     

        return session;

    }

}



3. Mapping sql statements.

==== sampleSql.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="Test">

 

     <select id="getTeam" resultType="hashmap">

             SELECT

  

BD_CD AS CODE,

    BD_NM AS NAME

    FROM

 ESTB102

    WHERE

 CAMP_FG = '1'

          ORDER BY BD_NM

     </select>

 </mapper>

: Actually, it's very simple. We just define an id attribute and resultType.



4. Calling the method against the Mapper interface

==== SampleController.java ====

package com.mobile.test.process;


import java.util.List;

import java.util.Map;


import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.apache.ibatis.session.SqlSession;


import com.mobile.log.Log;

import com.mobile.log.LogFactory;

import com.mobile.orm.GlobalSqlClientAssists;

import com.mobile.orm.SqlClientAssists;

import com.mobile.web.DataView;

import com.mobile.web.servlet.util.RequestData;

import com.mobile.mybatis.SqlMapClient;


public class MyBatisTestProcessor {

Log logger = LogFactory.getLog(MyBatisTestProcessor.class);

SqlSession session;

public MyBatisTestProcessor()

{

session = SqlMapClient.getSqlSession();

}

public DataView teamList(HttpServletRequest request, HttpServletResponse response,

DataView dataView, SqlClientAssists sqlClientAssists, GlobalSqlClientAssists globalSqlClientAssists,

RequestData reqData) throws Exception 

{

try{

       List<Map> list = session.selectList("Test.getTeam");

         

       for(Map map : list)

       {

        logger.info("###### " + map.get("CODE") + " ######");

        logger.info("###### " + map.get("NAME") + " ######");

       }

}finally{

session.close();

}

return dataView;

}

}


: "Test" means the namespace and "getTeam" means an id for the mapped statement, defining in the above sampleSql.xml.

We should specify the fully qualified name of "Test.getTeam".




+ Recent posts

출처: http://large.tistory.com/23 [Large]