MyBatis 示例-传递多个参数

映射器的主要元素

本章介绍 select 元素中传递多个参数的处理方式。

测试类:com.yjw.demo.MulParametersTest

使用 Map 传递参数(不建议使用)

使用 MyBatis 提供的 Map 接口作为参数来实现。

StudentDao

/**
 * 使用 Map 传递参数
 *
 * @param params
 * @return
 */
List<StudentDO> listByMap(Map<String, String> params);

StudentMapper.xml

<!-- 使用 Map 传递参数 -->
<select id="listByMap" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_student
    <where>
        <if test="ids != null and ids.size() > 0">
            AND id IN
            <foreach collection="ids" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="name != null and name != ''">
            AND name LIKE CONCAT('%', #{name}, '%')
        </if>
        <if test="sex != null"> AND sex = #{sex} </if> <if test="selfcardNo != null"> AND selfcard_no = #{selfcardNo} </if> </where> </select>

这个方法虽然简单,但是有一个弊端:这样设置的参数使用 Map,而 Map 需要键值对应,由于业务关联性不强,造成代码可读性低。

使用注解方式传递参数(参数少时可以使用)

使用 MyBatis 的参数注解 @Param(org.apache.ibatis.annotations.Param)来实现想要的功能。

StudentDao

/**
  * 使用注解方式传递参数
  *
  * @param name
  * @param sex
  * @return
  */
 List<StudentDO> listByParamAnnotation(@Param("name") String name, 
                                       @Param("sex") Sex sex);

StudentMapper.xml

把映射器的 XML 修改为无需定义参数类型。

<!-- 使用注解方式传递参数 -->
<select id="listByParamAnnotation" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_student
    <where>
        <if test="name != null and name != ''">
            AND name LIKE CONCAT('%', #{name}, '%')
        </if>
        <if test="sex != null">
            AND sex = #{sex}
        </if>
    </where>
</select>

当我们把参数传递给后台的时候,通过 @Param 提供的名称 MyBatis 就会知道 #{name} 代表 name参数,参数的可读性大大提高了。但是这回引起另一个麻烦,一条 SQL 拥有 10 个参数的查询,如果我们都使用 @Param 方式,那么参数将十分复杂,可读性依旧不高,不过 MyBatis 为我们提供了 JavaBean 定义参数的方式来解决这个问题。

使用 JavaBean 传递参数

在参数过多的情况下,MyBatis 允许组织一个 JavaBean,通过简单的 setter 和 getter 方法设置参数,这样就可以提高我们的可读性。

首先,定义一个 StudentQuery 的 JavaBean

public class StudentQuery extends PageQuery {

    private List<Long> ids;

    private String name;

    private Byte sex;

    private Long selfcardNo;

    // get set 方法
 }

StudentDao

/**
 * 根据条件获取学生信息
 * 
 * @param studentQuery
 * @return
 */
List<StudentDO> listByConditions(StudentQuery studentQuery);

StudentMapper.xml

<!-- 根据条件获取学生信息-->
<select id="listByConditions" parameterType="studentQuery" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_student
    <where>
        <if test="ids != null and ids.size() > 0">
            AND id IN
            <foreach collection="ids" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="name != null and name != ''">
            AND name LIKE CONCAT('%', #{name}, '%')
        </if>
        <if test="sex != null"> AND sex = #{sex} </if> <if test="selfcardNo != null"> AND selfcard_no = #{selfcardNo} </if> </where> </select>

总结

下面对各种方式加以总结,以利于我们在实际操作中的应用。

  • 使用 Map 传递参数。因为 Map 导致业务可读性的丧失,从而导致后续扩展和维护的困难,我们应该在实际的应用中果断废弃这样的传递参数的方式。
  • 使用 @Param 注解传递多个参数,这种方式的使用受到参数个数(n)的影响。当 n<= 5 时,它是最佳的传参方式,它比用 JavaBean 更好,因为它更加直观;当 n>5 时,多个参数将给调用带来困难。
  • 当参数个数多于5个时,建议使用 JavaBean 方式。

MyBatis 实用篇

MyBatis 概念

MyBatis 示例-简介

MyBatis 示例-类型处理器

MyBatis 示例-传递多个参数

MyBatis 示例-主键回填

MyBatis 示例-动态 SQL

MyBatis 示例-联合查询

MyBatis 示例-缓存

MyBatis 示例-插件

发布了83 篇原创文章 · 获赞 26 · 访问量 66万+

猜你喜欢

转载自blog.csdn.net/yin_jw/article/details/104257729
今日推荐