目标:
- 能够完成用户登录注册案例的实现
- 能够完成SqlSessionFactory工具类的抽取
接下来我们通过两个比较常见的案例,一个是注册,一个是登录来对今天学习的内容进行一个实战演练,首先来实现用户登录。
参考资料\1. 登陆注册案例\1. 静态页面
,拷贝完效果如下:
2.1 将资料\1. 登陆注册案例\2. MyBatis环境\tb_user.sql
中的sql语句执行下:
2.2 将资料\1. 登陆注册案例\2. MyBatis环境\User.java
拷贝到com.suki.pojo
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.6version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.29version>
dependency>
4.1 将资料\1. 登陆注册案例\2. MyBatis环境\mybatis-config.xml
拷贝到resources目录下
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.suki.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true"/>
<property name="username" value="root"/>
<property name="password" value="121121"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.suki.mapper"/>
mappers>
configuration>
4.2 在com.itheima.mapper包下创建UserMapper接口
public interface UserMapper {
}
4.3 将资料\1. 登陆注册案例\2. MyBatis环境\UserMapper.xml
拷贝到resources目录下
注意:在resources下创建UserMapper.xml的目录时,要使用/分割
至此我们所需要的环境就都已经准备好了,具体该如何实现?
/**
* @description 根据用户名和密码查询用户对象
* @param username
* @param password
* @return com.suki.pojo.User
*/
@Select("select * from tb_user where username = #{username} and password = #{password}")
User select(@Param("username")String username,@Param("password")String password);
说明
@Param注解的作用:用于传递参数,使方法的参数可以与SQL中的字段名相对应。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>logintitle>
<link href="css/login.css" rel="stylesheet">
head>
<body>
<div id="loginDiv">
<form action="/request-demo/loginServlet" method="post" id="form">
<h1 id="loginMsg">LOGIN INh1>
<p>Username:<input id="username" name="username" type="text">p>
<p>Password:<input id="password" name="password" type="password">p>
<div id="subDiv">
<input type="submit" class="button" value="login up">
<input type="reset" class="button" value="reset">
<a href="register.html">没有账号?点击注册a>
div>
form>
div>
body>
html>
nbsp的全称是:no breaking space,意为非截断空白符,该符号与前后单词合并为一个单词,不应该被截断
@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 password = request.getParameter("password");
// 2.调用MyBatis完成查询
//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 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);
}
}
4.1 如果用户名和密码输入错误,则
4.2 如果用户名和密码输入正确,则
至此用户的登录功能就已经完成了~
/**
* 根据用户名查询用户对象
* @param username
* @return
*/
@Select("select * from tb_user where username = #{username}")
User selectByUsername(String username);
/**
* 添加用户
* @param user
*/
@Insert("insert into tb_user values(null,#{username},#{password})")
void add(User user);
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="/request-demo/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.suki.web;
import com.suki.mapper.UserMapper;
import com.suki.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.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
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);
//提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}else{
//用户名存在,给出提示信息
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("用户名已存在");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
4.2 如果用户已经存在,则在页面上展示 用户名已存在
的提示信息
上面两个功能已经实现,但是在写Servlet的时候,因为需要使用Mybatis来完成数据库的操作,所以对于Mybatis的基础操作就出现了些重复代码,如下
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new
SqlSessionFactoryBuilder().build(inputStream);
有了这些重复代码就会造成一些问题:
那如何来优化呢?
有了这两个方向后,代码具体该如何编写?
public class SqlSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
//静态代码块会随着类的加载而自动执行,且只执行一次
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
工具类抽取以后,以后在对Mybatis的SqlSession进行操作的时候,就可以直接使用
SqlSessionFactory sqlSessionFactory =SqlSessionFactoryUtils.getSqlSessionFactory();
这样就可以很好的解决上面所说的代码重复和重复创建工厂导致性能低的问题了。
结合上一节学的内容,post请求参数中文乱码情况,在doGet中设置字符输入流的编码:
request.setCharacterEncoding("UTF-8");
学习笔记 from 黑马程序员
By – Suki 2023/3/2