当前位置: 首页 > news >正文

Mybatis参数传递

  1. Map传参, #{}里的key要一一对应不能乱写,如果不存在则会填充NULL,不会报错

    Map<String, Object> map = new HashMap<>();
    // 让key的可读性增强
    map.put("carNum", "103");
    map.put("brand", "奔驰E300L");
    map.put("guidePrice", 50.3);
    map.put("produceTime", "2020-10-01");
    map.put("carType", "燃油车");
    
    <insert id="insertCar">
    insert into t_car(car_num,brand,guide_price,produce_time,car_type)
    values(#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
    </insert>
    
  2. 实体类传参,#{} 里写的是get方法的方法名去掉get之后将剩下的单词首字母变小写(例
    如:getAge对应的是#{age},如果这样的get方法不存在会报错

    Car car = new Car();
    car.setCarNum("103");
    car.setBrand("奔驰C200");
    car.setGuidePrice(33.23);
    car.setProduceTime("2020-10-11");
    car.setCarType("燃油车");
    
    <insert id="insertCarByPOJO">
    <!--#{} 里写的是POJO的属性名-->
    insert into t_car(car_num,brand,guide_price,produce_time,car_type) values
    (#{carNum},#{brand},#{guidePrice},#{produceTime},#{carType})
    </insert>
    
  3. 多参数传递

    List<Student> selectByNameAndSex(String name, Character sex);
    
    这个是会报错的,实际上在mybatis底层会创建一个map集合,以:#{arg0} #{arg1} #{param1} #{param2}为key
    <select id="selectByNameAndSex" resultType="student">
    select * from t_student where name = #{name} and sex = #{sex}
    </select>
    
    正确的写法
    <select id="selectByNameAndSex" resultType="student">
    <!--select * from t_student where name = #{arg0} and sex = #{arg1}-->
    <!--select * from t_student where name = #{param1} and sex = #{param2}-->
    select * from t_student where name = #{arg0} and sex = #{param2}
    </select>
    
    或者使用注解指定参数名称
    List<Student> selectByNameAndAge(@Param(value="name") String name, @Par
    am("age") int age);
    
  4. 如果参数只有一个的话,#{} 里面的内容就随便写了。对于 ${} 来说,注意加单引号。

http://www.lryc.cn/news/119310.html

相关文章:

  • 手动实现 Spring 底层机制 实现任务阶段一编写自己 Spring 容器-准备篇【2】
  • 部署模型并与 TVM 集成
  • Android Navigation 导航切换fragment用法
  • Anaconda Prompt使用pip安装PyQt5-tools后无法打开Spyder或闪退
  • 【jvm】jvm整体结构(hotspot)
  • 通达信波段选股公式,使用钱德动量摆动指标(CMO)
  • 家电维修小程序开发指南:从零搭建到上线
  • 玩赚音视频开发高阶技术——FFmpeg
  • python 变量赋值 修改之后 原值改变
  • 拂袖一挥,zipfile秒列zip包内容
  • InnoDB文件物理结构解析2 - FIL_PAGE_INDEX
  • XML-BEANS compiled schema: Could not locate compiled schema resource 异常处理
  • IOC容器 - Autofac
  • 用i18n 实现vue2+element UI的国际化多语言切换详细步骤及代码
  • Vue3 :Pinia入门
  • Java线程池的类型和使用
  • QT的信号槽的四种写法和五种链接方式
  • Vue+SpringBoot项目开发:后台登陆功能的实现(二)
  • arcgis pro 3.0.2 安装及 geemap
  • oracle插入多表(insert all/first)
  • 工业以太网交换机-SCALANCE X200 环网组态
  • 利用 Splashtop Enterprise 改善公司的网络安全
  • mqbroker.cmd闪退(mqnamesrv.cmd能正常启动)
  • LeetCode--HOT100题(26)
  • HTTP 请求方法详解
  • 孤立随机森林(Isolation Forest)(Python实现)
  • 小程序如何自定义分享内容
  • SpringBoot整合WebSocket详解
  • 伪原创神码ai怎么样【php源码】
  • Air001基于Keil环境开发,使用airisp串口命令行烧录