欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis中传递多个参数的4种方法

程序员文章站 2024-03-06 08:09:31
...

概述

Mybatis在使用Mapper接口进行编程时,其实底层是采用了动态代理机制,表面上是调用的Mapper接口,而实际上是通过动态代理调用的SqlSession的对应方法,其最终会获得一个代理了Mapper接口的MapperProxy对象。MapperProxy对象在调用Mapper接口方法时会把传递的参数做一个转换,然后把转换后的参数作为入参调用SqlSession对应的操作方法(如:selectOneinsert等)。

使用myBatis注解形式的SQL,则首先需要在启动类上使用@MapperScan注解扫描到SQL所在的DAO接口或者mapper接口。

1.Java Bean传参法

参数以Bean形式传入,这种方法很直观,但需要建一个实体类(请求体),扩展时需要向类中添加属性

1.1 指定变量名

Controller.java

@GetMapping("/api/user/page/bean")
public R pageWithBean(UserPageRequest request) {
    log.debug("分页『Bean传参法』......");
    UserPageRequestParams params = new UserPageRequestParams();
    BeanUtils.copyProperties(request, params);
    Page<User> page = userMapper.pageWithBean(new Page(request.getCurrent(), request.getSize()), params);
    return R.ok(page);
}

UserMapper.java

/**
  * 指定变量名“params”
  *
  * @param page
  * @param params
  * @return
  */
 Page<User> pageWithBean(Page page, @Param("params") UserPageRequestParams params);

UserMapper.xml

<select id="pageWithBean" resultType="michael.spica.entity.mybatisplus.User">
    SELECT * FROM tb_user u
    <where>
        <if test="null != params.name">
            AND u.name LIKE CONCAT('%',#{params.name},'%')
        </if>
        <if test="null != params.email">
            AND u.email LIKE CONCAT('%',#{params.email},'%')
        </if>
        <if test="null != params.phoneType">
            AND u.phone_type = #{params.phoneType}
        </if>
        <if test="null != params.gender">
            AND u.gender = #{params.gender}
        </if>
    </where>
</select>

#{}里面的名称对应的是User类里面的成员属性

1.2 默认变量名

Controller.java

@GetMapping("/api/user/page/bean/default")
public R pageWithBean(UserPageRequest request) {
    log.debug("分页『Bean传参法』......");
    UserPageRequestParams params = new UserPageRequestParams();
    BeanUtils.copyProperties(request, params);
    Page<User> page = userMapper.pageWithBeanDefault(new Page(request.getCurrent(), request.getSize()), params);
    return R.ok(page);
}

UserMapper.java

/**
  * 默认变量“params” --> “param2”
  *
  * @param page
  * @param params
  * @return
  */
 Page<User> pageWithBeanDefault(Page page, UserPageRequestParams params);

UserMapper.xml

<select id="pageWithBeanDefault" resultType="michael.spica.entity.mybatisplus.User">
    SELECT * FROM tb_user u
    <where>
        <if test="null != param2.name">
            AND u.name LIKE CONCAT('%',#{param2.name},'%')
        </if>
        <if test="null != param2.email">
            AND u.email LIKE CONCAT('%',#{param2.email},'%')
        </if>
        <if test="null != param2.phoneType">
            AND u.phone_type = #{param2.phoneType}
        </if>
        <if test="null != param2.gender">
            AND u.gender = #{param2.gender}
        </if>
    </where>
</select>

#{}里面的名称对应的是User类里面的成员属性
#{param2.xxx}param2为默认变量名

2.Map传参法

参数以Map形式传入,这种方法对后台来说是一劳永逸(以后如果需要添加参数,Controller可以不用修改,只需要修改SQL文件),但是不直观,对于Map中解析了什么是不清楚的

Controller.java

@GetMapping("/api/user/page/map")
public R pageWithMap(Map<String, Object> paramsMap) {
    log.debug("分页『Map传参法』......");
    Page<User> page = userMapper.pageWithMap(new Page(request.getCurrent(), request.getSize()), paramsMap);
    Page<User> page = userMapper.pageWithMapDefault(new Page(request.getCurrent(), request.getSize()), paramsMap);
    return R.ok(page);
}

@GetMapping("/api/user/page/map/default")
public R pageWithMap(Map<String, Object> paramsMap) {
    log.debug("分页『Map传参法』......");
    Page<User> page = userMapper.pageWithMapDefault(new Page(request.getCurrent(), request.getSize()), paramsMap);
    return R.ok(page);
}

UserMapper.java

/**
  * 指定变量名“paramsMap”
  *
  * @param page
  * @param paramsMap
  * @return
  */
Page<User> pageWithMap(Page page, @Param("paramsMap") Map<String, Object> paramsMap);

/**
  * 默认变量“paramsMap” --> “param2”
  *
  * @param page
  * @param paramsMap
  * @return
  */
Page<User> pageWithMapDefault(Page page, Map<String, Object> paramsMap);

UserMapper.xml

<select id="pageWithMap" resultType="michael.spica.entity.mybatisplus.User">
   SELECT * FROM tb_user u
    <where>
        <if test="null != paramsMap.name">
            AND u.name LIKE CONCAT('%',#{paramsMap.name},'%')
        </if>
        <if test="null != paramsMap.email">
            AND u.email LIKE CONCAT('%',#{paramsMap.email},'%')
        </if>
        <if test="null != paramsMap.phoneType">
            AND u.phone_type = #{paramsMap.phoneType}
        </if>
        <if test="null != paramsMap.gender">
            AND u.gender = #{paramsMap.gender}
        </if>
    </where>
</select>

<select id="pageWithMapDefault" resultType="michael.spica.entity.mybatisplus.User">
    SELECT * FROM tb_user u
    <where>
        <if test="null != param2.name">
            AND u.name LIKE CONCAT('%',#{param2.name},'%')
        </if>
        <if test="null != param2.email">
            AND u.email LIKE CONCAT('%',#{param2.email},'%')
        </if>
        <if test="null != param2.phoneType">
            AND u.phone_type = #{param2.phoneType}
        </if>
        <if test="null != param2.gender">
            AND u.gender = #{param2.gender}
        </if>
    </where>
</select>

3.@Param注解传参法

这种方法在参数不多的情况还是比较直观的,若参数多的情况下推荐使用Java Bean传参法

Controller.java

@GetMapping("/api/user/page/param")
public R pageWithParam(UserPageRequest request) {
    log.debug("分页『@Param注解传参法』......");
    Page<User> page = userMapper.pageWithParam(new Page(request.getCurrent(), request.getSize()),
            request.getName(),
            request.getEmail(),
            request.getPhoneType(),
            request.getGender()
    );
    return R.ok(page);
}

UserMapper.java

Page<User> pageWithParam(Page page,
                         @Param("name") String name,
                         @Param("email") String email,
                         @Param("phoneType") String phoneType,
                         @Param("gender") String gender);

UserMapper.xml

<select id="pageWithParam" resultType="michael.spica.entity.mybatisplus.User">
    SELECT * FROM tb_user u
    <where>
        <if test="null != name">
            AND u.name LIKE CONCAT('%',#{name},'%')
        </if>
        <if test="null != email">
            AND u.email LIKE CONCAT('%',#{email},'%')
        </if>
        <if test="null != phoneType">
            AND u.phone_type = #{phoneType}
        </if>
        <if test="null != gender">
            AND u.gender = #{gender}
        </if>
    </where>
</select>

#{}里面的名称对应的是注解@Param括号里面修饰的名称

4.索引传参法(也叫顺序传参法)

#{}里面的参数如果使用arg,则从0开始。如果使用param,则从1开始,如:param1,因SQL层表达不直观,且一旦顺序调整容易出错,所以这种方法不建议使用

Controller.java

@GetMapping("/api/user/page/index")
public R pageWithIndex(UserPageRequest request) {
    log.debug("分页『Index传参法』......");
    Page<User> page = userMapper.pageWithIndex(new Page(request.getCurrent(), request.getSize()),
            request.getName(),
            request.getEmail(),
            request.getPhoneType(),
            request.getGender()
    );
    return R.ok(page);
}

UserMapper.java

Page<User> pageWithIndex(Page page,
                         String name,
                         String email,
                         String phoneType,
                         String gender);

UserMapper.xml

<select id="pageWithIndex" resultType="michael.spica.entity.mybatisplus.User">
    SELECT * FROM tb_user u
    <where>
        <if test="null != param2">
            AND u.name LIKE CONCAT('%',#{param2},'%')
        </if>
        <if test="null != param3">
            AND u.email LIKE CONCAT('%',#{param3},'%')
        </if>
        <if test="null != param4">
            AND u.phone_type = #{param4}
        </if>
        <if test="null != param5">
            AND u.gender = #{param5}
        </if>
    </where>
</select>
相关标签: MyBatisPlus