DAY04-SpringMVC实现验证登陆功能
先上目录结构
UserDao
public interface UserDao {
User findUserByUserName(String username);
}
UserService
@Service
public class UserService implements IUserService {
@Autowired
private UserDao userDao;
@Override
public boolean login(String username, String password) {
User user = userDao.findUserByUserName(username);
if (user != null && user.getPassword().equals(password)){
return true;
}else{
return false;
}
}
}
IUserService
public interface IUserService {
boolean login(String username, String password);
}
UserController
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user){
boolean flag = userService.login(user.getUsername(),user.getPassword());
ModelAndView modelAndView = new ModelAndView();
if (flag){
modelAndView.setViewName("./ok");
}else{
modelAndView.setViewName("./failure");
}
return modelAndView;
}
}
UserMapping.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.wzbc.dao.UserDao" >
<select id="findUserByUserName" parameterType="String" resultType="com.wzbc.bean.User">
select * from tb_user where username = #{username}
</select>
</mapper>