0
点赞
收藏
分享

微信扫一扫

3.数据传参


3.1单个数据类型传参

mapper.xml文件中sql语句传入参数方式为#{参数},当传入的参数为一个数据时,参数的名字任意但一般用接口中方法的传入参数。

3.2实体类对象传值

mapper接口方法:

int insertEmployee(Employee employee);

mapper.xml映射文件:

<insert id="insertEmployee">
  insert into t_emp(emp_name,emp_salary) values(#{empName},#{empSalary})
</insert>

映射文件中传值为实体类的属性名

3.3多个数据类型传参

当需要传入的参数为多个时,就不能随意起名,有以下两种方案:

(1)在mapper文件的方法中,每个需要传入的参数前加上@param注解,指定传入参数的名称.

(2)在xml映射文件中以arg0,arg1,arg2......方式传参,此方式为mybatis的默认传参方式.

推荐使用第一种方案

3.4Map集合数据

int updateEmployeeByMap(Map<String, Object> paramMap);

<update id="updateEmployeeByMap">

  update t_emp set emp_salary=#{empSalaryKey} where emp_id=#{empIdKey}

</update>

Map<String, Object> paramMap = new HashMap<>();
  paramMap.put("empSalaryKey", 999.99);
  paramMap.put("empIdKey", 5);
  int result = mapper.updateEmployeeByMap(paramMap);


举报

相关推荐

0 条评论