Mybatis传参的方式总结

简介: mybatis传参的几种方式?

mybatis传参的几种方式?


目录

第一种情形,传入单个参数 userId

第二种情况,传入多个参数 userId,sex 使用索引对应值

第三种情形,传入多个参数 userId,sex 使用注解@Param

第四种情形,传入多个参数 使用User实体类传入

第五种情形,传入多个参数, 使用Map类传入

第六种情形,传入多个参,使用 map封装实体类传入

第七种情形,即需要传入实体类,又需要传入多个单独参,使用注解@Param

List传参

数组传参

${}


首先大家都清楚,Mybatis里面传参方式分别有使用 #{} 和 ${}。


对于使用符 存 在 安 全 问 题 的 , 该 篇 不 做 分 析 和 介 绍 ( 其 实 就 是 如 果 传 参 的 话 , 使 用 符存在安全问题的,该篇不做分析和介绍(其实就是如果传参的话,使用符存在安全问题的,该篇不做分析和介绍(其实就是如果传参的话,使用需要手动拼接‘ ’ ,这就存在注入的风险)


接下来,进入正题,通过简单举例介绍,

#{}


第一种情形,传入单个参数 userId

service层:

@Override
public User getUserInfo(Integer userId) {
User user = userMapper.getUserInfo(userId);
//省略 业务代码…
return user;
}


mapper层:

User getUserInfo(Integer userId);
mapper.xml:


select userId from users where userId=#{userId};

第二种情况,传入多个参数 userId,sex 使用索引对应值


按照顺序传参

注意mapper层和xml层!

service层:

@Override
public User getUserInfo(Integer userId,String sex) {
User user = userMapper.getUserInfo(userId,sex);
//省略 业务代码…
return user;
}

mapper层:

User getUserInfo(Integer userId,String sex);
mapper.xml:


select userId from users where userId=#{0} and sex=#{1};

第三种情形,传入多个参数 userId,sex 使用注解@Param

service层:

@Override
public User getUserInfo(Integer userId,String sex) {
User user = userMapper.getUserInfo(userId,sex);
//省略 业务代码…
return user;
}

mapper层:

User getUserInfo(@Param(“userId”)Integer userId,@Param(“sex”)String sex);
mapper.xml:


select userId from users where userId=#{userId} and sex=#{sex};

第四种情形,传入多个参数 使用User实体类传入

service层:

@Override
public User getUserInfo(User user) {
User userInfo = userMapper.getUserInfo(user);
//省略 业务代码…
return userInfo;
}

mapper层:

User getUserInfo(User user);
mapper.xml:

select userId from users where userId=#{userId} and sex=#{sex};

第五种情形,传入多个参数, 使用Map类传入

service层:

@Override
public User getUserInfo(Map map) {
User user = userMapper.getUserInfo(map);
//省略 业务代码…
return user;
}

mapper层:

User getUserInfo(Map map);
mapper.xml层:

select userId from users where userId=#{userId} and sex=#{sex};

第六种情形,传入多个参,使用 map封装实体类传入

这种情况其实使用场景比较少,因为上面的各种姿势其实已经够用了

service层:

@Override
public User getUserInfo1(Integer userId,String sex) {
User userInfo = new User(userId,sex);
Map<String,Object> map=new HashMap<String,Object>();
map.put(“user”,userInfo);
User userResult= userMapper.getUserInfo(map);
//省略 业务代码…
return userResult;
}

mapper层:

User getUserInfo(Map map);
mapper.xml:


select userId from users where userId=#{userInfo.userId} and sex=#{userInfo.sex};

第七种情形,即需要传入实体类,又需要传入多个单独参,使用注解@Param

service层:

@Override
public User getUserInfo(User user,Integer age) {
User userResult = userMapper.getUserInfo(user,age);
//省略 业务代码…
return userResult;
}

mapper层:

User getUserInfo(@Param(“userInfo”) User user,@Param(“age”) Integer age);
mapper.xml:


select userId from users where userId=#{userInfo.userId} and sex=#{userInfo.sex} and age=#{age};

List传参

service层:

Listlist= new ArrayList>();
list. add(44);
list. add(45);
list. add(46);
List sysUser= sysUserMapper. selectList(list);

mapper层:

List selectList(List ids);
mapper.xml:
<select id=“selectList"resultMap"BaseResultMap”>
select
from sys_user
where id in
<foreach item=“item” index=“index” collection=“list"open=”(“separator”,“close=”)"> #{item}


数组传参

service层:

List sysuser= sysUserMapper. selectlist(new Integer[]{44,45,46});

mapper层:

List selectList(Integer[]ids);


mapper.xml:

<select id=“selectList"resultMap"BaseResultMap”>

select


from sys user

where id in

<foreach item=“item” index=“index collection=“array"open=”(“separator=”,” close=")"> #{item}



${}


使用这个的时候,只需要注意,如果是传递字段名或者表名,是直接做参数传入即可,

但是如果作为sql’语句里面使用的值, 记得需要手动拼接 ’ ’ 号。


例如, 传入单个参数 sex:

service层:

@Override

public User getUserInfo(String sex) {

sex="’"+sex+"’";

User user = userMapper.getUserInfo(sex);

//省略 业务代码…

return user;

}

mapper层:

User getUserInfo(String sex);

mapper.xml:


select userId from users where sex=${sex}; 多个参数,那也就是使用注解@Param取名字解决即可。


目录
相关文章
|
XML SQL Java
Mybatis 传参方式
多个参数,那也就是使用注解@Param取名字解决即可。
125 1
|
XML Java 数据库连接
mybatis传参为map的写法
mybatis传参为map的写法
159 0
|
SQL XML 存储
Mybatis知识点全总结(二)
Mybatis知识点全总结
232 1
Mybatis知识点全总结(二)
|
XML Java 数据库连接
mybatis接口方法参数传参解读
mybatis接口方法参数传参解读
|
Java 数据库连接 mybatis
mybatis传参、被逗号、分割的字符串、数组传参
mybatis传参、被逗号、分割的字符串、数组传参
962 0
mybatis传参、被逗号、分割的字符串、数组传参
|
SQL Java 数据库连接
五、MyBatis获取参数值的两种方式以及传参情况
${}的本质就是字符串拼接,#{}的本质就是占位符赋值。
436 0
五、MyBatis获取参数值的两种方式以及传参情况
|
XML SQL Java
[翻译]Mybatis useGeneratedKeys参数的使用和自增主键的获取方式
对于支持自动生成记录主键的数据库,如 MySQL 和 SQL Server,将 useGeneratedKeys 参数的值设置为 true,就可以在记录insert成功后获得数据库自动生成的主键 ID
658 0
|
SQL Java 数据库连接
SpringBoot整合Mybatis(超简单的整合方式)
SpringBoot整合Mybatis(超简单的整合方式)
SpringBoot整合Mybatis(超简单的整合方式)
|
Java 数据库连接 Spring
Spring整合Mybatis,SqlSessionDaoSupport方式
SqlSessionDaoSupport 是一个抽象的支持类,用来为你提供 SqlSession。调用 getSqlSession() 方法你会得到一个 SqlSessionTemplate🙌 接口实现类:(此类继承SqlSessionDaoSupport即可)
336 2