mybatis傳參的幾種方式


首先大家都清楚,Mybatis里面傳參方式分別有使用 #{} 和 ${}。

對於使用$符存在安全問題的,該篇不做分析和介紹(其實就是如果傳參的話,使用$需要手動拼接‘ ' ,這就存在注入的風險)

 

接下來,進入正題,通過簡單舉例介紹,

#{}

第一種情形,傳入單個參數  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層:

@Override
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取名字解決即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM