Mybatis实现同时传入对象参数和字符串参数

1. Dao

字符串和对象参数都用@param注解.

import org.apache.ibatis.annotations.Param;


public List<User> selectAllUsers(
                        @Param("user") User user, 
                        @Param("bm") String bm);
2. mapper.xml

mapper.xml中使用的时候,使用#{对象名.属性名}取值,如#{user.id},动态SQL判断时也要用 对象名.属性名.

注意,使用了@pram注解的话在mapper.xml不加parameterType。

<select id="selectAllUsers" resultMap="UserMap">
        select *
        from user
        where bm='0000'
        <if test="user.name != null and user.name != ''">
            and name like concat(concat('%',#{user.name}),'%')
        </if>
        <if test="user.sex != null and user.sex != ''">
            and sex like concat(concat('%',#{user.sex}),'%')
        </if>
</select>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。