第一种:基于注解(推荐)
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";
}
}