1. 程式人生 > >ibatis常用標籤測試

ibatis常用標籤測試

ibatis動態sql常用標籤

 <!-- 一元條件查詢 -->
    <select id="queryGroupOne" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
        SELECT group_id, group_name, group_numberForPeoples, create_year
        FROM development_application_group
        <!-- 如果dynamic下子標籤都是false,都沒能生效,則prepend中的值也無效了,
              如果有為true的子標籤,則會自動去除第一個標籤中prepend的值 -->
        <dynamic prepend="where">

            <!-- 即使是Null或者"",也依然生效 -->
            <isPropertyAvailable prepend="and" property="group_id">
                group_id = #group_id#
            </isPropertyAvailable>

            <!-- 沒有這個引數,則查詢條件有效 -->
            <isNotPropertyAvailable prepend="and" property="group_id">
                group_id is not null
            </isNotPropertyAvailable>

            <!-- 不是Null,也不是"",才生效 -->
            <isNotEmpty prepend="and" property="group_name">
                group_name = #group_name#
            </isNotEmpty>

            <!-- 是null或者"",才生效 -->
            <isEmpty prepend="and" property="group_name">
                group_name is not null
            </isEmpty>

            <!-- 不是Null,就可以生效 -->
            <isNotNull prepend="and" property="group_numberForPeoples">
                group_numberForPeoples = #group_numberForPeoples#
            </isNotNull>

            <!-- 是Null,才生效 -->
            <isNull prepend="and" property="group_numberForPeoples">
                group_numberForPeoples is not null
            </isNull>

        </dynamic>
    </select>

    <!-- 二元條件查詢 -->
    <select id="queryGroupTwo" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
        SELECT group_id, group_name, group_numberForPeoples, create_year
        FROM development_application_group
        <dynamic prepend="where">
            <!-- 引數必須大於10,才生效 -->
            <!-- 類似的標籤有
                    isEqual(比較屬性值和靜態值或另一個屬性值是否相等)
                    isNotEqual(較屬性值和靜態值或另一個屬性值是否不相等)
                    isGreaterThan(比較屬性值是否大於靜態值或另一個屬性值)
                    isGreaterEqual( 比較屬性值是否大於等於靜態值或另一個屬性值)
                    isLessThan(比較屬性值是否小於靜態值或另一個屬性值)
                    isLessEqual(比較屬性值是否小於等於靜態值或另一個屬性值)
                    -->
            <isGreaterThan prepend="and" property="number" compareValue="10">
                group_numberForPeoples is not null
            </isGreaterThan>

        </dynamic>
    </select>

    <!-- 遍歷 String [],不需要寫parameterClass -->
    <select id="queryGroupByStrings" resultClass="java.util.HashMap">
        SELECT group_id, group_name, group_numberForPeoples, create_year
        FROM development_application_group
        <dynamic prepend="where">
            group_id in
            <iterate close=")" open="("  conjunction=",">
            <![CDATA[
                 #[]#
            ]]>
            </iterate>
        </dynamic>
    </select>

    <!-- 遍歷 List<泛型引數是八大基本型別>,這裡的groups_ids不是非要和我們傳過來的List變數成一樣-->
    <select id="queryGroupByListString" parameterClass="java.util.List" resultClass="java.util.HashMap">
        SELECT group_id, group_name, group_numberForPeoples, create_year
        FROM development_application_group
        <dynamic prepend="where">
            group_id in
            <iterate open="(" conjunction="," close=")">
                #group_ids[]#
            </iterate>
        </dynamic>
    </select>

    <!-- 遍歷 List<物件>,獲取物件的一個屬性來操作 -->
    <select id="queryGroupByListObjectId" parameterClass="java.util.List" resultClass="java.util.HashMap">
        SELECT group_id, group_name, group_numberForPeoples, create_year
        FROM development_application_group
        <dynamic prepend="where">
            group_id in
            <iterate prepend="and" open="(" conjunction="," close=")">
                #list[].group_id#
            </iterate>
        </dynamic>
    </select>

    <!-- 遍歷 Map裡的一個key對應的value,實際是個List<String> -->
    <select id="queryGroupByMapListString" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
        SELECT group_id, group_name, group_numberForPeoples, create_year
        FROM development_application_group
        <dynamic prepend="where">
            <isNotNull prepend="and" property="group_ids">
                group_id in
                <iterate open="(" conjunction="," property="group_ids" close=")">
                    #group_ids[]#
                </iterate>
            </isNotNull>
        </dynamic>
    </select>

    <!-- 遍歷 Map裡的一個key對應的value,實際是個List<Object> -->
    <select id="queryGroupByMapListObject" parameterClass="java.util.HashMap" resultClass="java.util.HashMap">
        SELECT group_id, group_name, group_numberForPeoples, create_year
        FROM development_application_group
        <dynamic prepend="where">
            <isNotNull prepend="and" property="groups">
                group_id in
                <iterate open="(" conjunction="," property="groups" close=")">
                    #groups[].group_id#
                </iterate>
            </isNotNull>
        </dynamic>
    </select>

------------------------------------------------------------------------------------------------------------------

    /**
     * 測試一元條件標籤
     */
    @RequestMapping("ibatisLiveSqlController!oneRequireSql.do")
    public String oneRequireSql() throws Exception{
        Map map = new HashMap();
        List list = getDao().queryForList("liveSql.queryGroupOne", map);
        setList("dgd1", list);
        return JSON;
    }

    /**
     * 測試二元條件標籤中的isGreaterThan標籤
     */
    @RequestMapping("ibatisLiveSqlController!twoRequireSql.do")
    public String twoRequireSql() throws Exception{
        Map map = new HashMap();
        map.put("number", 8);
        List list = getDao().queryForList("liveSql.queryGroupTwo", map);
        setList("dgd1", list);
        return JSON;
    }

    /**
     * 測試五種不同類別的遍歷資料
     * 1.List<String>
     * 2.String[]
     * 3.List<Object>,遍歷Object物件的一個屬性
     * 4.Map裡的一個value是一個List<String>
     * 5.Map裡的一個value是一個List<Object>,遍歷Object物件的一個屬性
     */
    @RequestMapping("ibatisLiveSqlController!iterateSql.do")
    public String iterateSql() throws Exception{
//        List<String> group_ids = new ArrayList<String>();
//        group_ids.add("1");
//        group_ids.add("2");
//        List list = getDao().queryForList("liveSql.queryGroupByListString", group_ids);
//        setList("dgd1", list);

//        String [] group_ids = new String[]{"3","4"};
//        List list = getDao().queryForList("liveSql.queryGroupByStrings", group_ids);
//        setList("dgd1", list);

//        Group group1 = new Group();
//        group1.setGroup_id("1");
//        Group group4 = new Group();
//        group4.setGroup_id("4");
//        List<Group> groups = new ArrayList<Group>();
//        groups.add(group1);
//        groups.add(group4);
//        List list = getDao().queryForList("liveSql.queryGroupByListObjectId", groups);
//        setList("dgd1", list);

//        List<String> group_ids = new ArrayList<String>();
//        group_ids.add("1");
//        group_ids.add("3");
//        Map map = new HashMap();
//        map.put("group_ids", group_ids);
//        List list = getDao().queryForList("liveSql.queryGroupByMapListString", map);
//        setList("dgd1", list);

        Group group1 = new Group();
        group1.setGroup_id("1");
        Group group2 = new Group();
        group2.setGroup_id("2");
        Group group4 = new Group();
        group4.setGroup_id("4");
        List<Group> groups = new ArrayList<Group>();
        groups.add(group1);
        groups.add(group2);
        groups.add(group4);
        Map map = new HashMap();
        map.put("groups", groups);
        List list = getDao().queryForList("liveSql.queryGroupByMapListObject", map);
        setList("dgd1", list);

        return JSON;
    }