目录
1.CRUD
2.文件上传和下载
导入pom依赖:
4.0.0
org.example
ssm2
1.0-SNAPSHOT
war
ssm2 Maven Webapp
http://www.example.com
UTF-8
1.8
1.8
3.7.0
5.0.2.RELEASE
3.4.5
5.1.44
5.1.2
1.3.1
2.1.1
2.4.3
2.9.1
4.12
4.0.0
1.18.2
org.springframework
spring-context
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
mysql
mysql-connector-java
${mysql.version}
com.github.pagehelper
pagehelper
${pagehelper.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
org.apache.commons
commons-dbcp2
${commons.dbcp2.version}
org.apache.commons
commons-pool2
${commons.pool2.version}
org.apache.logging.log4j
log4j-core
${log4j2.version}
org.apache.logging.log4j
log4j-api
${log4j2.version}
org.apache.logging.log4j
log4j-web
${log4j2.version}
junit
junit
${junit.version}
test
javax.servlet
javax.servlet-api
${servlet.version}
provided
org.projectlombok
lombok
${lombok.version}
provided
org.springframework
spring-webmvc
${spring.version}
javax.servlet.jsp
javax.servlet.jsp-api
2.3.3
jstl
jstl
1.2
taglibs
standard
1.1.2
commons-fileupload
commons-fileupload
1.3.3
ssm2
src/main/java
**/*.xml
src/main/resources
jdbc.properties
*.xml
org.apache.maven.plugins
maven-compiler-plugin
${maven.compiler.plugin.version}
${maven.compiler.target}
${project.build.sourceEncoding}
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
mysql
mysql-connector-java
${mysql.version}
true
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
配置文件:
applicationContext.xml
applicationContext-mybatis.xml
generatorConfig.xml
jdbc.properties
log4j2.xml
springmvc-servlet.xml
web.xml:
Archetype Created Web Application
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/springmvc-servlet.xml
1
true
SpringMVC
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
Clazz:
package com.xbb.ssm.model;
public class Clazz {
protected Integer cid;
protected String cname;
protected String cteacher;
protected String pic;
public Clazz(Integer cid, String cname, String cteacher, String pic) {
this.cid = cid;
this.cname = cname;
this.cteacher = cteacher;
this.pic = pic;
}
public Clazz() {
super();
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCteacher() {
return cteacher;
}
public void setCteacher(String cteacher) {
this.cteacher = cteacher;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
}
ClazzMapper.java:
package com.xbb.ssm.mapper;
import com.xbb.ssm.model.Clazz;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ClazzMapper {
int deleteByPrimaryKey(Integer cid);
int insert(Clazz record);
int insertSelective(Clazz record);
Clazz selectByPrimaryKey(Integer cid);
List listPager(Clazz clazz);
int updateByPrimaryKeySelective(Clazz record);
int updateByPrimaryKey(Clazz record);
}
ClazzMapper.xml:
cid, cname, cteacher, pic
delete from t_struts_class
where cid = #{cid,jdbcType=INTEGER}
insert into t_struts_class (cid, cname, cteacher,
pic)
values (#{cid,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{cteacher,jdbcType=VARCHAR},
#{pic,jdbcType=VARCHAR})
insert into t_struts_class
cid,
cname,
cteacher,
pic,
#{cid,jdbcType=INTEGER},
#{cname,jdbcType=VARCHAR},
#{cteacher,jdbcType=VARCHAR},
#{pic,jdbcType=VARCHAR},
update t_struts_class
cname = #{cname,jdbcType=VARCHAR},
cteacher = #{cteacher,jdbcType=VARCHAR},
pic = #{pic,jdbcType=VARCHAR},
where cid = #{cid,jdbcType=INTEGER}
update t_struts_class
set cname = #{cname,jdbcType=VARCHAR},
cteacher = #{cteacher,jdbcType=VARCHAR},
pic = #{pic,jdbcType=VARCHAR}
where cid = #{cid,jdbcType=INTEGER}
ClazzBiz:
package com.xbb.ssm.biz;
import com.xbb.ssm.model.Clazz;
import com.xbb.ssm.util.PageBean;
import java.util.List;
public interface ClassBiz {
int deleteByPrimaryKey(Integer cid);
int insert(Clazz record);
int insertSelective(Clazz record);
Clazz selectByPrimaryKey(Integer cid);
int updateByPrimaryKeySelective(Clazz record);
int updateByPrimaryKey(Clazz record);
List listPager(Clazz clazz, PageBean pageBean);
}
ClazzBizImpl:
package com.xbb.ssm.biz.impl;
import com.xbb.ssm.biz.ClassBiz;
import com.xbb.ssm.mapper.ClazzMapper;
import com.xbb.ssm.model.Clazz;
import com.xbb.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author冰冰
* @create 2022-08-17 19:29
*/
@Service
public class ClazzBizImpl implements ClassBiz {
@Autowired
private ClazzMapper clazzMapper;
@Override
public int deleteByPrimaryKey(Integer cid) {
return clazzMapper.deleteByPrimaryKey(cid);
}
@Override
public int insert(Clazz record) {
return clazzMapper.insert(record);
}
@Override
public int insertSelective(Clazz record) {
return clazzMapper.insertSelective(record);
}
@Override
public Clazz selectByPrimaryKey(Integer cid) {
return clazzMapper.selectByPrimaryKey(cid);
}
@Override
public int updateByPrimaryKeySelective(Clazz record) {
return clazzMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(Clazz record) {
return clazzMapper.updateByPrimaryKey(record);
}
@Override
public List listPager(Clazz clazz, PageBean pageBean) {
return clazzMapper.listPager(clazz);
}
}
ClazzController:
package com.xbb.ssm.contorller;
import com.xbb.ssm.biz.ClassBiz;
import com.xbb.ssm.biz.impl.ClazzBizImpl;
import com.xbb.ssm.model.Clazz;
import com.xbb.ssm.model.dto.ClazzDto;
import com.xbb.ssm.util.PageBean;
import org.apache.logging.log4j.core.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.naming.Name;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @author冰冰
* @create 2022-08-17 20:05
*/
@Controller
@RequestMapping("/clz")
public class ClazzController {
@Autowired
private ClassBiz classBiz;
@RequestMapping("/list")
public String list(Clazz clazz, HttpServletRequest request){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List lst = this.classBiz.listPager(clazz, pageBean);
request.setAttribute("lst",lst);
request.setAttribute("pageBean",pageBean);
return "clzList";
}
@RequestMapping("/toEdit")
public String toEdit(Clazz clazz, HttpServletRequest request){
Integer cid = clazz.getCid();
//传递的id代表了修改,没传代表新增
if(cid != null){
List lst = this.classBiz.listPager(clazz, null);
request.setAttribute("b",lst.get(0));
}
return "clzEdit";
}
@RequestMapping("/add")
public String add(Clazz clazz){
this.classBiz.insertSelective(clazz);
return "redirect:/clz/list";
}
@RequestMapping("/edit")
public String edit(Clazz clazz){
this.classBiz.updateByPrimaryKeySelective(clazz);
return "redirect:/clz/list";
}
@RequestMapping("/del")
public String del(Clazz clazz){
this.classBiz.deleteByPrimaryKey(clazz.getCid());
return "redirect:/clz/list";
}
//文件上传
@RequestMapping("/upload")
public String upload(ClazzDto clazzDto){
//前台上传的文件
try {
/* MultipartFile picFile = clazzDto.getPicFile();
InputStream inputStream = picFile.getInputStream();
FileInputStream fout = new FileInputStream(new File("d/temp/1.png"));
byte[] bbuf = new byte[1024];
int len = 0;
while (true){
len = inputStream.read(bbuf,0,len);
fout.write(bbuf,0,len);
if(len <0){
break;
}
}*/
MultipartFile picFile = clazzDto.getPicFile();
String diskPath = "D:/tup/txt/";//图片存放地址
String requsetPath = "/upload/mvc/";//数据库保存的地址,也是访问地址
//拿到上传文件的名字
String filename = picFile.getOriginalFilename();
// FileUtils.copyInputStreamToFile(picFile.getInputStream(),new File(diskPath+filename));
Clazz clazz = new Clazz();
clazz.setCid(clazzDto.getCid());
clazz.setPic(requsetPath+filename);
this.classBiz.updateByPrimaryKeySelective(clazz);
}catch (Exception e){
e.printStackTrace();
}
return "redirect:/clz/list";
}
}
clzEdit:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
博客的编辑界面
clzList:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
博客列表
ID
班级名称
指导老师
班级相册
操作
${b.cid }
${b.cname }
${b.cteacher }
${b.pic }
修改
删除
上传图片
下载图片
效果展示:
ClazzDto:
package com.xbb.ssm.model.dto;
import com.xbb.ssm.model.Clazz;
import org.springframework.web.multipart.MultipartFile;
/**
* @author冰冰
* @create 2022-08-17 23:48
*/
public class ClazzDto extends Clazz {
private MultipartFile picFile;
public MultipartFile getPicFile(){
return picFile;
}
public void setPicFile(MultipartFile picFile){
this.picFile = picFile;
}
}
ClazzController:
package com.xbb.ssm.contorller;
import com.xbb.ssm.biz.ClassBiz;
import com.xbb.ssm.biz.impl.ClazzBizImpl;
import com.xbb.ssm.model.Clazz;
import com.xbb.ssm.model.dto.ClazzDto;
import com.xbb.ssm.util.PageBean;
import org.apache.logging.log4j.core.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.naming.Name;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @author冰冰
* @create 2022-08-17 20:05
*/
@Controller
@RequestMapping("/clz")
public class ClazzController {
@Autowired
private ClassBiz classBiz;
@RequestMapping("/list")
public String list(Clazz clazz, HttpServletRequest request){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List lst = this.classBiz.listPager(clazz, pageBean);
request.setAttribute("lst",lst);
request.setAttribute("pageBean",pageBean);
return "clzList";
}
@RequestMapping("/toEdit")
public String toEdit(Clazz clazz, HttpServletRequest request){
Integer cid = clazz.getCid();
//传递的id代表了修改,没传代表新增
if(cid != null){
List lst = this.classBiz.listPager(clazz, null);
request.setAttribute("b",lst.get(0));
}
return "clzEdit";
}
@RequestMapping("/add")
public String add(Clazz clazz){
this.classBiz.insertSelective(clazz);
return "redirect:/clz/list";
}
@RequestMapping("/edit")
public String edit(Clazz clazz){
this.classBiz.updateByPrimaryKeySelective(clazz);
return "redirect:/clz/list";
}
@RequestMapping("/del")
public String del(Clazz clazz){
this.classBiz.deleteByPrimaryKey(clazz.getCid());
return "redirect:/clz/list";
}
//文件上传
@RequestMapping("/upload")
public String upload(ClazzDto clazzDto){
//前台上传的文件
try {
/* MultipartFile picFile = clazzDto.getPicFile();
InputStream inputStream = picFile.getInputStream();
FileInputStream fout = new FileInputStream(new File("d/temp/1.png"));
byte[] bbuf = new byte[1024];
int len = 0;
while (true){
len = inputStream.read(bbuf,0,len);
fout.write(bbuf,0,len);
if(len <0){
break;
}
}*/
MultipartFile picFile = clazzDto.getPicFile();
String diskPath = "D:/tup/txt/";//图片存放地址
String requsetPath = "/upload/mvc/";//数据库保存的地址,也是访问地址
//拿到上传文件的名字
String filename = picFile.getOriginalFilename();
// FileUtils.copyInputStreamToFile(picFile.getInputStream(),new File(diskPath+filename));
Clazz clazz = new Clazz();
clazz.setCid(clazzDto.getCid());
clazz.setPic(requsetPath+filename);
this.classBiz.updateByPrimaryKeySelective(clazz);
}catch (Exception e){
e.printStackTrace();
}
return "redirect:/clz/list";
}
}
clzUpload:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/8/17
Time: 21:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title