package com.itheima.web; import com.itheima.mapper.UserMapper; import com.itheima.pojo.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; @WebServlet("/registerServlet") public class RegisterServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.接受用户数据 String username = request.getParameter("username"); String password = request.getParameter("password"); //封装用户对象 User user = new User(); user.setUsername(username); user.setPassword(password); //2.调用mapper 根据用户名调查用户对象 //2.1 获取SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.2 获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //2.3 获取Mapper UserMapper UserMapper = sqlSession.getMapper(UserMapper.class); //2.4 调用方法 User u = UserMapper.selectByUsername(username); //3.判断用户对象是否为null if (u == null){ //用户名不存在,可以添加用户 UserMapper.add(user); //设置 防止乱码 response.setContentType("text/html;charset=utf-8"); //调用Writer输出 response.getWriter().write("注册成功"); //提交事务 sqlSession.commit(); //释放资源 sqlSession.close(); }else { //用户名存在,给出提示信息 //设置 防止乱码 response.setContentType("text/html;charset=utf-8"); //调用Writer输出 response.getWriter().write("用户名已存在"); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>欢迎注册</title> <link href="css/register.css" rel="stylesheet"> </head> <body> <div class="form-div"> <div class="reg-content"> <h1>欢迎注册</h1> <span>已有帐号?</span> <a href="login.html">登录</a> </div> <form id="reg-form" action="/RootDemo1/registerServlet" method="post"> <table> <tr> <td>用户名</td> <td class="inputs"> <input name="username" type="text" id="username"> <br> <span id="username_err" class="err_msg" style="display: none">用户名不太受欢迎</span> </td> </tr> <tr> <td>密码</td> <td class="inputs"> <input name="password" type="password" id="password"> <br> <span id="password_err" class="err_msg" style="display: none">密码格式有误</span> </td> </tr> </table> <div class="buttons"> <input value="注 册" type="submit" id="reg_btn"> </div> <br class="clear"> </form> </div> </body> </html>
package com.itheima.mapper; import com.itheima.pojo.User; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; public interface UserMapper { /* * 根据用户名和密码查询用户对象 * */ @Select("select * from tb_user where username = #{username} and password =#{password}") User select(@Param("username") String username,@Param("password") String password); /* * 根据用户名查询用户对象 * */ @Select("select * from tb_user where username=#{username}") User selectByUsername(String username); /* * 添加用户 * */ @Insert("insert into tb_user values (null,#{username},#{password})") void add(User user); }
标签:username,用户名,用户注册,案例,User,import,password,user From: https://www.cnblogs.com/Karl-hut/p/17512549.html