django Transformer wpf post pyspark gulp vue教程 git视频教程 mac虚拟打印机 android调试工具 js获取body的高度 idea开发python python开发教程 python日期转时间戳 java数据库连接 java系统时间 java字符串格式化 java线程死锁 java版本查看 java类方法 linux基础教程 linux的find flash实例 backtrack3 脚本大全 c语言指数函数 橄榄山快模 华为下拉开关设置 R语言初学者指南 apihook 极速傲引擎 远程桌面管理工具 maya骨骼绑定 class选择器 php单例模式 python进程池 电脑防护软件 迅雷被封 工行天天益 实时渲染软件
当前位置: 首页 > 学习教程  > 编程语言

Spring+SpringMVC+Mybatis实现简单的用户的登录和退出

2020/7/24 9:37:00 文章标签:

一.项目需求

使用SSM框架实现简单的用户的登录和退出,如果输入的用户名正确的话,将跳转到登录成功的界面,否则跳转到登录界面,并显示错误信息。

二.解决步骤

2.1.创建web工程导入相关的jar包。

2.2.创建数据访问层的接口和实现类。

2.3.创建接口的映射文件。

2.4.创建业务逻辑层的接口以及实现类。

2.5.创建测试程序,验证程序的逻辑是否正确。

2.6.编写控制器类实现页面的跳转。

三.代码实现.

3.1项目相关依赖包的下载.

链接:https://pan.baidu.com/s/1VEXbmo85CKQDPWBiMAcKsA 
提取码:zih8

3.2 创建数据访问层的接口和实现类。

接口:

package com.atguigu.ssm.dao;

import com.atguigu.ssm.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

/**
 * 数据访问层的接口
 * @author sd
 * @date 2020-07-21 20:19
 */
public interface UserDao {
    //    根据用户的名称和密码来进行登录
    public User queryByUserNameAndPassword(@Param("username") String username, @Param("password") String password);

    // 查询所有的用户列表
    public List<User> getUserList(String username,Integer roleId);

    //查看某个人的信息
    public User getUserById(Integer id);


    //修改某个人的信息根据id
    public int updateByUserId(Integer id, String namess, String passwords, Integer roles, String gender, Date birthday);

    //添加用户
    public Boolean add(User user);

// 删除用户

    public int deleteById(Integer id);
}

实现类:

package com.atguigu.ssm.dao.Impl;

import com.atguigu.ssm.dao.UserDao;
import com.atguigu.ssm.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author sd
 * @date 2020-07-23 14:13
 */
@Repository
public class UserDaoImpl implements UserDao {

//    定义SqlSessionTemplate
    private SqlSessionTemplate session;

    public SqlSessionTemplate getSession() {
        return session;
    }

    public void setSession(SqlSessionTemplate session) {
        this.session = session;
    }

    //    实现用户的登录
    @Override
    public User queryByUserNameAndPassword(String username, String password) {
        return session.getMapper(UserDao.class).queryByUserNameAndPassword(username,password);
    }

    @Override
    public List<User> getUserList(String username, Integer roleId) {
        return null;
    }

    @Override
    public User getUserById(Integer id) {
        return null;
    }

    @Override
    public int updateByUserId(Integer id, String namess, String passwords, Integer roles, String gender, Date birthday) {
        return 0;
    }

    @Override
    public Boolean add(User user) {
        return null;
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }
}

3.3.创建接口的映射文件。

<?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">
<mapper namespace="com.atguigu.ssm.dao.UserDao">
        <select id="queryByUserNameAndPassword" resultType="com.atguigu.ssm.pojo.User">
              SELECT u.namess,u.passwords from user u WHERE u.namess=#{username} AND u.passwords=#{password}
        </select>
</mapper>

3.4.创建业务逻辑层的接口以及实现类。

接口:

package com.atguigu.ssm.service;

import com.atguigu.ssm.pojo.User;

import java.util.Date;
import java.util.List;

/**
 * 数据访问层的接口
 * @author sd
 * @date 2020-07-21 20:19
 */
public interface UserService {
    //    根据用户的名称和密码来进行登录
    public User queryByUserNameAndPassword(String username, String password);

    // 查询所有的用户列表
    public List<User> getUserList(String username, Integer roleId);

    //查看某个人的信息
    public User getUserById(Integer id);


    //修改某个人的信息根据id
    public int updateByUserId(Integer id, String namess, String passwords, Integer roles, String gender, Date birthday);

    //添加用户
    public Boolean add(User user);

// 删除用户

    public int deleteById(Integer id);
}

实现类:

package com.atguigu.ssm.service.Impl;

import com.atguigu.ssm.dao.UserDao;
import com.atguigu.ssm.pojo.User;
import com.atguigu.ssm.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author sd
 * @date 2020-07-23 19:09
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User queryByUserNameAndPassword(String username, String password) {
        return userDao.queryByUserNameAndPassword(username,password);
    }

    @Override
    public List<User> getUserList(String username, Integer roleId) {
        return null;
    }

    @Override
    public User getUserById(Integer id) {
        return null;
    }

    @Override
    public int updateByUserId(Integer id, String namess, String passwords, Integer roles, String gender, Date birthday) {
        return 0;
    }

    @Override
    public Boolean add(User user) {
        return null;
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }
}

 

3.5.创建测试程序,验证程序的逻辑是否正确。

数据库测试:

package com.atguigu.ssm.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author sd
 * @date 2020-07-23 14:09
 */
public class TestConnection {
    @Test
    public void test1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applictionContext.xml");
        Object dataSource = context.getBean("dataSource");
        System.out.println(dataSource.toString());
    }
}

数据访问层实现类测试:

package com.atguigu.ssm.test;

import com.atguigu.ssm.dao.Impl.UserDaoImpl;
import com.atguigu.ssm.dao.UserDao;
import com.atguigu.ssm.pojo.User;
import org.junit.Test;

/**
 * @author sd
 * @date 2020-07-23 14:16
 */
public class TestUserDaoImpl {
    @Test
    public void test1(){
        UserDao userDao=new UserDaoImpl();
        User user = userDao.queryByUserNameAndPassword("admin", "ly@168");
        System.out.println(user.getNamess()+user.getPasswords());

    }
}

3.6.编写控制器类实现页面的跳转。

package com.atguigu.ssm.controller;

import com.atguigu.ssm.pojo.User;
import com.atguigu.ssm.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;

/**
 * @author sd
 * @date 2020-07-23 19:01
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private  UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

//    实现用户的登录
    @RequestMapping(value = "/logins")
    public String logins(String username, String password, Model model, HttpServletRequest request){
        User user = userService.queryByUserNameAndPassword(username, password);
        if (user == null){
            model.addAttribute("errors","用户名称或者密码不正确");
            return "logins";
        }else {
            request.getSession().setAttribute("user",user);
            return "success";
        }
    }

    @RequestMapping(value = "/clear")
    public String loginout(HttpServletRequest request){
        request.getSession().invalidate();
        return "logins";
    }

}

3.7.配置文件

数据源的配置:

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--配置数据源-->
    <!--使用读取配置文件的形式进行数据元的读取-->
    <context:property-placeholder location="classpath:db.properties"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--配置sessionFactry-->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--加载接口的映射文件-->
        <property name="mapperLocations" value="classpath:com/atguigu/ssm/dao/*.xml"/>
    </bean>

    <bean id="zhujie" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="SqlSessionFactory"></property>
        <property name="basePackage" value="com.atguigu.ssm.dao"/>
    </bean>

</beans>

Mybatis配置:

<?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>

    <!--给实体类起一个别名-->
    <typeAliases>
        <package name="com.atguigu.ssm.pojo"/>
    </typeAliases>
</configuration>

Spring配置:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <import resource="classpath:dataSourceContext.xml"/>

    <!--扫描数据访问层的注解-->
    <context:component-scan base-package="com.atguigu.ssm.dao"/>

    <!--配置自动扫描业务逻辑层的注解-->
    <context:component-scan base-package="com.atguigu.ssm.service"/>

</beans>

SpringMVC配置:

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--配置静态资源的访问-->
    <!--配置SpringMVC的前端控制器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--补充mvc的配置-->
    <mvc:annotation-driven/>
    
    <!--扫描指定包下面的注解-->
    <context:component-scan base-package="com.atguigu.ssm.controller"/>



</beans>

Web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">


    <!--添加过滤器处理程序的乱码-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

        <!--出入的参数的编码的标准-->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>

        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>

    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--加载Spring的配置文件-->
    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:applictionContext.xml</param-value>

    </context-param>

    <!--配置监听器-->
    <listener>
        <!--上下文的加载的监听-->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--配置拦截器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置初始化参数:加载SpringMVC的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVCContext.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置静态资源的默认的访问-->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>

</web-app>

 


本文链接: http://www.dtmao.cc/news_show_50137.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?