intra-mart Accel Platform / TERASOLUNA Global Framework Programming Guide

Version 2 2014-01-01

Contents

Programming Method using MyBatis

Summary

In this section one example of programming method using MyBatis2.3 is shown.

About MyBatis

MyBatis is an O/R mapping framework which was developed on the concept of SQL and object mapping
instead of the record and object mapping managed by the database.
It will be useful when the complex combination conditions or precise query tuning are required.
Please see below for the details about MyBatis.

Note

MyBatisは2.x系と3.x系で実装が異なります。
TERASOLUNA Global Framework では、MyBatis2.3系を利用して実装します。

Programming Method

SQLMap Creation

  • When the functions of MyBatis are used, it is necessary to create the SQL map that defines the query to be executed.
    As an example, the xml file below is created and is placed under META-INF/mybatis/sql.
    Here, please set the file nae to [*-sqlmap.xml].
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sqlMap
                PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
                "http://ibatis.apache.org/dtd/sql-map-2.dtd">
    
    <sqlMap namespace="mycompany">  <!-- ① -->
    
      <typeAlias alias="MyCompany" type="my.terasoluna.app.domain.model.MyCompany"/>  <!-- ② -->
      
      <resultMap id="resultMapMyCompnay" class="MyCompany">  <!-- ③ -->
        <result property="companyId" column="company_id" />
        <result property="name"      column="name" />
      </resultMap>
      
      <insert id="insert" parameterClass="MyCompany">  <!-- ④ -->
        INSERT INTO my_company (company_id, name) 
        VALUES(#companyId#, #name#)
      </insert>
      
      <update id="update" parameterClass="MyCompany">  <!-- ⑤ -->
        UPDATE my_company SET 
        name = #name#
        WHERE company_id   = #companyId#
      </update>
      
      <delete id="delete" parameterClass="java.lang.String">  <!-- ⑥ -->
        DELETE FROM my_company WHERE company_id = #value#
      </delete>
      
      <select id="selectOne" parameterClass="java.lang.String" resultMap="resultMapMyCompnay">  <!-- ⑦ -->
        SELECT * FROM my_company WHERE company_id = #value#
      </select>
      
      <select id="selectAll" resultMap="resultMapMyCompnay">  <!-- ⑦ -->
        SELECT * FROM my_company
      </select>
    
      <select id="selectLessId" parameterClass="java.lang.String" resultMap="resultMapMyCompnay">  <!-- ⑧ -->
      <![CDATA[ 
        SELECT * FROM my_company WHERE company_id <  #value#  
      ]]>
      </select>
    </sqlMap>
    
    1. <sqlMap> tag is defined. Add arbitrary name of SQL map to the namespace attribute.
      Please try to put the name that is unique in the total application program.
    2. <typeAlias> tag is placed optionally.
      You can now use the class path specified in the type attribute by the omitted name specified in the alias attribute.
    3. <resultMap> tag is placed optionally.
      It will be used when the remapping is needed because of the difference in field name and column name of entity.
    4. In case INSERT statement is defined, use the <insert> tag.
      Here, in order to embed the field value of entity to the query,
      “MyCompany”(my.terasoluna.app.domain.model.MyCompany class) is used
      as a parameterClass attribute.
      If the reference to parameter is required, you should specify the field name by enclosing with #.
    5. In case UPDATE statement is defined, use the <update> tag.
    6. In case DELETE statement is defined, use the <delete> tag.
      Since the character string is used as a condition of the main key,
      String is used as parameterClass, and
      #value# is used as a reference to variables.
    7. In case SELECT statement is defined, <select> tag should be used.+
      Search result will sepcify the ID of resultMap defined in ③,
      and will be returned by remapping the value.
    8. If the condition statement includes an inequality sign and needs an escape,
      query or the corresponding part should be enclosed by <![CDATA[ ]]>.

    Note

    Various other tags or functions are available.
    Please refer to the Reference for detail.

Repository Class Creation

  • For any given entity, repository class is implemented as follows.
    package my.terasoluna.app.domain.repository;
    
    import java.util.List;
    
    import jp.terasoluna.fw.dao.QueryDAO;
    import jp.terasoluna.fw.dao.UpdateDAO;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    
    import my.terasoluna.app.domain.model.MyCompany;
    
    @Repository
    public class MyCompanyRepositoryMyBatisImpl implements MyCompanyRepositoryMyBatis {
        
        @Autowired
        UpdateDAO updateDAO; // ①
        
        @Autowired
        QueryDAO queryDAO;   // ②
    
        @Override
        public void insert(MyCompany entity) {
            updateDAO.execute("mycompany.insert", entity); // ③
        }
    
        @Override
        public void update(MyCompany entity) {
            updateDAO.execute("mycompany.update", entity); // ③
        }
    
        @Override
        public void delete(String companyId) {
            updateDAO.execute("mycompany.delete", companyId); // ③
        }
        
        @Override
        public MyCompany selectOne(String companyId) {
            return queryDAO.executeForObject("mycompany.selectOne", companyId, MyCompany.class); // ④
        }
        
        @Override
        public List<MyCompany> selectAll(int offset, int pageSize) {
            return queryDAO.executeForObjectList("mycompany.selectAll", null, offset, pageSize); // ⑤
        }
    
        @Override
        public List<MyCompany> selectLessId(String companyId) {
            return queryDAO.executeForObjectList("mycompany.selectLessId", companyId);
        }
    }
    
    1. In case update-type query is used, UpdateDao should be defined.
    2. In case selection-type query is used, QueryDao is defined.
    3. In case update-type query is executed, UpdateDao#execute is executed.
      Query to be used is selected as the first argument.
      Query name should be specified as “namespace attribute of SQL map.Quesry id”.
      Parameter class object requested by the query should bes et to the second argument.
    4. In case a single record is searched, QueryDao#executeForObject is executed.
      Query name is specified to the first argument, condition to the second argument, and entity class of search result to the third argument.
    5. In case multiple records are searched, QueryDao#executeForList is executed.
      In case query name is fetched from the first argument and condition is fetched from the second argument,
      offset should be set to the third argument, and number of items obatined will be set to the fourth argument.

Shared Database Usage

Please remove the comment-out of sharedDataSource
of applicationContext-im_tgfw_common.xml.
In this case connection ID for the target shared database is specified to connectId.
<bean id="dataSource"   class="jp.co.intra_mart.framework.extension.spring.datasource.TenantDataSource" />

<bean id="sharedDataSource" class="jp.co.intra_mart.framework.extension.spring.datasource.SharedDataSource">
  <constructor-arg name="connectId" value="sharedA" />
</bean>
In the applicationContext-im_tgfw_mybatis.xml,
please remove the comment-outs at the following locations.
<bean id="sharedSqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" scope="singleton">
  <property name="configLocations" value="classpath*:/META-INF/mybatis/config/*sqlMapConfig.xml" />
  <property name="mappingLocations" value="classpath*:/META-INF/mybatis/sql/**/*-sqlmap.xml" />
  <property name="dataSource" ref="sharedDataSource" />
</bean>

<bean id="sharedQueryDAO"
  class="jp.terasoluna.fw.dao.ibatis.QueryDAOiBatisImpl">
  <property name="sqlMapClient" ref="sharedSqlMapClient" />
</bean>

<bean id="sharedUpdateDAO"
  class="jp.terasoluna.fw.dao.ibatis.UpdateDAOiBatisImpl">
  <property name="sqlMapClient" ref="sharedSqlMapClient" />
</bean>

<bean id="sharedSpDAO"
  class="jp.terasoluna.fw.dao.ibatis.StoredProcedureDAOiBatisImpl">
  <property name="sqlMapClient" ref="sharedSqlMapClient" />
</bean>

<bean id="sharedQueryRowHandleDAO"
  class="jp.terasoluna.fw.dao.ibatis.QueryRowHandleDAOiBatisImpl">
  <property name="sqlMapClient" ref="sharedSqlMapClient" />
</bean>
In the repository implementation class, please add @Qualifier annotation to
every field definition location of TERASOLUNA DAO, and
explicitly specify bean Id for each database to be used.
@Autowired
@Qualifier("sharedQueryDAO")
QueryDAO queryDAO;

Note

If you select TERASOLUNA Global Framework(...) Setting File
from [Select Additional Resource] of Juggling project,
setting file which includes the above file is placed on the Juggling project.
In a team development environment, placing the edit file shown above on the module project
might affect other module projects.
Editing of the setting file should be made in advance on the Juggling project.

Contents