传递一位参数
<!-- 传参一位数 --> <!--parameterType 外部传参一个整数 --> <!--resultType 结果的类型 --> <select id="findOneById" parameterType="integer" resultType="com.imooc.mybatis.entity.GoodsEntity"> SELECT * FROM t_goods WHERE goods_id = #{value} </select> public void testFindOne() throws Exception { SqlSession sqlSession = null; try { //获取sql对象 sqlSession = MybatisUtils.openSession(); //执行sql GoodsEntity good = sqlSession.selectOne("goods.findOneById",739); System.out.println(good.getTilte()); //查看连接状态 Connection conn = MybatisUtils.getConnection(sqlSession); }catch (Exception e){ throw e; }finally { MybatisUtils.release(sqlSession); } }
传参多位参数
<!--传递多位参数 --> <!--parameterType 传递多位参数--> <select id="findMoreParam" parameterType="java.util.Map" resultType="com.imooc.mybatis.entity.GoodsEntity"> SELECT * FROM t_goods WHERE current_price BETWEEN #{min} AND #{max} ORDER BY current_price DESC limit 0,#{limit} </select> public void testMoreParam() throws Exception { SqlSession sqlSession = null; try { //获取sql对象 sqlSession = MybatisUtils.openSession(); //执行sql Map para = new HashMap(); para.put("min",100); para.put("max",200); para.put("limit",5); List<GoodsEntity> list = sqlSession.selectList("goods.findMoreParam",para); for (GoodsEntity good:list ) { System.out.println(good.getTilte()+"---"+good.getCurrentPrice()); } //查看连接状态 Connection conn = MybatisUtils.getConnection(sqlSession); }catch (Exception e){ throw e; }finally { MybatisUtils.release(sqlSession); } }
Mybatis中parameterType和parameterMap的区别
parameterMap和resultMap类似,parameterMap通常应用于mapper中有多个参数要传进来时,表示将查询结果集中列值的类型一一映射到java对象属性的类型上,在开发过程中不推荐这种方式。
一般使用parameterType直接将查询结果列值类型自动对应到java对象属性类型上,不再配置映射关系一一对应。
MyBatis报错笔记——Could not find parameter map java.util.List
解决办法就是将写错的parameterMap改为parameterType就ok了