• sql 传参


    传递一位参数

            <!-- 传参一位数 -->
            <!--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了

  • 相关阅读:
    vue + ajax + php 接口的使用小接
    网页调用qq聊天
    基于touch.js 左滑删除功能
    touch.js——常见应用操作
    常用链接
    如何判断滚动条已到达底部
    前端如何优雅的选择字体
    纯css3打造瀑布流布局
    移动端软键盘监听(弹出,收起),及影响定位布局的问题
    jq获取图片的原始尺寸,自适应布局
  • 原文地址:https://www.cnblogs.com/wuheng-123/p/13828776.html
Copyright © 2020-2023  润新知