MyBatis傳參三種方式
MyBatis傳參
1、直接傳
Dao層的函式方法
Public User selectUser(String name,String area);
對應的Mapper.xml
<select * from user where name =#{0} and area=#{1}> </select>
其中#{0} 代表第一個引數,#{1}代表第二個引數,依次類推
2、 採用Map傳引數
Public User selectUser(Map paramMap);
對應的Mapper.xml
<select * from user where name=#{userName,jdbcType=VARCHAR} and area=#{userArea,jdbcType=VARCHAR} </select>
Service 層呼叫
private User XXXSelectUser(){
Map paramMap=new hashMap();
paramMap.put("userName","對應的value");
paramMap.put("userArea","對應的value");
User user=xxx.selectUser(paramMap);
}
3、用內嵌變數
Dao層函式方法
Public User selectUser(@param("userName")String name,@param("userArea") String area);
對應的Mapper.xml
<select id=" selectUser" resultMap="BaseResultMap">
select * from user where name = #{userName,jdbcType=VARCHAR} and area=#{userArea,jdbcType=VARCHAR}
</select>
這種還是比較好的