SSM(SpringMVC Spring Mybatis)三大框架从零开始搭建实例

SSM(SpringMVC Spring Mybatis)三大框架从零开始搭建实例
1、新建一个web工程,带有web.xml文件

2、第二步,将ssm框架搭建所需要的jar包复制到lib目录下,全部选中,Add-to-Path,加入到工程下
SSM(SpringMVC Spring Mybatis)三大框架从零开始搭建实例_第1张图片

3、配置web.xml文件:


	qzone
	
		index.jsp
	
	
	
		contextConfigLocation
		classpath:applicationContext.xml
	
	
	
		org.springframework.web.context.ContextLoaderListener
	

	
	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		true
		
			encoding
			UTF-8
		
	
	
		encodingFilter
		/*
	
	
	
	
		springMVC
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
		1
		true
	
	
		springMVC
		*.do
	

4、在src下新建applicationContext.xml配置文件:



	
	

	
	
		
		
		
		
	

	
	
		
		
	

	
	
		
		
		
		
		
	
	
	
	
		
		
	
	
	
	
		
	
	
	
	
		
		
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		
	
	

5、在src下新建log4j.properties文件:
log4j.rootLogger=info,appender1,appender2
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
log4j.appender.appender2=org.apache.log4j.FileAppender
log4j.appender.appender2.File=D:/logFile.txt
log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout

6、在src下新建mybatis-config.xml配置文件:使得我们可以在UserMapper.xml中直接使用User,而不用写全名
com.etc.entity.User,使得代码更为简便。



	
	
		
	
如果想要指定别名的名称,如下配置:



	
	
		
	
7、在src下新建spring-mvc.xml配置文件:


	
	
	
	
		
		
	


8、在src下新建com.etc.entity包,在包下新建User.java类:
package com.etc.entity;
/**
 * Descript:用户实体类
 * @author zoey
 * @date 2017年8月14日
 */
public class User {
	private Integer id;
	private String username;
	private String password;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

9、在src下新建com.etc.dao包,在包下新建UserDao接口:
package com.etc.dao;

import com.etc.entity.User;

public interface UserDao {
	public User login(User user);
}

10、在src下新建com.etc.mappers包,在包下新建UserMapper.xml文件:



	
		
		
		
	
	
	

11、在src下新建com.etc.service包,在包下新建UserService.java类:
package com.etc.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.etc.dao.UserDao;
import com.etc.entity.User;
import com.etc.service.UserService;
@Service
public class UserService{
	@Resource
	private UserDao userDao;
	public User login(User user) {
		return userDao.login(user);
	}
}

11、在src下新建com.etc.controller包,在包下新建UserController.java类:
package com.etc.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.etc.entity.User;
import com.etc.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	
	@RequestMapping("/login")
	public String login(User user,HttpServletRequest request){
		User resultUser = userService.login(user);
		if(resultUser == null){
			request.setAttribute("user", user);
			request.setAttribute("errorMsg","用户名或密码错误");
			return "index";
		}else{
			HttpSession session = request.getSession();
			session.setAttribute("currentUser",user);
			return "redirect:/success.jsp";
		}
	}
}

12、编写index.jsp页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
    
  
  	
username: password: ${errorMsg }

13、编写success.jsp页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
   	欢迎${currentUser.username }
  


14、运行项目如下:http://localhost:8080/qzone/index.jsp
SSM(SpringMVC Spring Mybatis)三大框架从零开始搭建实例_第2张图片

SSM(SpringMVC Spring Mybatis)三大框架从零开始搭建实例_第3张图片
至此:ssm框架搭建全部完成,简单实例也已经成功实现。
补充:
数据库信息如下:
SSM(SpringMVC Spring Mybatis)三大框架从零开始搭建实例_第4张图片

项目总体框架图如下:
SSM(SpringMVC Spring Mybatis)三大框架从零开始搭建实例_第5张图片

ps:这个登录实例实现的方法不唯一,可以使用其他方式完成。

你可能感兴趣的:(ssm)