本文最后更新于 2024-12-12,文章内容可能已经过时。

xmvc01.war

UserMapper

<select id="findUserByName" resultType="user">
		SELECT * FROM user where userName = #{userName};
	</select>
	<select id="findAll" resultType="user">
		SELECT * FROM user;
	</select>

UserServiceImpl

User dbUser = userMapper.findUserByName(user.getUserName());//根据userName查询数据库信息
		if(dbUser != null && user.getPassword().equals(dbUser.getPassword())) {//校验密码
			return true;
		}else {
			return false;
		}

LoginController

if(userService.isLogin(user)) {//判断用户是否正确
			HttpSession session = request.getSession();//获取session对象
			session.setAttribute("user", user);//将用户名保存到session对象中
			return "redirect:/users/index"
		}else {
			return "redirect:/index";
		}

UserController

public ModelAndView  index(HttpServletRequest request) {
HttpSession session = request.getSession();//获取session对象
		User user = (User) session.getAttribute("user");
		ModelAndView mv = new ModelAndView();
		if(user != null) {
			List<User> users = userService.getUsers();//获取所有user表数据
			mv.addObject("userList", users);
			mv.setViewName("users");
		}else {
			mv.setViewName("login");
		}
		return mv; //请修改代码
	}

添加

<insert id="insert">

insert into user(id,userName,password)

values (#{id},#{userName},#{password});

</insert>