엄지월드

mybatis mapper xml include 본문

java/Spring

mybatis mapper xml include

킨글 2022. 8. 14. 15:51

1. 아래와 같이 sql id로 선언해준다.

<sql id="reUseSql">

...

...

</sql>

2. sql을 사용할 곳에서 선언해준다.

<include refid="reUseSql" />

 

3. 전체 코드 

<?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.myapp.lms.course.mapper.CourseMapper">
    <sql id="selectListWhere">
        <if test="searchType != null and searchValue != null">
            <choose>                
                <when test="searchType == 'phone'">
                    and phone like concat('%', #{searchValue}, '%')
                </when>
                <otherwise>
                    and
                    (
                    user_Id like concat('%', #{searchValue}, '%')
                    or
                    user_Name like concat('%', #{searchValue}, '%')
                    or
                    phone like concat('%', #{searchValue}, '%')
                    )
                </otherwise>
            </choose>
        </if>
    </sql>
    
    <select id="selectListCount"
                parameterType="com.myapp.lms.course.model.CourseParam"
                resultType="long">
            select count(*)
            from member
            where 1 = 1        
            <include refid="selectListWhere" />        
    </select>

</mapper>
Comments