java开发

技术学习笔记

Mybatis传递多个参数的方法

1:顺序传递参数

 #{}里面的数字代表你传入参数的顺序。

public User selectUser(String name, int deptId);


<select id="selectUser" resultMap="UserResultMap">
    select * from user
    where user_name = #{0} and dept_id = #{1}
</select> 

2:@Param注解传参法 

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


public User selectUser(@Param("userName") String name, int @Param("deptId") deptId);


<select id="selectUser" resultMap="UserResultMap">
    select * from user
    where user_name = #{userName} and dept_id = #{deptId}
</select> 

3:Map传参法

#{}里面的名称对应的是Map里面的key名称。



public User selectUser(Map<String, Object> params);



<select id="selectUser" parameterType="java.util.Map" resultMap="UserResultMap">
    select * from user
    where user_name = #{userName} and dept_id = #{deptId}
</select> 



4:Java Bean传参法

参数以Bean形式传入,这种方法很直观,但需要建一个实体类

UserPageRequestParams
(请求体),扩展时需要向类中添加属性

Page<User> pageWithBean(Page page, @Param("params") UserPageRequestParams params);


<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> 


发表评论:

Powered By Z-BlogPHP 1.7.1

唐云飞个人日记