天天看點

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>      

--------------------------------------------------------------------------------------

${} 

 使用這個的時候,隻需要注意,如果是傳遞字段名或者表名,是直接做參數傳入即可,

但是如果作為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>