作业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>

 

测试类:

 

 

 

 

posted @ 2019-10-24 17:06  AxeBurner  阅读(129)  评论(0编辑  收藏  举报