Mybatis——简单的登录验证

Mybatis——简单的登录验证

1、mybatis-连接数据库。
connection-config.xml:

<?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 resource="jdbc.properties"/>
    <!--1.配置环境 ,默认的环境id为mysql-->
    <environments default="mysql">
        <!--1.2.配置id为mysql的数据库环境 -->
        <environment id="mysql">
            <!-- 使用JDBC的事务管理 -->
            <transactionManager type="JDBC"  />
            <!--数据库连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8" />
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--2.配置Mapper的位置 -->
    <mappers>
    <mapper resource="com/controller/GetUserMapper.xml"/>
    </mappers>
</configuration>

jdbc.propertiesSQl数据文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123456

2工具类:

public class MybatisUtil {
    private  static SqlSessionFactory sqlSessionFactory=null;
    static {
        try {
            String resource = "application-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //2.根据配置文件构建sqlSessionFactory
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //3.构建一个会话 创建SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public  static  SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

3拦截器:springmvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.controller"/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/><!--拦截路径(所有的请求)-->
            <!--    <mvc:exclude-mapping path=""/>&lt;!&ndash;不需要拦截的路径&ndash;&gt;-->
            <bean class="com.controller.MuIntercePtor"/>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

4、响应体:

public class MuIntercePtor implements HandlerInterceptor {

    @Override
    public boolean preHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("interceptTwo正在执行prehandel方法111");
        String url=httpServletRequest.getRequestURI();//获取访问路径
        if(url.indexOf("/index")>=0){
            return true;
        }
        HttpSession httpSession=httpServletRequest.getSession();
        User user= (User) httpSession.getAttribute("myuser");
        if(user!=null){
            System.out.println(user.getUsername()+"我是用户名");
            return true;
        }
        httpServletRequest.setAttribute("msg","你没有登录请。。。");
        httpServletRequest.getRequestDispatcher("/index.jsp").forward(httpServletRequest,httpServletResponse);
        return false;
    }
    @Override
    public void postHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}

5、web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

pom.xml的配置:
配置xml文件在非resoult目录下被识别

<resources>
  <resource>
    <directory>src/main/java</directory>
    <includes>
      <include>**/*.xml</include>
    </includes>
  </resource>
</resources>

前后端的数据校验

@Controller
public class UserController {
        MybatisUtil mybatisUtil=new MybatisUtil();
        SqlSession sqlSession= mybatisUtil.getSqlSession();
        GetUserMapper getUserMapper=sqlSession.getMapper(GetUserMapper.class);
        List<User> userList= (List<User>) getUserMapper.findUser();
@RequestMapping("index")
    public String MyConctroller(User user, Model model, HttpSession httpSession){
String id=user.getId();
String username=user.getUsername();
    System.out.println("id:"+id+"username:"+username);
for(User user1:userList){
    if(username!=null&&username.equals(user1.getUsername())&&id.equals(user1.getId())&&id!=null){
        httpSession.setAttribute("myuser",user);
        return "succes";
    }
}
    model.addAttribute("msg","密码或者账号错误!"+user.toString());
    return "index";
    }
    @RequestMapping("succes")
    public String tiMain(){
        return "succes";
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值