Mybatis 传参的方式

发布时间:2023年12月30日

第一种情形,传入单个参数??userId

service层:

@Override
public User getUserInfo(Integer userId) {
    User user = userMapper.getUserInfo(userId);
 
    //省略 业务代码...
    
    return user;
}

mapper层:

User getUserInfo(Integer userId);

mapper.xml:

<!--查询-->
<select id="getUserInfo" resultType="com.demo.elegant.pojo.User">
    select userId 
    from users
    where userId=#{userId};
</select>

第二种情况,传入多个参数??userId,sex??使用索引对应值

按照顺序传参

注意mapper层和xml层!

service层:

@Override
public User getUserInfo(Integer userId,String sex) {
    User user = userMapper.getUserInfo(userId,sex);
    //省略 业务代码...
    return user;
}

mapper层:

User getUserInfo(Integer userId,String sex);

mapper.xml:

<!--查询-->
<select id="getUserInfo" resultType="com.demo.elegant.pojo.User">
    select userId
    from users
    where userId=#{0} and sex=#{1};
</select>

第三种情形,传入多个参数??userId,sex?使用注解@Param?

service层

@Overrid
public User getUserInfo(Integer userId,String sex) {
    User user = userMapper.getUserInfo(userId,sex);
    //省略 业务代码...
    return user;
}

mapper层:

User getUserInfo(@Param("userId")Integer userId,@Param("sex")String sex);

mapper.xml:

<!--查询-->
<select id="getUserInfo" resultType="com.demo.elegant.pojo.User">
    select userId
    from users
    where userId=#{userId} and sex=#{sex};
</select>

第四种情形,传入多个参数???使用User实体类传入

service层:

@Override
public User getUserInfo(User user) {
    User userInfo = userMapper.getUserInfo(user);
    //省略 业务代码...
    return userInfo;
}

mapper层:

User getUserInfo(User User);

mapper.xml:

<!--查询-->
<select id="getUserInfo"  parameterType="User"  resultType="com.demo.elegant.pojo.User">
    select userId
    from users
    where userId=#{userId} and sex=#{sex};
</select>

第五种情形,传入多个参数,?使用Map类传入

service层:

@Override
public User getUserInfo(Map map) {
    User user = userMapper.getUserInfo(map);
    //省略 业务代码...
    return user;
}

mapper层:

User getUserInfo(Map map);

mapper.xml层:

<!--查询-->
<select id="getUserInfo"  parameterType="Map"  resultType="com.demo.elegant.pojo.User">
    select userId
    from users
    where userId=#{userId} and sex=#{sex};
</select>

第六种情形,传入多个参,使用?map封装实体类传入

这种情况其实使用场景比较少,因为上面的各种姿势其实已经够用了

service层:

@Override
public User getUserInfo1(Integer userId,String sex) {
    User userInfo = new User(userId,sex);
    Map<String,Object> map=new HashMap<String,Object>();
    map.put("user",userInfo);
 
    User userResult=  userMapper.getUserInfo(map);
    //省略 业务代码...
    return userResult;
}

mapper层:?

User getUserInfo(Map map);

mapper.xml:

<!--查询-->
<select id="getUserInfo"  parameterType="Map"  resultType="com.demo.elegant.pojo.User">
    select userId
    from users
    where userId=#{userInfo.userId} and sex=#{userInfo.sex};
</select>

第七种情形,即需要传入实体类,又需要传入多个单独参,使用注解@Param?

service层:

@Override
public User getUserInfo(User user,Integer age) {
    User userResult = userMapper.getUserInfo(user,age);
    //省略 业务代码...
    return userResult;
}

mapper层:?

User getUserInfo(@Param("userInfo") User user,@Param("age") Integer age);

mapper.xml:

<!--查询-->
<select id="getUserInfo"   resultType="com.demo.elegant.pojo.User">
    select userId
    from users
    where userId=#{userInfo.userId} and sex=#{userInfo.sex} and age=#{age};
</select>

List传参

service层:

List<Integer>list= new ArrayList>();
 list. add(44);
 list. add(45);
 list. add(46);
List<SysUser> sysUser= sysUserMapper. selectList(list);

mapper层:

 List<SysUser> selectList(List<Integer> ids);

mapper.xml:

 <select id="selectList"resultMap"BaseResultMap">
 select
 <include refid="Base_Column_List"/>
 from sys_user
 where id in
 <foreach item="item" index="index" collection="list"open="("separator","close=")"> #{item}
 </foreach>
 </select>

数组传参

service层:

List<SysUser> sysuser= sysUserMapper. selectlist(new Integer[]{44,45,46});

mapper层:

List<SysUser> selectList(Integer[]ids);

mapper.xml:

 <select id="selectList"resultMap"BaseResultMap">
 select
 <include refid="Base Column_List"/>
 from sys user
 where id in
 <foreach item="item" index="index collection="array"open="("separator="," close=")"> #{item}
 </foreach>
 </select>

${}

?使用这个的时候,只需要注意,如果是传递字段名或者表名,是直接做参数传入即可,

但是如果作为sql'语句里面使用的值, 记得需要手动拼接 ' ' 号。

例如, 传入单个参数 sex:

service层:

@Override
public User getUserInfo(String sex) {
 
    sex="'"+sex+"'";
    User user = userMapper.getUserInfo(sex);
    //省略 业务代码...
    return user;
}

mapper层:

User getUserInfo(String sex);

mapper.xml:

<!--查询-->
<select id="getUserInfo"   resultType="com.demo.elegant.pojo.User">
    select userId
    from users
    where sex=${sex};
</select>

多个参数,那也就是使用注解@Param取名字解决即可。

文章来源:https://blog.csdn.net/X_islu/article/details/135309696
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。