Mybatis学习第4节 -- 多参数传递

第一种方法使用索引

一般不使用,不记录

第二种方法使用注解

接口
List<Shop> getShopListByPageAno(@Param(value = "offset") int offset, @Param(value = "pagesize") int pagesize);
mapper
<select id="getShopListByPageAno"  resultMap="simpleResultMap" >
select * from tb_shop ORDER BY CONVERT(shop_name USING gbk) limit #{offset}, #{pagesize}
</select>
用例
@Test
public void testGetShopListByPageAno() {
String template = "查询结果: %s";
SqlSession session = MyBatisUtil.getSqlSession();
ShopMapper mapper = session.getMapper(ShopMapper.class);
System.out.printf(template, mapper.getShopListByPageAno(2, 1));
session.close();
}

第三种方法, 通过Map来传递

接口
List<Shop> getShopListByPageMap(Map<String, Object> map);
mapper
<select id="getShopListByPageMap"  resultMap="simpleResultMap" >
select * from tb_shop ORDER BY CONVERT(shop_name USING gbk) limit #{offset}, #{pagesize}
</select>
用例
@Test
public void testGetShopListByPage() {
String template = "查询结果: %s";
SqlSession session = MyBatisUtil.getSqlSession();
ShopMapper mapper = session.getMapper(ShopMapper.class);

Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("offset", 2);
paramMap.put("pagesize", 2);

System.out.printf(template, mapper.getShopListByPageMap(paramMap));
session.close();
}

猜你喜欢

转载自www.cnblogs.com/litran/p/10543835.html