Mybatis传递参数

发布时间:2022-07-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Mybatis传递参数脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

原博文链接

1. 匿名参数,顺序传递参数

mapper:

List<Employee> selectByGenderAndAge(Short gender,String age );

xml:

<select id="selectByGenderAndAge" resultMap="BaseResultMap" >
  select * from employee where gender = #{0} and age = #{1}
</select>

 

2.  使用@Param注解

mapper:

List<Employee> selectByGenderAndAge( @Param("gender") Short gender,@Param("age") String age );

xml:

<select id="selectByGenderAndAge" resultMap="BaseResultMap" >
  select * from employee where gender = #{gender} and age = #{age}
</select>

 

3. 使用map传递参数

mapper:

List<Employee> selectByMapParams(Map params);

xml:

<select id="selectByMapParams" resultMap="BaseResultMap" parameterType="map">
  select * from employee where gender = #{gender} and age = #{age}
</select>

 

4. 如果参数是 pojo,即实体类

mapper:

List <Employee> selectByBeans(Employee employee);

xml:

参数的引用直接使用实体类的字段

<select id="selectByBeans" resultMap="BaseResultMap" parameterType="com.wg.demo.po.Employee">
  select
  *
  from employee where gender = #{gender} and age = #{age}
</select>

 

5. 使用json传递数据

controller:  使用@RequestBody注解之后,前端发送的json数据不会被转化为对象

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

mapper:

List <Employee> findByJSONObject(JSONObject params);

xml:

<select id="findByJSONObject" resultMap="BaseResultMap" parameterType="com.alibaba.fastjson.JSONObject">
  select
  *
  from employee where gender = #{gender} and age = #{age}
</select>

 

6.

传递集合类型参数List、Set、Array

controller:

@ApiOperation(value = "多个参数查询_通过List、Set、Array传递多个参数")
@PostMapping("findByList")
public ResultMsg findByList(@RequestBody List<String> list)
{
    List result= employeeMapper.findByList (list);
    return ResultMsg.getMsg(result);
}

mapper:

List <Employee> findByList(List list);

xml:

  <select id="findByList" resultMap="BaseResultMap" >
SELECT * from employee where age in
    <foreach collection="list" open="(" separator="," close=")" item="age">
      #{age}
    </foreach>
  </select>

 

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

close表示以什么结束

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

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

 

7. 参数类型为对象+集合

pojo:

@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 <Employee> findByDepartment(@Param("department")Department department);

xml;

<select id="findByDepartment" resultMap="BaseResultMap" parameterType="com.wg.demo.po.Department">
    SELECT * from employee where dept_id =#{department.id} and age in
    <foreach collection="department.employees" open="(" separator="," close=")" item="employee">
        #{employee.age}
    </foreach>
</select>

 

请求参数: 查询部门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": ""
}

 

 

 

 

 

 

 

 

 

 

 

 

TRANSLATE with Mybatis传递参数 x
English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
Mybatis传递参数
Mybatis传递参数 Mybatis传递参数 Mybatis传递参数
 
TRANSLATE with Mybatis传递参数
COPY THE URL BELOW
Mybatis传递参数
Mybatis传递参数 Back
EMBED THE SNIPPET BELOW IN YOUR SITE Mybatis传递参数
Enable collaborative features and customize widget: Bing Webmaster Portal
Back

脚本宝典总结

以上是脚本宝典为你收集整理的Mybatis传递参数全部内容,希望文章能够帮你解决Mybatis传递参数所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: