Mybatis的传参注意点(单参数、多参数、复杂参数) | 您所在的位置:网站首页 › mybatis传入单个参数 › Mybatis的传参注意点(单参数、多参数、复杂参数) |
Mybatis的传参注意点(单参数、多参数、复杂参数)
1. 传入参数为List2. 传入多个不同类型参数3. useGeneratedKeys和keyProperty的使用:
1. 传入参数为List
mapper接口: List getUserList(List list);mapper映射文件:List用foreach去遍历 如果查询的实体类的参数和数据库中的字段名不一致,要用resultMap进行映射 select * from user_info where id in ( #{item} )resultMap映射:column:数据库中的字段名,property:实体类中的属性名 javaType和jdbcType的对应表: mapper接口: void updateAlarmInfo(@Param("user") User user,@Param("id") Long id);mapper映射文件:传入多个参数时,parameterType不用写,在接口上写上参数的注解 update market_channel_alarm_info set update_time = sysDate(), name = #{user.name}, status = '0' where id=#{id} 3. useGeneratedKeys和keyProperty的使用:当主键是自增的情况下,添加一条记录的同时,其主键是不能使用的,但是有时我们需要该主键,这时我们只需要在其对应xml中加入以下属性即可: useGeneratedKeys=“true” keyProperty=“对应的主键的对象”。 useGeneratedKeys这个只在insert语句中有效,当useGeneratedKeys为true时,如果插入的表id以自增列为主键时,将会把该自增id返回。例如: INSERT INTO market_channel_info(`user_name`, `user_gender`, `user_age`) VALUES(#{name}, #{gender}, #{age})这样在之后的java代码中我们就可以获取该主键对应的对象的属性值(id) |
CopyRight 2018-2019 实验室设备网 版权所有 |