MyBatis “include refid” and “sql” tags

MyBatis “include refid” and “sql” tags

include refid

The following statements can be made in MyBatis.

<include refid="from" />

Here we use the key from, but it can be anything. This key will include SQL written elsewhere.

<sql></sql>タグ

The way to describe it elsewhere is to use as follows. Place a reusable SQL statement with this tag and reuse it.

<sql id="from">
    FROM SCHEMA.TBL A 
</sql>

Below is an example description.

<select id="select_data"> 
    SELECT A.COLUMN
    <include refid="from" />
    <include refid="where" />
</select>

The namespace attribute is assumed to be specified in the mapper XML file, so the refid should be “namespece.refid”.

<?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="jp.co.confrage.repository.EmployeeMapper" >
  <sql id="colomun_list">
  id,
  name,
  age
  </sql>

  <select id="select_data" >
    select 
      <include refid="jp.co.confrage.repository.EmployeeMapper.column_list" />
    from employee
  </select>
</mapper>

コメント

Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL