现在的位置: 首页 > 编程语言 > 正文

MyBatisMapper接受参数的四种方式代码解析

2020年02月13日 编程语言 ⁄ 共 1161字 ⁄ 字号 评论关闭

这篇文章主要介绍了MyBatis Mapper接受参数的四种方式代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

对于单个参数而言,可以直接写#{param},这里的占位符名称没有限制,反正就一个参数一个占位符,不需要指定名称

对于多个参数,有常用的四种方式

根据位置排序号

public interface UserDao { public Integer addUser(String username, String password);}

对应mapper文件中的片段

<insert id="addUser"> <!-- 按照参数位置从param1开始排序 --> insert into users(username, password) values(#{param1}, #{param2}) </insert>

POJO对象传入

public interface UserDao { public Integer addUser(String username, String password);}

对应mapper文件中的片段

<insert id="addUser"> <!-- 根据属性名访问数据 --> insert into users(username, password) values(#{username}, #{password}) </insert>

Map对象传入

public interface UserDao { // mapper中#{}则通过map中的key访问 public Integer addUser(Map<String, Object> map);}

对应mapper文件中的片段

<insert id="addUser"> insert into users(username, password) values(#{username}, #{password}) </insert>

@Param注解(Map对象的另一种形式)

public interface UserDao { // 注解中的值就是map的key public Integer addUser(@Param("name")String username, @Param("word")String password);}

对应mapper文件中的片段

<insert id="addUser"> insert into users(username, password) values(#{name}, #{word}) </insert>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: MyBatis Mapper接受参数的四种方式代码解析

以上就上有关MyBatisMapper接受参数的四种方式代码解析的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.