一、用户登录/注册实现
综合前面学的知识来实现简单的注册登录功能
1.准备工作
-
注册登录页面
-
数据库,数据表
-
mybatis
-
坐标引入,MySQL驱动
-
配置
-
映射文件
-
-
用户实体类
-
Servlet代码
2.页面
不想手写的可以看博主IT黄大大【带源码】
【炫酷登录界面】详解5款高级的前端登录页面及实现源码(附完整源码)_好看的前端页面-CSDN博客
也可以根据需要自己仿照着改,我是直接拿来用练习了~
3.数据库
使用IDEA,Navicat等工具创建点初始数据即可
4.mybatis配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><!-- 数据库连接信息--><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql:///【数据库名】?useSSL=false&useServerPrepStmts=true"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><mappers>
<!-- 包扫描,扫描mapper--><package name="com/mapper"/></mappers>
</configuration>
映射文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.UserMapper"><sql标签>sql代码...</sql标签>
</mapper>
依赖:
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version>
</dependency>
<!-- mysql驱动-->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.46</version>
</dependency><!--单元测试-->
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version><scope>test</scope>
</dependency><!--Servlet-->
<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope>
</dependency>
5.用户实体类
没啥要说的,下一个
6.具体
映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.UserMapper">
<select id="selectByUsername" resultType="string">select username from user where username = #{username};</select>
<select id="selectByEmail" resultType="string">select email from user where email = #{email};</select>
<select id="selectInfoBy_U_P" resultType="com.pojo.User">select * from user where username = #{username} and password = #{password}</select>
<select id="selectInfoBy_E_P" resultType="com.pojo.User">select * from user where email = #{email} and password = #{password}</select>
<insert id="add">insert intouser(username,email,password)values(#{username},#{email},#{password});</insert>
</mapper>
封装工具类
package com.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class SqlSessionFactoryUtils {private static final SqlSessionFactory SQL_SESSION_FACTORY;
static {String resource = "mybatis-config.xml";InputStream inputStream = null;try {inputStream = Resources.getResourceAsStream(resource);} catch (IOException e) {e.printStackTrace();}SQL_SESSION_FACTORY = new SqlSessionFactoryBuilder().build(inputStream);}
public static SqlSessionFactory getSqlSessionFactory(){return SQL_SESSION_FACTORY;}
}
UserMapper
package com.mapper;
import com.pojo.User;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {/*** 注册:根据用户名查询User对象* @param username 用户名* @return User对象,如果为null,则代表不存在该用户,该用户名可用*/String selectByUsername(String username);
/*** 注册:根据邮箱查询User对象* @param email 用户名* @return User对象,如果为null,则代表不存在该用户,该邮箱可用*/String selectByEmail(String email);
/*** 登录:根据用户名和密码查询User是否存在* @param username 用户名* @param password 密码* @return User对象,如果为null,则代表不存在该用户*/User selectInfoBy_U_P(@Param("username") String username,@Param("password") String password);
/*** 登录:根据邮箱和密码查询User是否存在* @param email 邮箱* @param password 密码* @return User对象,如果为null,则代表不存在该用户*/User selectInfoBy_E_P(@Param("email") String email,@Param("password") String password);
/*** 注册:添加用户* @param username 用户名* @param email 邮箱* @param password 密码*/void add(@Param("username") String username,@Param("email") String email,@Param("password") String password);
}
Servlet
login
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {User user;final String voucher = request.getParameter("voucher");final String password = request.getParameter("password");//查询//1.获取SqlSessionFactory对象final SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
//2.获取SqlSession对象final SqlSession sqlSession = sqlSessionFactory.openSession();//3.获取Mapperfinal UserMapper mapper = sqlSession.getMapper(UserMapper.class);//4.调用方法if (voucher.contains("@")){user = mapper.selectInfoBy_E_P(voucher,password);}else{user = mapper.selectInfoBy_U_P(voucher,password);}//5.释放资源sqlSession.close();response.setContentType("text/html;charset=utf-8");final PrintWriter writer = response.getWriter();if (user != null){//成功writer.write("登录成功");}else{//失败writer.write("登录失败");}
}
register:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {final String username = request.getParameter("username");final String email = request.getParameter("email");final String password = request.getParameter("password");response.setContentType("text/html;charset=utf-8");//查询//1.获取SqlSessionFactory对象final SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
//2.获取SqlSession对象final SqlSession sqlSession = sqlSessionFactory.openSession();//3.获取Mapperfinal UserMapper mapper = sqlSession.getMapper(UserMapper.class);
final PrintWriter writer = response.getWriter();//4.调用方法
if (mapper.selectByUsername(username) != null){System.out.println("1");writer.write("注册失败:用户名已存在");}else if (mapper.selectByEmail(email) != null){System.out.println("2");writer.write("注册失败:邮箱已使用");}else {mapper.add(username, email, password);sqlSession.commit();writer.write("注册成功");}
//5.释放资源sqlSession.close();
}
核心就这点,结束