package Servlet.controller;
import Servlet.service.UserServlce;
import Servlet.vo.MessageModel;
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;
/**
* @Author 陈平安
* @Date 2022/6/10 8:41
* @PackageName:Servlet.controller
* @ClassName: UserServlet
* @Description: TODO
* @Version 1.0
*/
@WebServlet("/login")//标记,login.jsp的接收数据处理的Java代码
public class UserServlet extends HttpServlet {
private UserServlce userServlce = new UserServlce();//实例化servlce层,进行后面的
//调用的servlce的逻辑调用
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uname = request.getParameter("uname");//接收前台数据
String pwd= request.getParameter("upwd");//接收前台数据
MessageModel messageModel = userServlce.userLogin(uname,pwd);//正式开始调用servlce的逻辑判断并且存储数据,方便后期返回数据,进行数据的判断
if (messageModel.getCode()==1){//表示登陆成功
request.getSession().setAttribute("user",messageModel.getObject());//将用户名存入session中
response.sendRedirect("index.jsp");//重回定向到index。jsp界面
}else {//登陆失败
request.setAttribute("messageModel",messageModel);//数据回显。因为messagemodel就是用来存放登录信息和状态码,方便登录失败后进行数据回显
request.getRequestDispatcher("login.jsp").forward(request,response);//请求转发到login.jsp
}
}
}
//接受请求,响应数据
//调用servlce的方法进行逻辑判断
package Servlet.entity;
/**
* @Author 陈平安
* @Date 2022/6/10 8:24
* @PackageName:com.xxxx.entity
* @ClassName: User
* @Description: TODO
* @Version 1.0
*/
public class User {
private Integer userid;
private String username;
private String userpwd;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
}
package Servlet.mapper;
import Servlet.entity.User;
/**
* @Author 陈平安
* @Date 2022/6/10 8:26
* @PackageName:com.xxxx.mapper
* @ClassName: UserMapper
* @Description: TODO
* @Version 1.0
*/
public interface UserMapper {
public User queryUserByName(String username);
}
<?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">
<!-- 首先来解析 namespace: 命名空间,此属性通常用来映射Dao(Mapper)层接口. -->
<mapper namespace="Servlet.mapper.UserMapper">
<!-- id: 对应Dao(Mapper)层接口方法名 parameterType:指定输入参数类型 -->
<!-- useGeneratedKeys="true"把新增加的主键赋值到自己定义的keyProperty(id)中 -->
<select id="queryUserByName" parameterType="String" resultType="Servlet.entity.User">
select * from tbb_user where username = #{username}
</select>
</mapper>
UserMapper.xml
package Servlet.service;
import Servlet.entity.User;
import Servlet.mapper.UserMapper;
import Servlet.util.GetSqlSession;
import Servlet.util.StringUtil;
import Servlet.vo.MessageModel;
import org.apache.ibatis.session.SqlSession;
/**
* @Author 陈平安
* @Date 2022/6/10 8:41
* @PackageName:Servlet.service
* @ClassName: UserServlce
* @Description: TODO
* @Version 1.0
*/
//逻辑判断
public class UserServlce {
public MessageModel userLogin(String uname, String pwd) {//方法
MessageModel messageModel = new MessageModel();//实例化信息模型
User u = new User();//数理化user
u.setUsername(uname);//将用户的姓名和密码存放到user中
u.setUserpwd(pwd);
messageModel.setObject(u);
if (StringUtil.isEmpty(uname) || StringUtil.isEmpty(pwd)) {//以下都是逻辑判断
messageModel.setCode(0);
messageModel.setMsg("用户或者密码不能为空");
return messageModel;
}
SqlSession session = GetSqlSession.createSqlSession();
// 得到对应Mapper
UserMapper userMapper = session.getMapper(UserMapper.class);
// 调用方法,返回用户对象
User user = userMapper.queryUserByName(uname);//调用数据库方法进行密码判断
if (user == null){
messageModel.setCode(0);
messageModel.setMsg("用户不存在");
return messageModel;
}
if (!pwd.equals(user.getUserpwd())){
messageModel.setCode(0);
messageModel.setMsg("密码错误");
return messageModel;
}
messageModel.setObject(user);//将用户对象存放到信息模型中,
return messageModel;//返回信息模型,方便调用
}
}
package Servlet.test;
import Servlet.entity.User;
import Servlet.mapper.UserMapper;
import Servlet.util.GetSqlSession;
import org.apache.ibatis.session.SqlSession;
/**
* @Author 陈平安
* @Date 2022/6/10 8:29
* @PackageName:com.xxxx.test
* @ClassName: test
* @Description: TODO
* @Version 1.0
*/
public class test {
public static void main(String[] args) {
SqlSession session = GetSqlSession.createSqlSession();
// 得到对应Mapper
UserMapper userMapper = session.getMapper(UserMapper.class);
// 调用方法,返回用户对象
User user = userMapper.queryUserByName("admin");
System.out.println(user);
}
}
package Servlet.util;
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 java.io.IOException;
import java.io.InputStream;
public class GetSqlSession {
/**
* 获取SqlSession对象
* @return
*/
public static SqlSession createSqlSession () {
SqlSessionFactory sqlSessionFactory = null;
InputStream input = null;
SqlSession session = null;
try {
// 获得mybatis的环境配置文件
String resource = "mybatis-config.xml";
// 以流的方式获取recource(mybatis的环境配置文件)
input = Resources.getResourceAsStream(resource);
// 创建会话工厂
sqlSessionFactory = new SqlSessionFactoryBuilder().build(input);
// 通过工厂得到SqlSession
session = sqlSessionFactory.openSession();
return session;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
System.out.println(createSqlSession());
}
}
package Servlet.util;
/**
* 字符串工具类
*/
public class StringUtil {
/**
* 判断字符串是否为空
* 如果为空,返回true;
* 如果不为空,返回false
* @param str
* @return
*/
public static boolean isEmpty(String str) {
if (str == null || "".equals(str.trim())) {
return true;
}
return false;
}
}
package Servlet.vo;
/**
* @Author 陈平安
* @Date 2022/6/10 8:39
* @PackageName:Servlet.vo
* @ClassName: MessageModel
* @Description: TODO
* @Version 1.0
*/
public class MessageModel {
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
private Integer code = 1;
private String msg = "成功";
private Object object;
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--
properties 标签中的配置可以供整个配置文件使用,在任何位置都可以引入其中配置的值。
properties 标签可以通过子标签 property 标签来配置一些子元素信息,也可以配置外部的动态文件。
-->
<properties resource="mysql.properties"/> <!-- 也可以配置url,但url和resource只能存在一个 -->
<!-- 类型别名 -->
<!--<typeAliases>
<typeAlias alias="User" type="User" />
</typeAliases>-->
<!-- 对事务的管理和连接池的配置 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- mappers映射器 -->
<mappers>
<!-- 映射文件方式1,一个一个的配置 -->
<!--<mapper resource="com/xxxx/mapper/UserMapper.xml"/>-->
<!--<mapper class="UserMapper"/>-->
<!-- 映射文件方式2,自动扫描包内的Mapper接口与配置文件 -->
<package name="Servlet.mapper"/>
</mappers>
</configuration>
mybates-config.xml
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/exam?serverTimezone=UTC&useSSL=false&characterEncoding=UTF-8
username=root
password=cjd
my.properties
<%--
Created by IntelliJ IDEA.
User: ${陈平安}
Date: 2022/6/10
Time: 8:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>欢迎${user.username}登录成功!</h2>//从session取值
</body>
</html>
index.jsp
<%--
Created by IntelliJ IDEA.
User: ${陈平安}
Date: 2022/6/10
Time: 8:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div style="text-align: center">
<form action="login" method="post" id="login">
姓名:<input type="text" name="uname" id="uname" value="${ messageModel.object.username}"> <br>
密码:<input type="password" name="upwd" id="upwd"value="${messageModel.object.userpwd}"> <br>
<span id="msg" style="font-size: 12px;color: red">${ messageModel.msg}</span> <br>
<button type="button" id="btn">登录</button>
<button type="button">注册</button>
</form>
</div>
</body>
<script type="text/javascript" src="js/jquery-3.4.1.js"></script>
<script type="text/javascript">
$("#btn").click(function () {
var uname = $("#uname").val();
var pwd = $("#upwd").val();
if (isEmpty(uname)) {
$("#msg").html("用户不能为空");
return
}
if (isEmpty(pwd)) {
$("#msg").html("密码不能为空");
return
}
$("#login").submit();
});
function isEmpty(string) {
if (string == null || string.trim() == "") {
return true;
}
return false;
}
</script>
</html>
login.jsp