作业7,8

直接上代码:

接口:

/**
* 通过id查询信息
* @param map
* @return
*/
Goods getGoodById(Map<String ,String > map);

xml:

    <select id="getGoodById" resultType="com.pojo.Goods">

        SELECT * FROM goods where
        <choose>
            <when test="userId!=null and userId != ''">
                 goods_id = #{userId};
            </when>
            <otherwise>
这里查询相当于switch里的default
goods_id = 2; </otherwise> </choose> </select>

测试类:

test
 @Test
    public void test06(){
        SqlSession sqlSession = MyBatisUtil.createSqlSession();

        DemoMapper mapper = sqlSession.getMapper(DemoMapper.class);

       Map<String ,String > map = new HashMap<String, String>();
       map.put("userId","");
        Goods goods = mapper.getGoodById(map);


        System.out.println(goods.getGoods_code()+" "+goods.getGoods_name());

        MyBatisUtil.closeSqlSession(sqlSession);
    }

结果:

8:

直接上代码:

接口:

/**
     * 分页查询
     * @param map
     * @return
     */
List<Goods> getGoodsByLimit(Map<String ,Integer > map);
 

xml:

  <select id="getGoodsByLimit" resultType="com.pojo.Goods">

SELECT * FROM goods LIMIT #{parNo},#{parSize}

    </select>

测试类:

猜你喜欢

转载自www.cnblogs.com/bichen-01/p/11733382.html