pom文件:
jakarta.servlet jakarta.servlet-api 5.0.0 provided commons-io commons-io 2.6 org.mybatis mybatis 3.5.11 mysql mysql-connector-java 8.0.31
接口:
public interface UserMapper { @Select("select * from tb_user where username = #{username} and 密码英文 = #{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); }
接口对应的接口,一模一样的名字.xml:
mybatis-config.xml:
---------------------------------------------------------------------------------------------------------------------------------
实列类:
public class User { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", 密码英文='" + password + '\'' + '}'; } }
----------------------------------------------------------------------------------------------------------------------------
//代码优化(工具类) public class SqlSessionFactoryUtils { private static final SqlSessionFactory sqlSessionFactory; static{ //静态代码块随着类的加载而自动执行,且只执行一次 try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { throw new RuntimeException(e); } } public static SqlSessionFactory getSqlSessionFactory(){ return sqlSessionFactory; } }
-----------------------------------------------------------------------------------------------------------------------------
登录页面代码:
@WebServlet("/loginServlet") public class loginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.接受用户名和密码 String username = request.getParameter("username"); String 密码英文 = request.getParameter("password"); //2.调用Mybatis完成查询 //2.1获取SqlSessionFactory对象 SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory(); //2.2获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //2.3获取Mapper UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //2.4调用方法 User user = userMapper.select(username, password); //2.5释放资源 sqlSession.close(); //获取字符输出流,并设置content type response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); //3.判断user是否为null if (user !=null){ //登录成功 writer.write("登录成功"); }else { //登录失败 writer.write("登录失败"); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response); } }
注册代码:
@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 密码英文 = request.getParameter("password"); //封装用户对象 User user = new User(); user.setUsername(username); user.setPassword(password); //2.调用mapper 根据用户名查询用户对象 //2.1获取SqlSessionFactory对象 SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory(); //2.2获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //2.3获取Mapper UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //2.4调用方法 User u = userMapper.selectByUsername(username); //用户名存在,给出提示信息 //获取字符输出流,并设置content type response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); //3.判断用户对象是否为null if (u==null){ //用户名不存在 userMapper.add(user); //提交事务 sqlSession.commit(); writer.write("注册成功"); //释放资源 sqlSession.close(); }else { writer.write("用户名以及存在"); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response); } }