Mybatis中Map的用法
2020-08-05 本文已影响0人
c_gentle
一、多个参数可以使用Map进行传参
xml文件SQL语句
<select id="findByPriceRange" parameterType="java.util.Map" resultType="com.itlaoqi.mybatis.entity.Goods">
select * from t_goods
where
current_price between #{min} and #{max}
order by current_price
limit 0,#{limt}
</select>
二、接受多个参数的时候使用Map方法
一般进行单表查询的时候,返回接受参数使用实体类就可以了,但是当设计到多表查询时,我们可以使用Map进行多参数的接受
xml文件的sql语句:我们需要返回结果有序的话,可以使用LinkedHashMap
<select id="findGoodsMap" resultType="java.util.LinkedHashMap">
select g.* , c.category_name from t_goods g , t_category c where g.category_id = c.category_id
</select>
Java代码:
public void testFindGoodsMap(){
SqlSession session = null;
//openSession创建一个新的SqlSession对象,SqlSession提供了增删改查的方法调用
try {
session = sqlSessionFactory.openSession();
List<Map> list = session.selectList("goods.findGoodsMap");
for (Map goods : list) {
System.out.println(goods.get("title") + "-" + goods.get("current_price"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (session != null) {
//将Connection归还到连接池供其他Session重用
session.close();
}
}
}