SSM(Mybatis + Spring + Spring MVC)框架整合详细步骤(附jar包和项目下载,免费的)

本例从0开始逐一整合SSM的步骤,实现基础的CRUD

开发环境:Eclipse4.6 + jdk1.8 + Tomcat8 + MySQL

SSM所需jar包和项目下载路径在文章最后

1.数据库

       创建test数据库 ,file表

use test;

CREATE TABLE `file` (
  `id` bigint(2) NOT NULL AUTO_INCREMENT,
  `userId` bigint(2) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `path` varchar(255) DEFAULT NULL,
  `size` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=UTF8;

2.准备数据

INSERT INTO `file` VALUES 
('1', '10', 'tp1.jpg', '/Library/Tomcat/apache-tomcat-8.5.31/webapps/uploadDownloadFile/upload/tp1.jpg', '550714'), 
('2', '7', '??????.xls', '/Library/Tomcat/apache-tomcat-8.5.31/webapps/uploadDownloadFile/upload/??????.xls', '76800'), 
('3', '7', '3222.json', '/Library/Tomcat/apache-tomcat-8.5.31/webapps/uploadDownloadFile/upload/3222.json', '135482'), 
('4', '7', 'tp2.jpg', '/Library/Tomcat/apache-tomcat-8.5.31/webapps/uploadDownloadFile/upload/tp2.jpg', '446418'), 
('5', '1', 'tp7.jpg', '/Library/Tomcat/apache-tomcat-8.5.31/webapps/uploadDownloadFile/upload/tp7.jpg', '469380');

3.新建项目

       在eclipse中新建项目uploadDownloadFile,选择Dynamic Web Project的方式。

4.导入jar包

       将所需jar包复制到uploadDownloadFile/WebContent/WEB-INF/lib目录下(文章最后有jar包下载地址)。

5.domain    File

package com.file.domain;

public class File {
	
	private int id;
	private int userId;
	private String name;
	private String path;
	private int size;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	
	@Override
	public String toString() {
		return "File [id=" + id + ", userId=" + userId + ", name=" + name + ", path=" + path
				+ ", size=" + size + "]";
	}

}

6.FileMapper

package com.file.mapper;

import java.util.List;

import com.file.domain.File;

public interface FileMapper {
	
public int insert(File file);  
    
    public void delete(int id);  
       
    public File get(int id);  
     
    public int update(File file);   
       
    public List getAll();
    
}

7.FileMapper.xml




	
	    
	        insert into file (userId,name,path,size) values (#{userId},#{name},#{path},#{size})    
	    
	    
	    
	        delete from file where id= #{id}   
	    
	    
	    

	    
	        update file set userId=#{userId},name=#{name},path=#{path},size=#{size} where id=#{id}    
	    
	    	    
	

8.FileService

package com.file.service;

import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import com.file.domain.File;

public interface FileService {
	
	void insert(MultipartFile file, String path);
	
	void delete(int id);
	
	File get(int id);
	
	List getAll();
	
}

9.FileServiceImpl

package com.file.service.impl;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.file.domain.File;
import com.file.mapper.FileMapper;
import com.file.service.FileService;

@Service
public class FileServiceImpl implements FileService {
	
	@Autowired
	private FileMapper fileMapper;

	@Override
	public void insert(MultipartFile file, String path) {
		File f = new File();
		int userId = new Random().nextInt(10) + 1;
		f.setName(file.getOriginalFilename());
		f.setPath(path);
		f.setSize((int)file.getSize());
		f.setUserId(userId);
		fileMapper.insert(f);
	}

	@Override
	public void delete(int id) {
		fileMapper.delete(id);
	}

	@Override
	public File get(int id) {
		return fileMapper.get(id);
	}

	@Override
	public List getAll() {
		return fileMapper.getAll();
	}

}

10.FileController

package com.file.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.file.service.FileService;

// 告诉spring mvc这是一个控制器类
@Controller
@RequestMapping("")
public class FileController {
	@Autowired
	FileService fileService;
	
	@RequestMapping("listFile")
	public ModelAndView listCategory(){
		ModelAndView mav = new ModelAndView();
		List fs= fileService.getAll();
		
		// 放入转发参数
		mav.addObject("fs", fs);
		// 放入jsp路径
		mav.setViewName("listFile");
		return mav;
	}

}

11.web.xml



  uploadDownloadFile
  
  	
	
		contextConfigLocation
		classpath:applicationContext.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
	
		mvc-dispatcher
		org.springframework.web.servlet.DispatcherServlet
		
		
			contextConfigLocation
			classpath:springMVC.xml
		
		1
	
	
		mvc-dispatcher
		/
	
  

12. 在项目中新建名为resource的Source Folder文件,在该目录下新建db.properties文件、log4j.properties文件、mybatis-config.xml,分别是数据库、日志、mybatis的配置文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=admin
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.file=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n




	
	
	
	
		
	
	

13.applicationContext.xml

          在resource目录下新建applicationContext.xml文件,这是Spring的配置文件,其作用已在代码中注释



	
	
	
	
	
	
	
	
		
		
		
		
	
	
	
	
		
		
		
		
	

	
	
		
	
	
	
	
		
	

14.在resource目录下新建springMVC.xml文件




	
    
    

	
    
    
    
    

	
    
        
        
        
    
    

15.listFile.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"%>
 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 



文件列表


	
	        
id userId name path size
${c.id} ${c.userId} ${c.name} ${c.path} ${c.size}

16.部署成功之后测试地址:http://localhost:8080/uploadDownloadFile/listFile

SSM(Mybatis + Spring + Spring MVC)框架整合详细步骤(附jar包和项目下载,免费的)_第1张图片

17.jar包、项目下载路径

jar包:链接:https://pan.baidu.com/s/1VD5mz_9fBFpPNaKJJVGuQg  密码:7d0d

项目:链接:https://pan.baidu.com/s/1cu6F61e9xXQDZk4iDif1hQ  密码:111i

你可能感兴趣的:(Java框架,Spring系列,SSM)