Skip to content

Support child nodes on bind element #1053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kazuki43zoo
Copy link
Member

@kazuki43zoo kazuki43zoo commented Jul 13, 2017

I want to support child nodes(such as <if>, <foreach>, etc ...) on <bind> element.

By this changes, we can write sql map as follow:

<?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.example.domain.repository.order.OrderRepository">

    <!-- ... -->

    <sql id="selectFromJoin">
        SELECT
            o.id,
            o.status_code,
            os.name AS status_name,
            oi.quantity,
            i.code AS item_code,
            i.name AS item_name,
            i.price AS item_price,
            ct.code AS category_code,
            ct.name AS category_name,
            cp.code AS coupon_code,
            cp.name AS coupon_name,
            cp.price AS coupon_price
        FROM
            ${orderTable} o
        INNER JOIN c_order_status os ON os.code = o.status_code
        INNER JOIN t_order_item oi ON oi.order_id = o.id
        INNER JOIN m_item i ON i.code = oi.item_code
        INNER JOIN m_item_category ic ON ic.item_code = i.code
        INNER JOIN m_category ct ON ct.code = ic.category_code
        LEFT JOIN t_order_coupon oc ON oc.order_id = o.id
        LEFT JOIN m_coupon cp ON cp.code = oc.coupon_code
    </sql>

    <select id="findOne" resultMap="orderResultMap">
        <bind name="orderTable" value="'t_order'" />
        <include refid="selectFromJoin"/>
        WHERE
            o.id = #{id}
        ORDER BY
            item_code ASC,
            category_code ASC,
            coupon_code ASC
    </select>

    <select id="findPage" resultMap="orderResultMap">
        <bind name="orderTable">
            (
            SELECT
                id,
                status_code
            FROM
                t_order
            <where>
                <if test="status != null">
                    status_code = #{status}
                </if>
            </where>
            ORDER BY
                id DESC
            LIMIT #{pageable.pageSize}
            OFFSET #{pageable.offset}
            )
        </bind>
        <include refid="selectFromJoin"/>
        ORDER BY
            id DESC,
            item_code ASC,
            category_code ASC,
            coupon_code ASC
    </select>

    <!-- ... -->

</mapper>

@harawata @emacarron @hazendaz @jeffgbutler
What do you think this idea ?
If you agree with my idea, please review the this PR !!

Thanks.

@kazuki43zoo kazuki43zoo added the enhancement Improve a feature or add a new feature label Jul 14, 2017
@kazuki43zoo kazuki43zoo self-assigned this Jul 14, 2017
@harawata
Copy link
Member

I think the same can be achieved using <include />.

<sql id="selectFromJoin">
  SELECT
    o.id,
    o.status_code,
    os.name AS status_name,
    oi.quantity,
    i.code AS item_code,
    i.name AS item_name,
    i.price AS item_price,
    ct.code AS category_code,
    ct.name AS category_name,
    cp.code AS coupon_code,
    cp.name AS coupon_name,
    cp.price AS coupon_price
  FROM
    <include refid="${orderTable}" />
    o
  INNER JOIN c_order_status os ON os.code = o.status_code
  INNER JOIN t_order_item oi ON oi.order_id = o.id
  INNER JOIN m_item i ON i.code = oi.item_code
  INNER JOIN m_item_category ic ON ic.item_code = i.code
  INNER JOIN m_category ct ON ct.code = ic.category_code
  LEFT JOIN t_order_coupon oc ON oc.order_id = o.id
  LEFT JOIN m_coupon cp ON cp.code = oc.coupon_code
</sql>

<sql id="orderTablePlain">
  t_order
</sql>

<select id="findOne" resultMap="orderResultMap">
  <include refid="selectFromJoin">
    <property name="orderTable" value="orderTablePlain" />
  </include>
  WHERE
      o.id = #{id}
  ORDER BY
      item_code ASC,
      category_code ASC,
      coupon_code ASC
</select>

<sql id="orderTablePageable">
(
  SELECT
      id,
      status_code
  FROM
      t_order
  <where>
      <if test="status != null">
          status_code = #{status}
      </if>
  </where>
  ORDER BY
      id DESC
  LIMIT #{pageable.pageSize}
  OFFSET #{pageable.offset}
)
</sql>

<select id="findPage" resultMap="orderResultMap">
  <include refid="selectFromJoin">
    <property name="orderTable" value="orderTablePageable" />
  </include>
  ORDER BY
    id DESC,
    item_code ASC,
    category_code ASC,
    coupon_code ASC
</select>

Sorry if I am missing something!

@kazuki43zoo
Copy link
Member Author

@harawata Thanks for your comment.

Your suggestion looks good !!
But i think the dynamic include reference(<include refid="${...}" />) is little complex(difficult) than using bind element for developer that not familiar with MyBatis. (I think using bind element is more intuitive)

WDYT ?

@harawata
Copy link
Member

Um...I'm not sure.
@mnesarco @emacarron Could you guys take a look when you have time, please?

@guoweixin
Copy link

看到了,只是看一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve a feature or add a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants