Mybatis 传多个参数

来源:互联网 发布:小米平板刷windows包 编辑:程序博客网 时间:2024/06/06 02:26

第一种:基于注解(推荐)

DAO层的函数方法

public User  getUserInfo(@Param("username")String username,@Param("password") String password);

对应的Mapper.xml

<select id="getUserInfo" parameterType="String" resultMap="BaseResultMap">
select * from users WHERE 1=1 
<if test="username!='' and username!=null">
and username=#{username}
</if>
<if test="password!='' and password!=null">
and userpassword=#{password}
</if>

</select>

第二种:

DAO层的函数方法

public User getUser(String username,String password);

对应的Mapper.xml

<select id="getUser" parameterType="String" resultMap="BaseResultMap">
select * from users WHERE username = #{0} and userpassword=#{1}
</select>


第三种:

DAO层的函数方法

 public User getUserInfo(Map map);

对应的Mapper.xml

<select id="getUserInfo" parameterType="String" resultMap="BaseResultMap">
select * from users WHERE 1=1 
<if test="username!='' and username!=null">
and username=#{username}
</if>
<if test="password!='' and password!=null">
and userpassword=#{password}
</if>

</select>

对应的Controller

@RequestMapping(value="redirect",method=RequestMethod.POST)
public String loginInfo(String username,String password) {
    
       // User user=userService.getUser(username, password);
//User user=userService.getUserInfo(username, password);
Map map=new HashMap();
map.put("username", username);
map.put("password", password);
User user=userService.getUserInfo(map);
        if(user!=null)
return "redirect:list";
        else {
return "login";
}
}




原创粉丝点击