[](()pom.xml
这些是本项目中要用到依赖,直接复制我的 pom.xml 即可。
<?xml version="1.0" encoding="UTF-8"?><project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.7.RELEASE
com.yusael
myfiles
0.0.1-SNAPSHOT
myfiles
文件的上传与下载实战
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.2
mysql
mysql-connector-java
5.1.43
runtime
com.alibaba
druid
1.1.12
org.projectlombok
lombok
true
commons-fileupload
commons-fileupload
1.4
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-maven-plugin
[](()数据库建表与环境准备
=============================================================================
需求分析:
-
要做的是文件管理系统,自然要存储文件的信息,需要文件信息表(t_files)
-
我们要做用户的登录功能,需要用户表(t_user)(此处为了简略,省略注册,若想看注册,可以看 [基于springboot+thymeleaf+mybatis的员工管理系统 —— 登录与注册](())
[](()建表SQL
用户表 t_user 的 SQL:
CREATE TABLE t_user
(
id
int(8) NOT NULL,
username
varchar(80) DEFAULT NULL,
password
varchar(80) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
文件信息表 t_files 的 SQL:
CREATE TABLE t_files
(
id
int(8) NOT NULL AUTO_INCREMENT,
oldFileName
varchar(200) DEFAULT NULL,
newFileName
varchar(300) DEFAULT NULL,
ext
varchar(20) DEFAULT NULL,
path
varchar(300) DEFAULT NULL,
size
varchar(200) DEFAULT NULL,
type
varchar(120) DEFAULT NULL,
isImg
varchar(8) DEFAULT NULL,
downcounts
int(6) DEFAULT NULL,
uploadTime
datetime DEFAULT NULL,
userId
int(8) DEFAULT NULL,
PRIMARY KEY (id
),
KEY userId
(userId
),
CONSTRAINT userId
FOREIGN KEY (userId
) REFERENCES t_user
(id
)
) ENGINE=InnoDB AUTO_INCREMENT=46 DEFAULT CHARSET=utf8;
数据库建完表以后,还需要在 application.properties 中配置。
[](()配置文件 application.properties
spring.application.name=files
server.port=8989
server.servlet.context-path=/files
##配置thymleaf(下面注释的是默认配置, 可以不设置)
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.servlet.content-type=text/html
#本项目使用了热部署, 想让热部署生效必须配置这个
spring.thymeleaf.cache=false
#默认无法直接访问templates下的页面, 需要设置
spring.resources.static-locations=classpath:/templates/, classpath:/static/
##mysql配置
#指定连接池类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#指定驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#指定url
spring.datasource.url=jdbc:mysql://localhost:3306/userfiles?characterEncoding=UTF-8
#指定用户名
spring.datasource.username=root
#指定密码
spring.datasource.password=1234
#指定mapper配置文件位置
mybatis.mapper-locations=classpath:/com/yusael/mapper/*.xml
#指定起别名了的类
mybatis.type-aliases-package=com.yusael.entity
#允许最大上传大小
spring.servlet.multipart.max-file-size=500MB
spring.servlet.multipart.max-request-size=500MB
##日志配置
logging.level.root=info
logging.level.com.yusael.dao=debug
[](()整体架构
搭建出项目的整体架构如下,然后可以开始进行开发各个功能了。
在启动类中加上 @MapperScan("com.yusael.dao")
来扫描 com.yusae.dao
包。
@SpringBootApplication
@MapperScan(“com.yusael.dao”)
public class MyfilesApplication {
public static void main(String[] args) {
SpringApplication.run(MyfilesApplication.class, args);
}
}
[](()前端页面
=======================================================================
这次的实战主要是为了熟悉文件上传与下载,因此页面十分简易。
[](()登录页面 login.html
欢迎访问用户文件管理系统
username:
password:
[](()文件列表页面 showAll.html
欢迎:
文件列表
下载
在线打开
删除
上传列表
[](()页面跳转控制器
==========================================================================
com.yusael.controller
包下创建 IndexController.java
:
package com.yusael.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping(“/index”)
public String index() {
return “login”;
}
}
[](()登录功能
=======================================================================
com.yusael.entity
包下创建数据库映射的实体类 User.java
:
package com.yusael.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
private Integer id;
private String username;
private String password;
}
com.yusael.dao
包下创建 UserDAO.java
:
package com.yusael.dao;
import com.yusael.entity.User;
import org.apache.ibatis.annotations.Param;
public interface UserDAO {
User login(@Param(“username”) String username, @Param(“password”) String password);
}
resources/com/yusael/mapper
目录下创建接口 UserDAOMapper.xml
:
select id, username, password from t_user
where username = #{username} and password = #{password}
com.yusael.service
包下创建接口 UserService.java
:
package com.yusael.service;
import com.yusael.entity.User;
public interface UserService {
User login(String username, String password);
}
com.yusael.service
包下创建接口的实现类 UserServiceImpl.java
:
package com.yusael.service;
import com.yusael.dao.UserDAO;
import com.yusael.entity.User;
import org.springframework.beans.factory.annotation.Autowired;