十五、持久層框架設(shè)計(jì)實(shí)現(xiàn)及MyBatis源碼分析-MyBatis基礎(chǔ)回顧及高級應(yīng)用-MyBatis的動態(tài)SQL-foreach標(biāo)簽的使用和SQL片斷抽取(九)

一、動態(tài) SQL 之foreach

循環(huán)執(zhí)?sql的拼接操作,例如:SELECT * FROM USER WHERE id IN (1,2,5)

在IUserProxyDao接口中添加一個(gè)findUserByIds方法

 /**
     * 用數(shù)組進(jìn)行查詢,用來演示foreach標(biāo)簽
     * @param ids
     * @return
     */
    List<User> findUserByIds(int[] ids);

在UserProxyMapper.xml文件中添加以下代碼片斷

<select id="findUserByIds" parameterType="list" resultType="user">
        SELECT * FROM user
        <where>
            <foreach collection="array" open="id in (" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </where>
    </select>

編寫測試方法,在MyBatisProxyTest測試類中添加如下代碼片斷進(jìn)行單元測試

@Test
    public void testFindByIds() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IUserProxyDao mapper = sqlSession.getMapper(IUserProxyDao.class);
        int[] ids = new int[]{1,2,6};
        List<User> all = mapper.findUserByIds(ids);
        for (User user : all) {
            System.out.println(user);
        }
    }

foreach標(biāo)簽的屬性含義如下:
標(biāo)簽?于遍歷集合,它的屬性:
? collection:代表要遍歷的集合元素,注意編寫時(shí)不要寫#{}
? open:代表語句的開始部分
? close:代表結(jié)束部分
? item:代表遍歷集合的每個(gè)元素,?成的變量名
? sperator:代表分隔符

二、SQL?段抽取

Sql 中可將重復(fù)的 sql 提取出來,使?時(shí)? include 引?即可,最終達(dá)到 sql 重?的?的

<?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="study.lagou.com.dao.IUserProxyDao">
    <sql id="selectUser">
        SELECT * FROM user
    </sql>
    
    <!--查詢方法-->
    <select id="findAll" resultType="study.lagou.com.pojo.User">
        <include refid="selectUser"/>
    </select>

    <select id="findByCondition" resultType="user" parameterType="user">
        <include refid="selectUser"/>
        <where>
            <if test="id != null">
                AND id = #{id}
            </if>
            <if test="username != null and username != ''">
                AND username = #{username}
            </if>
        </where>
    </select>

    <select id="findUserByIds" parameterType="list" resultType="user">
        <include refid="selectUser"/>
        <where>
            <foreach collection="array" open="id in (" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </where>
    </select>
</mapper>

上一篇筆記地址:http://www.itdecent.cn/p/26a4cbba4a98

下一篇筆記地址:http://www.itdecent.cn/p/200d11349f8d
具體代碼對應(yīng)下載地址:https://gitee.com/happymima/mybatis.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容