blog--用户登录与注册
1.注册signUp
将用户注册到数据库(使用INSERT来插入)
userMapper.java(接口)
package com.xueba.boke.dao; import com.xueba.boke.bean.User; import org.springframework.stereotype.Repository; @Repository public interface UserMapper { public void login(User user); }
userMapper.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.xueba.boke.dao.UserMapper"> <!--public void login(User user);--> <!-- parameterType:参数类型,可以省略, 获取自增主键的值: mysql支持自增主键,自增主键值的获取,mybatis也是利用statement.getGenreatedKeys(); useGeneratedKeys="true";使用自增主键获取主键值策略 keyProperty;指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装给javaBean的哪个属性 --> <insert id="login" parameterType="com.xueba.boke.bean.User" useGeneratedKeys="true" keyProperty="id"> INSERT INTO userInfo(name, password) VALUES(${userName}, ${userPwd}) </insert> </mapper>
注册之后返回到登录界面
使用redirect:../login.jsp进行转发
@RequestMapping(value="/signUp", method=RequestMethod.POST) public String signUp(HttpServletRequest request, HttpServletResponse response){ String name = request.getParameter( "userName" ); String email = request.getParameter("Email"); String password = request.getParameter("Password"); System.out.println("----------" + email); User user = new User(name, password, email); System.out.println("user: " + user); userService.signUp( user ); return "redirect:../login.jsp"; }
2.用户登录
回显问题
开启驼峰命名
属性mapUnderscoreToCamelCase表示是否启用下划线与驼峰式命名规则的映射(如 => )。其实mybatis是支持数据库属性和实体对象的驼峰命名映射,只需要开启这个配置即可。
数据库字段,我们一般都用下划线分隔 first_name
但是Model中的属性,一般用驼峰命名 firstName
如果需要自动映射,则配置mybatis-config.xml文件 JavaEE——Mybatis(13)--MyBatis与Spring整合--Spring SpringMVC相关配置文件 及Spring整合MyBatis
UserController.java中
package com.xueba.boke.controller; import com.xueba.boke.bean.User; import com.xueba.boke.service.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Create by yyq on 2018/1/17 */ @Controller @RequestMapping("/user") public class UserController { private static Logger log= LoggerFactory.getLogger(UserController.class); @Autowired UserService userService; @RequestMapping(value="/login",method= RequestMethod.POST) public String login(HttpServletRequest request, HttpSession httpSession){ String userName = request.getParameter("userName"); String userPwd = request.getParameter("userPwd"); User visitor=new User(userName, userPwd); System.out.println("visitor: " + visitor); User user = userService.login(visitor); /*添加日志*/ log.debug(user.toString()); if(user == null){ System.out.println("用户不存在"); return "login"; }else{ if(!user.getUserPwd().equals(visitor.getUserPwd())){ System.out.println("密码不正确"); return "login"; } else{ System.out.println("user" + user); httpSession.setAttribute("user", user); return "index"; } } } @RequestMapping(value="/signUp", method=RequestMethod.POST) public String signUp(HttpServletRequest request, HttpServletResponse response){ String name = request.getParameter( "userName" ); String email = request.getParameter("Email"); String password = request.getParameter("Password"); User user = new User(name, password, email); userService.signUp( user ); return "redirect:../login.jsp"; } }
UserServiceImpl.java
package com.xueba.boke.serviceImp; import com.xueba.boke.bean.User; import com.xueba.boke.dao.UserMapper; import com.xueba.boke.service.UserService; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Map; /** * Create by yyq on 2018/1/17 */ @Service("UserService") public class UserServiceImpl implements UserService{ @Autowired UserMapper userMapper; @Override public User login(User visitor) { return userMapper.login(visitor.getUserName()); } public void signUp(User user) { userMapper.signUp( user ); } }
UserMapper.java接口
package com.xueba.boke.dao; import com.xueba.boke.bean.User; import org.springframework.stereotype.Repository; /** * Create by yyq on 2018/1/17 */ @Repository public interface UserMapper { public User login(String userName); public void signUp(User user); }
UserMapper.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.xueba.boke.dao.UserMapper"> <!--public User login(String userName); SELECT user_name as userName, user_pwd as userPwd FROM userInfo WHERE user_name=#{userName}--> <select id="login" resultType="com.xueba.boke.bean.User" parameterType="java.lang.String"> SELECT * FROM userInfo WHERE user_name=#{userName} </select> <!--public void signUp(User user);--> <!-- parameterType:参数类型,可以省略, 获取自增主键的值: mysql支持自增主键,自增主键值的获取,mybatis也是利用statement.getGenreatedKeys(); useGeneratedKeys="true";使用自增主键获取主键值策略 keyProperty;指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装给javaBean的哪个属性 --> <insert id="signUp" parameterType="com.xueba.boke.bean.User" useGeneratedKeys="true" keyProperty="id"> INSERT INTO userInfo(user_name, user_pwd, email) VALUES(#{userName}, #{userPwd}, #{email}) </insert> </mapper>