mybatis 传递参数的7种方法

在实际开发过程中,增删改查操作都要涉及到请求参数的传递,今天这节就集中讲下在mybatis中传递参数的7中方法

单个参数的传递很简单没有什么好将的,这里主要说下多个参数的传递

1.第一种方式 匿名参数 顺序传递参数
复制代码
controller

@ApiOperation(value = “多个参数查询_匿名顺序传参”)
@GetMapping(“findByParams”)
public ResultMsg findByParams(Short gender,String age)
{
List result= employeeMapper.selectByGenderAndAge(gender,age);
return ResultMsg.getMsg(result);
}
复制代码

mapper

List selectByGenderAndAge(Short gender,String age );

xml

select * from employee where gender = #{gender} and age = #{age}

注意这里按参数名去引用的话会报如下错误,mybatis错误提示很细致,这里明确给我们提示,匿名参数只能使用

arg1, arg0, param1, param2 类似的形式

这种传参方式的缺点是不够灵活,必须严格按照参数顺序来引用

BindingException: Parameter ‘gender’ not found. Available parameters are [arg1, arg0, param1, param2]

所以正确的引用方式如下:

select * from employee where gender = #{param1} and age = #{param2}

2.第二种方式 使用@Param注解
controller

复制代码
@ApiOperation(value = “多个参数查询_注解方式传参”)
@GetMapping(“findByParams2”)
public ResultMsg findByParams2(Short gender,String age)
{
List result= employeeMapper.selectByGenderAndAge2(gender,age);
return ResultMsg.getMsg(result);
}
复制代码

mapper

使用@Param注解显示的告诉mybatis参数的名字,这样在xml中就可以按照参数名去引用了

List selectByGenderAndAge( @Param(“gender”) Short gender,@Param(“age”) String age );

xml

select * from employee where gender = #{gender} and age = #{age}

3.使用Map传递参数
实际开发中使用map来传递多个参数是一种推荐的方式

复制代码
controller

@ApiOperation(value = “多个参数查询”)
@GetMapping(“findByMapParams”)
public ResultMsg findByMapParams(Short gender,String age)
{
Map params = new HashMap<>();
params.put(“gender”,gender);
params.put(“age”,age);
List result= employeeMapper.selectByMapParams(params);
return ResultMsg.getMsg(result);
}
复制代码

mapper

List selectByMapParams(Map params);

可以看到使用map来传递多个参数,可以直接使用参数名称进行引用

select * from employee where gender = #{gender} and age = #{age}

4.用过java bean传递多个参数
也可以使用bean的方式来传递多个参数,使用时parameterType指定为对应的bean类型即可

这就传参方式的优点是比较方便,controller层使用@RequestBody接收到实体类参数后,直接传递给mapper层调用即可,不需要在进行参数的转换

复制代码
controller

@ApiOperation(value = “多个参数查询_通过Java Bean传递多个参数”)
@PostMapping(“findByBeans”)
public ResultMsg findByBeans(@RequestBody Employee employee)
{
List result= employeeMapper.selectByBeans(employee);
return ResultMsg.getMsg(result);
}
复制代码

mapper

List selectByBeans(Employee employee);

xml

参数的引用直接使用bean的字段

select * from employee where gender = #{gender} and age = #{age}

测试一下:

5.直接使用JSON传递参数
这也是推荐的一种传参方式,controller层收到JSON型数据后,直接传递给mapper层进行查询操作,简单 方便

controller

复制代码
@ApiOperation(value = “多个参数查询_通过JSON传递多个参数”)
@PostMapping(“findByJSONObject”)
public ResultMsg findByJSONObject(@RequestBody JSONObject params)
{
List result= employeeMapper.findByJSONObject(params);
return ResultMsg.getMsg(result);
}
复制代码

mapper

List findByJSONObject(JSONObject params);

select
*
from employee where gender = #{gender} and age = #{age}

测试一下:

6.传递集合类型参数List、Set、Array
在一些复杂的查询中(如 sql中的 in操作),传统的参数传递已无法满足需求,这时候就要用到List、Set、Array类型的参数传递,具体使用如下:

controller

复制代码
@ApiOperation(value = “多个参数查询_通过List、Set、Array传递多个参数”)
@PostMapping(“findByList”)
public ResultMsg findByList(@RequestBody List list)
{
List result= employeeMapper.findByList (list);
return ResultMsg.getMsg(result);
}
复制代码

mapper

List findByList(List list);

xml

SELECT * from employee where age in #{age}

这里foreach表示循环操作,具体的参数含义如下:

foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名,
index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,
open表示该语句以什么开始,
separator表示在每次进行迭代之间以什么符号作为分隔符,

close表示以什么结束

在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:

1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map或者Object
测试:

])

7.参数类型为对象+集合
该类参数与java Bean参数形式类似,只不过更复杂一些,如下面的Department类,除了基本字段还包括一个Employee的列表

bean

复制代码
@Data
public class Department {
private Long id;

private String deptName;

private String descr;

private Date createTime;

List<Employee> employees;

}
复制代码

controller

复制代码
@ApiOperation(value = “多个参数查询_对象+集合参数”)
@PostMapping(“findByDepartment”)
public ResultMsg findByDepartment(@RequestBody Department department)
{
List result= employeeMapper.findByDepartment(department);
return ResultMsg.getMsg(result);
}
复制代码

mapper

List findByDepartment(@Param(“department”)Department department);

xml

SELECT * from employee where dept_id =#{department.id} and age in #{employee.age}

这里foreach 对应Departmen部门中的List employees

请求参数: 查询部门Id=1,并且年龄 等于24和25的员工

复制代码
{
“createTime”: “2019-07-02T10:17:16.756Z”,
“deptName”: “string”,
“descr”: “string”,
“employees”: [
{
“age”: “24”,
},
{
“age”: “25”,
}
],
“id”: 1
}
复制代码

结果:

复制代码
{
“data”: [
{
“address”: “北新街ndcpc”,
“age”: “24”,
“createTime”: 1562062434000,
“deptId”: “1”,
“gender”: 1,
“id”: “318397755696631808”,
“name”: “kls0bx19cy”
},
{
“address”: “北新街lavi0”,
“age”: “25”,
“createTime”: 1562062436000,
“deptId”: “1”,
“gender”: 1,
“id”: “318397755801489408”,
“name”: “gj9q3ygikh”
}
],
“result”: “SUCCESS”,
“resultCode”: 200,
“resultMsg”: “”
}

原文:https://www.cnblogs.com/cangqinglang/p/14237361.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个优秀的Java持久化框架,它可以轻松地将数据库表和Java对象之间进行映射。在开发过程中,我们通常需要向MyBatis的SQL语句中传递多个参数。下面我们来介绍MyBatis传递多个参数方法。 1. 使用Map传递参数。 通过使用Map来传递多个参数,可以将多个参数打包到一个Map对象中。在MyBatis的Mapper.xml文件中,使用#{key}来引用Map中的参数值。例如,如果我们要传递两个参数,一个是username,一个是age,可以使用如下的方式: ``` <select id="getUser" parameterType="map" resultType="User"> SELECT * FROM user WHERE username = #{username} AND age = #{age} </select> ``` 在Java中使用Map传递参数的例子如下: ``` Map<String, Object> parameterMap = new HashMap<>(); parameterMap.put("username", "张三"); parameterMap.put("age", 18); User user = sqlSession.selectOne("getUser", parameterMap); ``` 2. 使用@Param注解传递参数。 @Param注解可以用来指定参数的名称,从而在Mapper.xml文件中和Java中使用相同的参数。在Mapper.xml文件中,使用#{参数名}来引用参数值。例如,如果我们要传递两个参数,一个是username,一个是age,可以使用如下的方式: ``` <select id="getUser" parameterType="map" resultType="User"> SELECT * FROM user WHERE username = #{username} AND age = #{age} </select> ``` 在Java中使用@Param注解传递参数的例子如下: ``` public User getUser(@Param("username") String username, @Param("age") int age); ``` 3. 使用JavaBean传递参数。 在Java中,我们可以使用JavaBean来封装多个参数,然后在Mapper.xml文件中使用#{属性名}来引用JavaBean属性的值。例如,如果我们要传递两个参数,一个是username,一个是age,可以使用如下的JavaBean: ``` public class UserInfo { private String username; private int age; // getter/setter } ``` 在Mapper.xml文件中,可以如下使用JavaBean传递参数: ``` <select id="getUser" parameterType="UserInfo" resultType="User"> SELECT * FROM user WHERE username = #{username} AND age = #{age} </select> ``` 在Java中使用JavaBean传递参数的例子如下: ``` UserInfo userInfo = new UserInfo(); userInfo.setUsername("张三"); userInfo.setAge(18); User user = sqlSession.selectOne("getUser", userInfo); ``` 总之,MyBatis传递多个参数方法有很多,主要是使用Map、@Param注解和JavaBean来封装参数。在使用的时候,我们需要根据具体情况,选择最适合的方法传递参数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值