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

一.项目需求

使用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 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 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.创建接口的映射文件。




        

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 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 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.配置文件

数据源的配置:




    
    
    

    
        
        
        
        
    

    
    
        
        
        
        
        
        
    

    
        
        
    

Mybatis配置:





    
    
        
    

Spring配置:




    

    
    

    
    

SpringMVC配置:




    
    
    
        
        
    

    
    
    
    
    



Web.xml配置:





    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter

        
        
            encoding
            UTF-8
        

        
            forceEncoding
            true
        

    

    
        encodingFilter
        /*
    

    
    

        contextConfigLocation

        classpath:applictionContext.xml

    

    
    
        
        org.springframework.web.context.ContextLoaderListener
    

    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:springMVCContext.xml
        
    

    
        springmvc
        /
    

    
    
        default
        *.jpg
    

 

你可能感兴趣的:(Spring+SpringMVC+Mybatis实现简单的用户的登录和退出)