首页 > 数据库 >015.3MyBatis动态SQL(多条件复合查询)

015.3MyBatis动态SQL(多条件复合查询)

时间:2022-12-06 21:46:42浏览次数:45  
标签:Exception goods param 查询 015.3 session SQL MyBatis

1.goods.xml

<select id="dynamicSQL" parameterType="java.util.Map" resultType="com.imooc.mybatis.entity.Goods">
        select * from t_goods
        <where>
            <if test="categoryId != null">
                and category_id = #{categoryId}
            </if>
            <if test="currentPrice != null">
                and current_price &lt; #{currentPrice}
            </if>
        </where>
    </select>

2.测试用例

 /**
     * 动态SQL语句
     *
     * @throws Exception
     */
    @Test
    public void testDynamicSQL() throws Exception
    {
        SqlSession session = null;
        try
        {
            session = MyBatisUtils.openSession();
            Map param = new HashMap();
            param.put("categoryId", 44);
            param.put("currentPrice", 500);
            //查询条件
            List<Goods> list = session.selectList("goods.dynamicSQL", param);
            for (Goods g : list)
            {
                System.out.println(g.getTitle() + ":" + g.getCategoryId() + ":" + g.getCurrentPrice());

            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            MyBatisUtils.closeSession(session);
        }
    }

 

标签:Exception,goods,param,查询,015.3,session,SQL,MyBatis
From: https://www.cnblogs.com/LLL0617/p/16960637.html

相关文章