1.pojo:存放项目涉及得到实体类,实体的属性和setter,getter方法
package com.pojo;
public class Image {
private int id;
private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
2.dao:存放对数据库操作的接口
package com.dao;
import com.pojo.Image;
import java.util.List;
public interface ImageDao {
int add(String content);
int deleteById(int id);
List queryAllImage();
String queryImageById(int id);
}
3.service:存放对pojo实体类的操作接口,与dao接口相对应
package com.service;
import com.pojo.Image;
import java.util.List;
public interface ImageService {
List queryAllImage();
int add(Image image);
int deleteById(int id);
String queryImageById(int id);
}
4.service下的impl包:存放service下接口的实现类,每个方法返回对dao中接口方法的调用,注意注解不要丢了
package com.service.impl;
import com.dao.ImageDao;
import com.pojo.Image;
import com.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ImageServiceImpl implements ImageService {
@Autowired
private ImageDao imageDao;
@Override
public int add(Image image) {
return imageDao.add(image.getContent());
}
@Override
public int deleteById(int id) {
return imageDao.deleteById(id);
}
@Override
public List queryAllImage() {
return imageDao.queryAllImage();
}
public String queryImageById(int id) {
return imageDao.queryImageById(id);
}
}
5.mapper:对dao中方法的实现,对数据库进行增删改除操作,具体细节可以查看MyBatis教程
INSERT INTO image(image_content)
VALUES (#{content})
delete from image where image_id = #{id}
6.配置文件
spring-dao.xml
spring-mvc.xml
spring-service.xml
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/papersystem?useSSL=false&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=password
log4j.properties
log4j.rootLogger=ERROR, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
mybatis-congif.xml
6.jsp页面
index.jsp
<%--JSP指令page:定义页面的依赖属性--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%--隐含对象pageContext:PageContext类的实例,提供对JSP页面所有对象以及命名空间的访问--%>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
首页
<%--bootstrap网格的基本结构--%>
<%--clearfix清除浮动--%>
基于SSM框架的管理系统:简单实现增、删、改、查。
点击进入管理页面
allImage.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/2/22
Time: 15:56
To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
<%@ page isELIgnored="false" %>
allImage
addImage.jsp
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2018/4/7
Time: 16:45
To change this template use File | Settings | File Templates.
--%>
<%--JSP指令taglib:引入标签库的定义,可以是自定义标签,下面是引入核心标签库--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
out.print(path);
/*path = ""*/
/*basepath=http://localhost:8080/*/
%>
新增论文
基于SSM框架的管理系统:简单实现增、删、改、查。
新增论文
7.controller
imageController
package com.controller;
import com.pojo.Image;
import com.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;
@Controller
@RequestMapping("/image")
public class ImageController {
@Autowired
private ImageService imageService;
@RequestMapping("/allImage")
public String queryAllImage(Model model) {
List list1 = imageService.queryAllImage();
System.out.println(list1.size());
for (Image i:list1
) {
System.out.println(i.getContent()+","+i.getId());
}
model.addAttribute("list1", list1);
return "allImage";
}
@RequestMapping("/addImage")
public String addImage() {
return "addImage";
}
@RequestMapping(value="/doAddImage",method = RequestMethod.POST)
public String doAddImage(@RequestParam(value = "file") MultipartFile[] file, Image image, HttpServletRequest request) throws IOException {
/**
* 图片上传到项目
*/
/*
//上传后的地址,注意("/upload")是表示文件上传后的目标文件夹
String realPath = request.getSession().getServletContext().getRealPath("/upload");
System.out.println("=========打印文件上传的路径"+realPath);
for (int i = 0; i < file.length; i++) {
//获取文件名
String filename = file[i].getOriginalFilename();
//获取文件后缀名
String extensionname = filename.substring(filename.lastIndexOf(".") + 1);
//给上传的文件起别名,有很多种方式
String newFilename=String.valueOf(System.currentTimeMillis())+"."+extensionname;
//创建File对象,传入目标路径参数,和新的文件别名
File dir=new File(realPath,newFilename);
if (!dir.exists()){//如果dir代表的文件不存在,则创建它,
dir.mkdirs();//
}
//如果存在则直接执行下面操作
file[i].transferTo(dir);//将上传的实体文件复制到指定目录upload下
image.setContent(newFilename);//将文件名赋值给实体类,然后一并存到数据库
imageService.add(image);//调用后台方法,将对象存进数据库
}
*/
/**
* 图片上传到本地
*/
//图片上传成功后,将图片的地址写到数据库
String filePath = "d:\\myImage1";
for (int i = 0; i < file.length; i++) {
//获取文件名
String filename = file[i].getOriginalFilename();
//获取文件后缀名
String extensionname = filename.substring(filename.lastIndexOf(".") + 1);
//给上传的文件起别名,有很多种方式
String newFileName=String.valueOf(System.currentTimeMillis())+"."+extensionname;
//封装上传文件位置的全路径,就是硬盘路径+文件名
File targetFile = new File(filePath,newFileName);
System.out.println("!!!!!!!========="+targetFile);
if (!targetFile.exists()){//如果dir代表的文件不存在,则创建它,
targetFile.mkdirs();
}
//把本地文件上传到已经封装好的文件位置的全路径就是上面的targetFile
file[i].transferTo(targetFile);
image.setContent(newFileName);//文件名保存到实体类对应属性上
//保存商品
imageService.add(image);
}
return "redirect:/image/allImage";
}
/*文件下载*/
@RequestMapping("/down/{id}")
public void download(@PathVariable("id") int id, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//根据选择文件的编号,查找到相应的文件名字
String beginname = imageService.queryImageById(id);
System.out.println("@@@@@@@@@@@@@@@@@@@0" + beginname);
//生成文件的存储位置
String fileName = "d:\\myImage1"+"/"+ beginname;
System.out.println("!!!!!!!!!!!!!!!!!" + fileName);
//封装为完整的文件路径
File file = new File(fileName);
//如果文件存在的话
if (file.exists()) {
//获取输入流
InputStream bis = new BufferedInputStream(new FileInputStream(file));
//假如以中文名下载的话
String filename = beginname;
//转码,免得文件名中文乱码
filename = URLEncoder.encode(filename, "UTF-8");
//设置文件下载头
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
/*getOutputStream获取子程序的输出流*/
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
int len = 0;
while ((len = bis.read()) != -1) {
out.write(len);
/*将缓冲区中的数据写入到输出流中*/
out.flush();
}
out.close();
} else {
//下载的文件不存在
request.setAttribute("errorResult", "文件不存在下载失败!");
RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/01.jsp");
dispatcher.forward(request, response);
}
}
/*根据文件序号删除指定的文件*/
@RequestMapping("/delete/{id}")
public String deleteImageById(@PathVariable("id") int id) {
imageService.deleteById(id);
return "redirect:/image/allImage";
}
}