关于Spring+SpringMVC+MyBatis+Mysql+Maven实现简易的登录操作(详细)

环境:jdk1.8  Tomcat8.5

文件的结构

关于Spring+SpringMVC+MyBatis+Mysql+Maven实现简易的登录操作(详细)_第1张图片

关于Spring+SpringMVC+MyBatis+Mysql+Maven实现简易的登录操作(详细)_第2张图片

1、pom.xml配置文件


  4.0.0
  SSM-Login
  SSM-Login
  0.0.1-SNAPSHOT
  war
  
  
  	
	
	    org.springframework
	    spring-context
	    5.1.5.RELEASE
	
	
	
	
	    org.springframework
	    spring-webmvc
	    5.1.5.RELEASE
	
	
	
	
	    mysql
	    mysql-connector-java
	    5.1.47
	
	
	
	
	    com.fasterxml.jackson.core
	    jackson-databind
	    2.9.8
	
	
	
	
	
	    org.apache.logging.log4j
	    log4j-core
	    2.11.2
	
	
	
	
	
	    javax.servlet
	    javax.servlet-api
	    3.0.1
	    provided
	
	
	
	
	
	
	
	    org.mybatis
	    mybatis-spring
	    2.0.0
	
	
	
	
	
	
	    com.alibaba
	    druid
	    1.1.14
	
	
	
	
	
	    org.mybatis
	    mybatis
	    3.5.0
	
	
	
	
	
	    org.aspectj
	    aspectjweaver
	    1.9.2
	
	
	
	
	
	    org.springframework
	    spring-tx
	    5.1.5.RELEASE
	
	
	
	
	
	
	    org.springframework
	    spring-jdbc
	    5.1.5.RELEASE
	
	
	
  	
  
  
  
  

		

			

				org.apache.maven.plugins
				maven-compiler-plugin
				3.8.0
				
					1.8
					1.8
				

			

		

	
  
  

2、web.xml配置文件



	SSM-Login
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	


	
	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
		
			forceEncoding
			true
		
	
	
		encodingFilter
		/*
	

	
	
		org.springframework.web.context.ContextLoaderListener
	

	
		contextConfigLocation
		/WEB-INF/spring-mybatis.xml
	
	
	
		springmvc
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			/WEB-INF/spring-servlet.xml
		
		1
	

	
		springmvc
		/
	


3、spring-servlet.xml配置文件



		
	
	
	
	
	
	
	
	
	
	
	
    
	

4、spring-mybatis.xml配置文件




	


	
	
		
		
		

		

		
		
		
		

		
		
		
		
		
		
		

		
		

		
	

	
	
		
		

	

	
	
		
	

	
	
		
	
	
	
		
		
			
			
			
			
		

	

	
	
		
		
	
   

5、log4j2.xml配置



	
		
			
		
	
	
		
			
		
		
			
		
	

6、db.properties配置(数据库)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/+“数据库库名”
jdbc.user=数据库用户名
jdbc.password=数据库密码

7、login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




登录界面




	
用户名:
密码:

8、LoginController.java

package com.tnr.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tnr.model.User;
import com.tnr.service.LoginService;

@Controller
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	
	@RequestMapping("/login")
	public String LoginDo(User user) {

		User us= loginService.checkLogin(user);
		if(us==null) {
			System.out.println(">>>>");
			return "/login.jsp";
		}else {
			System.out.println("<<<<<<");
			return "redirect:/index.jsp";
		}
	}

}

9、LoginService.java

package com.tnr.service;

import org.springframework.stereotype.Service;

import com.tnr.model.User;
@Service
public interface LoginService {
	
	
	User checkLogin(User user);

}

10、LoginServiceImpl.java

package com.tnr.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tnr.mapper.UserMapper;
import com.tnr.model.User;
import com.tnr.service.LoginService;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {
	
	@Autowired
	private UserMapper usermapper;

	@Override
	public User checkLogin(User user) {
		
		return usermapper.Login(user);
	}

}

11、实体类User.java

package com.tnr.model;

public class User {
	
	private String u_id;
	private String u_name;
	private String u_pwd;
	
	
	public String getU_id() {
		return u_id;
	}
	public void setU_id(String u_id) {
		this.u_id = u_id;
	}
	public String getU_name() {
		return u_name;
	}
	public void setU_name(String u_name) {
		this.u_name = u_name;
	}
	public String getU_pwd() {
		return u_pwd;
	}
	public void setU_pwd(String u_pwd) {
		this.u_pwd = u_pwd;
	}
	
	@Override
	public String toString() {
		return "User [u_id=" + u_id + ", u_name=" + u_name + ", u_pwd=" + u_pwd + "]";
	}
	
}

12、UserMapper.java

package com.tnr.mapper;


import org.apache.ibatis.annotations.Select;

import com.tnr.model.User;

public interface UserMapper {
	
	@Select("select * from us where u_id=#{u_id} and u_pwd=#{u_pwd}")
	public User Login(User user);

}

 

你可能感兴趣的:(利用SSM实现简单的登录)